Difference between revisions of "Control:While"

From Lavish Software Wiki
Jump to navigation Jump to search
 
Line 35: Line 35:
  
 
=== Example 1: While ===
 
=== Example 1: While ===
  delcare Count int 0
+
  variable int Count=0
 
  while ${Count:Inc}<=10
 
  while ${Count:Inc}<=10
 
   echo ${Count}
 
   echo ${Count}
  
 
=== Example 2: Do-While ===
 
=== Example 2: Do-While ===
  declare Count int 1
+
  variable int Count=1
 
  do
 
  do
 
  {
 
  {
Line 48: Line 48:
  
 
=== Example 3: Continue ===
 
=== Example 3: Continue ===
  declare Count int 0
+
  variable int Count=0
 
  while ${Count:Inc}<=10
 
  while ${Count:Inc}<=10
 
  {
 
  {
Line 58: Line 58:
  
 
=== Example 4: Break ===
 
=== Example 4: Break ===
  declare Count int 0
+
  variable int Count=0
 
  while ${Count:Inc}<=10
 
  while ${Count:Inc}<=10
 
  {
 
  {
Line 69: Line 69:
  
 
== See Also ==
 
== See Also ==
*[[LavishScript:Script Development|Script Development]]
+
*[[LavishScript:Language_and_Engine_Overview|Language_and_Engine_Overview]]
*[[LavishScript:Script Development#Control Structures|Control Structures]]
+
*[[LavishScript:Language_and_Engine_Overview#Control Structures|Control Structures]]
 
*[[LavishScript:Commands|Commands]]
 
*[[LavishScript:Commands|Commands]]
 
*[[LavishScript:Mathematical Formulae|Mathematical Formulae]]
 
*[[LavishScript:Mathematical Formulae|Mathematical Formulae]]
  
[[Category:LavishScript]] [[Category:LavishScript Commands]]
+
[[Category:LavishScript]] [[Category:LavishScript Control Structures]]

Latest revision as of 04:45, 16 May 2006

Basic Information

This provides basic while and do while information.

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

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

Example 2: Do-While

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

Example 3: Continue

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

Example 4: Break

variable int Count=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