Difference between revisions of "Control:While"

From Lavish Software Wiki
Jump to navigation Jump to search
Line 17: Line 17:
 
=== Continue ===
 
=== Continue ===
 
  continue
 
  continue
Continue is used to immediately skip ''to'' the end of the loop, where the repeat condition is checked and may loop again if the condition is met.
+
Continue is used to immediately skip ''to'' the end of the loop, where the repeat condition is checked and may loop again if the condition is met.  If the loop is a do-while loop, the loop repeats without re-checking the condition.
  
 
== Examples ==
 
== Examples ==

Revision as of 02:16, 21 October 2005

Forms

While

while <formula>
  <command or code block>

This form will conditionally execute the following command or code block. The command or code block must begin on the following line. The command or code block will be executed if the result of the calculation is not equal to zero (0), which means it will be skipped if the result of the calculation is equal to zero (0). After executing the command or code block, execution will return to the while, where the entire process repeats (formula is evaluated, code is either executed or skipped, ad infinitum).

Do-While

do
  <command or code block>
while <formula>

This form is very similar to the first form. The difference is simply that the code will always execute at least once, and the repeat condition is checked afterwards.

Break

break

Break is used to immediately abort from the current loop, and continue execution outside of the loop. See also: Break for Switches

Continue

continue

Continue is used to immediately skip to the end of the loop, where the repeat condition is checked and may loop again if the condition is met. If the loop is a do-while loop, the loop repeats without re-checking the condition.

Examples

Examples 1 and 2 both produce the following output:

1
2
3
4
5
6
7
8
9 
10

Example 1: While

delcare Count int 0
while ${Count:Inc}<=10
  echo ${Count}

Example 2: Do-While

declare Count int 1
do
{
  echo ${Count}
}
while ${Count:Inc}<=10

Example 3: Continue

declare Count int 0
while ${Count:Inc}<=10
{
  ; skip if Count is odd
  if ${Count}%2==1
    continue
  echo ${Count}
}

Example 4: Break

declare Count int 0
while ${Count:Inc}<=10
{
  ; Break early if session number ${Count} exists
  if ${Session[${Count}](exists)}
     break
  echo ${Count}
}
echo Done. Session[${Count}] is ${Session[${Count}]}

See Also