Difference between revisions of "Control:For"
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. |
== Examples == | == Examples == | ||
Line 34: | Line 31: | ||
10 | 10 | ||
− | === Example 1: | + | === Example 1: For === |
− | + | variable int Count | |
− | + | for (Count:Set[0] ; ${Count}<=10 ; Count:Inc) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
echo ${Count} | echo ${Count} | ||
− | |||
− | |||
− | === 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 58: | Line 47: | ||
=== Example 4: Break === | === Example 4: 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 74: | Line 63: | ||
*[[LavishScript:Mathematical Formulae|Mathematical Formulae]] | *[[LavishScript:Mathematical Formulae|Mathematical Formulae]] | ||
− | [[Category:LavishScript]] [[Category:LavishScript | + | [[Category:LavishScript]] [[Category:LavishScript Control Structures]] |
Revision as of 04:44, 16 May 2006
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.
Examples
Examples 1 and 2 both produce the following output:
1 2 3 4 5 6 7 8 9 10
Example 1: For
variable int Count for (Count:Set[0] ; ${Count}<=10 ; Count:Inc) echo ${Count}
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 4: 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}]}