Copyright © tutorialspoint.com

Euphoria - The while statement

previous next


Advertisements

A while loop is a control structure that allows you to repeat a task a certain number of times.

Syntax:

The syntax of a while loop is:

while expression do
   -- Statements executed if expression returns true
end while

When executing, if the expression result is true then the actions inside the loop will be executed. This will continue as long as the expression result is true.

Here key point of the while loop is that the loop might not ever run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Example:

#!/home/euphoria-4.0b2/bin/eui

integer a = 10

while a < 20 do
   printf(1, "value of a : %d\n", a)
   a = a + 1
end while

This would produce following result:

value of a : 10
value of a : 11
value of a : 12
value of a : 13
value of a : 14
value of a : 15
value of a : 16
value of a : 17
value of a : 18
value of a : 19

The while....with entry statement:

It is often the case that the first iteration of a loop is somehow special. Some things have to be done before the loop starts. They are done before the statement starting the loop.

The with entry statement serves the purpose very well. You need to use this statement with while loop and just add the entry keyword at the point you wish the first iteration starts.

Syntax:

The syntax of a while loop with entry is:

while expression with entry do
   -- Statements executed if expression returns true
entry
   -- Initialisation statements.
end while

Before executing the expression, it would execute initialisation statements and then it would start as a normal while loop and later on these initialisation statements would become part of loop body.

Example:

#!/home/euphoria-4.0b2/bin/eui

integer a = 10

while a < 20 with entry do
   printf(1, "value of a : %d\n", a)
   a = a + 1
entry
   a = a + 2
end while

This would produce following result:

value of a : 12
value of a : 15
value of a : 18

The while....label statement:

A while loop can have a label clause just before the first do keyword. You can keep label clause before or after enter clause.

A while loop label is used just to name the loop block and label names must be double quoted constant strings having single or multiple words. The label keyword is a case sensitive and should be written as label.

Syntax:

The syntax of a while loop with label clause is:

while expression label "Label Name" do
   -- Statements executed if expression returns true
end while

labels would be very useful when you would use nested while loops. You can use continue or exit loop control statements with label names to control the flow of loops.

Example:

#!/home/euphoria-4.0b2/bin/eui

integer a = 10
integer b = 20

while a < 20 label "OUTER" do
   printf(1, "value of a : %d\n", a)
   a = a + 1
   while b < 30 label "INNER" do
      printf(1, "value of b : %d\n", b)
      b = b + 1
      if b > 25 then
         continue "OUTER"  -- go to start of OUTER loop
      end if
   end while
end while

This would produce following result:

value of a : 10
value of b : 20
value of b : 21
value of b : 22
value of b : 23
value of b : 24
value of b : 25
value of a : 11
value of b : 26
value of a : 12
value of b : 27
value of a : 13
value of b : 28
value of a : 14
value of b : 29
value of a : 15
value of a : 16
value of a : 17
value of a : 18
value of a : 19

previous next

Copyright © tutorialspoint.com