Difference between revisions of "Control:For"

From Lavish Software Wiki
Jump to navigation Jump to search
 
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
==Basic Information==
 
==Basic Information==
This provides basic while and do while information.
 
 
== Forms ==
 
== Forms ==
=== While ===
+
=== For ===
  '''while''' <[[LavishScript:Mathematical Formulae|formula]]>
+
  '''for''' (<start command> ; <[[LavishScript:Mathematical Formulae|formula]]> ; <iterate command>)
 +
  <command or code block>
 +
- or -
 +
'''for''' (<[[LavishScript:Mathematical Formulae|formula]]> ; <iterate command>)
 
   <command or code block>
 
   <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 ===
+
A for loop will repeat the given command or code block while the formula given evaluates to non-zero. The formula is checked at the start, and then after each time the command or code block executes. The ''start command'' is executed before the first check of the formula, and the ''iterate command'' is executed before each successive check of the formulaThese will typically initialize a count variable and increment the count variable, respectively.
  '''do'''
 
  <command or code block>
 
  '''while''' <[[LavishScript:Mathematical Formulae|formula]]>
 
This form is very similar to the first formThe difference is simply that the code will always execute at least once, and the repeat condition is checked afterwards.
 
  
 
=== Break ===
 
=== Break ===
Line 19: Line 16:
 
=== 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.  If the loop is a do-while loop, the loop repeats without re-checking the condition.
+
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 ==
+
=== Example 1: For ===
Examples 1 and 2 both produce the following output:
+
variable int Count
 +
for (Count:Set[0] ; ${Count}<=10 ; Count:Inc)
 +
  echo ${Count}
 +
 
 +
== Example 1 output ==
 +
Example 1 produces the following output:
 
  1
 
  1
 
  2
 
  2
Line 34: Line 36:
 
  10
 
  10
  
=== Example 1: While ===
+
=== Example 2: Continue ===
  delcare Count int 0
+
  variable int Count
  while ${Count:Inc}<=10
+
  for (Count:Set[0] ; ${Count}<=10 ; Count:Inc)
  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
 
   ; skip if Count is odd
Line 57: Line 46:
 
  }
 
  }
  
=== Example 4: Break ===
+
== Example 2 output ==
  declare Count int 0
+
Example 2 produces the following output:
  while ${Count:Inc}<=10
+
0
 +
2
 +
4
 +
6
 +
8
 +
10
 +
 
 +
=== Example 3: Break ===
 +
  variable int Count
 +
  for (Count:Set[0] ; ${Count}<=10 ; Count:Inc)
 
  {
 
  {
 
   ; Break early if session number ${Count} exists
 
   ; Break early if session number ${Count} exists
Line 67: Line 65:
 
  }
 
  }
 
  echo Done. Session[${Count}] is ${Session[${Count}]}
 
  echo Done. Session[${Count}] is ${Session[${Count}]}
 +
 +
== Example 3 output ==
 +
Example 3 produces the following output:
 +
Done. Session[0] is FALSE
  
 
== See Also ==
 
== See Also ==
*[[LavishScript:Language_and_Engine_Overview]]
+
*[[LavishScript:Language_and_Engine_Overview|Language_and_Engine_Overview]]
 
*[[LavishScript:Language_and_Engine_Overview#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 10:03, 31 January 2008

Basic Information

Forms

For

for (<start command> ; <formula> ; <iterate command>)
  <command or code block>

- or -

for (<formula> ; <iterate command>)
  <command or code block>

A for loop will repeat the given command or code block while the formula given evaluates to non-zero. The formula is checked at the start, and then after each time the command or code block executes. The start command is executed before the first check of the formula, and the iterate command is executed before each successive check of the formula. These will typically initialize a count variable and increment the count variable, respectively.

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.

Example 1: For

variable int Count
for (Count:Set[0] ; ${Count}<=10 ; Count:Inc)
  echo ${Count}

Example 1 output

Example 1 produces the following output:

1
2
3
4
5
6
7
8
9 
10

Example 2: Continue

variable int Count
for (Count:Set[0] ; ${Count}<=10 ; Count:Inc)
{
  ; skip if Count is odd
  if ${Count}%2==1
    continue
  echo ${Count}
}

Example 2 output

Example 2 produces the following output:

0
2
4
6
8
10

Example 3: Break

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

Example 3 output

Example 3 produces the following output:

Done. Session[0] is FALSE

See Also