Difference between revisions of "Control:While"

From Lavish Software Wiki
Jump to navigation Jump to search
 
Line 10: Line 10:
 
  '''while''' <[[LavishScript:Mathematical Formulae|formula]]>
 
  '''while''' <[[LavishScript:Mathematical Formulae|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.
 
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.
 +
 +
=== 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.
  
 
== Examples ==
 
== Examples ==
The following two examples both produce the following output:
+
Examples 1 and 2 both produce the following output:
 
  1
 
  1
 
  2
 
  2
Line 24: Line 32:
 
  10
 
  10
  
=== While ===
+
=== Example 1: While ===
 
  delcare Count int 0
 
  delcare Count int 0
 
  while ${Count:Inc}<=10
 
  while ${Count:Inc}<=10
 
   echo ${Count}
 
   echo ${Count}
  
=== Do-While ===
+
=== Example 2: Do-While ===
 
  declare Count int 1
 
  declare Count int 1
 
  do
 
  do
Line 37: Line 45:
 
  while ${Count:Inc}<=10
 
  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 ==
 
== See Also ==

Revision as of 02:19, 13 September 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.

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.

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