Difference between revisions of "Control:For"

From Lavish Software Wiki
Jump to navigation Jump to search
 
Line 18: Line 18:
 
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 ==
+
=== 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 30: Line 35:
 
  9  
 
  9  
 
  10
 
  10
 
=== Example 1: For ===
 
variable int Count
 
for (Count:Set[0] ; ${Count}<=10 ; Count:Inc)
 
  echo ${Count}
 
  
 
=== Example 2: Continue ===
 
=== Example 2: Continue ===
Line 46: 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
 
  variable int Count
 
  for (Count:Set[0] ; ${Count}<=10 ; Count:Inc)
 
  for (Count:Set[0] ; ${Count}<=10 ; Count:Inc)
Line 56: 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 ==

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