Difference between revisions of "Control:For"
(2 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
==Basic Information== | ==Basic Information== | ||
− | |||
== Forms == | == Forms == | ||
− | === | + | === For === |
− | ''' | + | '''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> | ||
− | |||
− | + | 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 === | ||
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 | + | 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 | 1 | ||
2 | 2 | ||
Line 34: | Line 36: | ||
10 | 10 | ||
− | === Example | + | === Example 2: Continue === |
− | + | variable int Count | |
− | + | for (Count:Set[0] ; ${Count}<=10 ; Count:Inc) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
{ | { | ||
; skip if Count is odd | ; skip if Count is odd | ||
Line 57: | Line 46: | ||
} | } | ||
− | === Example 4: Break === | + | == 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 | ; 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 | + | [[Category:LavishScript]] [[Category:LavishScript Control Structures]] |
Latest revision as of 10:03, 31 January 2008
Contents
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