Control:For

From Lavish Software Wiki
Jump to navigation Jump to search

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