A while loops is a control structure that allows you to repeat a task a certain number of times.
Syntax
The syntax of a while loop is as follows −
while expression do -- Statements executed if expression returns true end while
When executing, if the expression results in true then the actions inside the loop is executed. This continues as long as the expression result is true.
The 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 is skipped and the first statement after the while loop is 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 produce 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 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 as follows −
while expression with entry do -- Statements executed if expression returns true entry -- Initialisation statements. end while
Before executing the expression, it executes initialization statements and then it starts as a normal while loop. Later, these initialization statements become part of the 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 produces the 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 as follows −
while expression label "Label Name" do -- Statements executed if expression returns true end while
The labels are very useful when you 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 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
Previous Topic:-Click Here
Pingback: Euphoria - Loop Types - Adglob Infosystem Pvt Ltd