Euphoria - The loop until statement



A loop...until loop is similar to a while loop, except that a loop...until loop is guaranteed to execute at least one time.

Syntax

The syntax of a loop...until is as follows −

loop do
   -- Statements to be executed.
until expression

Notice that the expression appears at the end of the loop, hence the statements in the loop execute once before the expression's value is tested.

If the expression returns true, the flow of control jumps back up to do, and the statements in the loop execute again. This process repeats until the expression is false.

Example

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

integer a = 10

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

This produces the 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 loop....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 loop...until and just add the entry keyword at the point you wish the first iteration starts.

Syntax

The syntax of a loop...until loop with entry is as follows −

loop with entry do
   -- Statements to be executed.
entry
   -- Initialisation statements.
until expression

Before executing the expression, it executes initialization statements and then it starts as a normal loop. Later, these initialization statements become part of loop body.

Example

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

integer a = 10

loop with entry do
   printf(1, "value of a : %d\n", a)
   a = a + 1
entry
   a = a + 2
until a > 20

This produces the following result −

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

The loop....label statement

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

This 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 loop...until with label clause is as follows −

loop label "Label Name" do
   -- Statements to be executed.
until expression

The labels are very useful when you use nested 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

loop label "OUTER" do
   printf(1, "value of a : %d\n", a)
   a = a + 1
   
   loop 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
   until b > 30
until a > 20

This produces the 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

NOTE − The above example should work as explained, but looks like Euphoria interpreter has some problem and it is working as expected, may be it would be fixed in future versions of Euphoria.

euphoria_loop_types.htm
Advertisements