The Break() and Continue() functions give you more control over looping. Break() immediately stops the loop and proceeds to the next expression that follows the loop. Continue() is a gentler form of Break(). It immediately stops the current iteration of the loop and continues with the next iteration of the loop.
Break() is typically used inside a conditional expression. For example:
Begins the For() loop.
|
|
Sets i to 1.
|
|
As long as i is less than or equal to 5, continues evaluating the loop.
|
|
Begins the If() loop.
|
|
If i is equal to 3, breaks the loop.
|
|
Prints the string "i=" to the log.
|
|
Places "i=" on the same line as the value that follows.
|
|
Prints the value of i to the log.
The For() loop then repeats until the value of i is less than or equal to 5, breaking when i equals 3, and breaking and printing only when i is less than 3.
|
|
Note that when the If() and Break() expressions follow Print(), the script prints the values of i from 1 to 3, because the loop breaks after "i=3" is printed.
Begins the For() loop.
|
|
Sets i to 1.
|
|
Begins the If() loop.
|
|
Ends the If() loop.
|
|
Prints the string "i=" to the log.
|
|
Places "i=" on the same line as the value that follows.
|
|
Prints the value of i to the log.
The For() loop then repeats until the value of i is less than or equal to 5, continuing and printing only when i is equal to or greater than 3.
|
|