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:
For( i = 1, i <= 5, i++,
If( i == 3, Break() );
Print( "i=" || Char( i ) );
);
"i=1"
"i=2"
For( |
Begins the For() loop.
|
i = 1, |
Sets i to 1.
|
i <= 5, |
As long as i is less than or equal to 5, continues evaluating the loop.
|
i++, |
|
If( |
Begins the If() loop.
|
i == 3, Break() |
If i is equal to 3, breaks the loop.
|
); |
|
Print( |
|
"i=" |
Prints the string "i=" to the log.
|
|| |
Places "i=" on the same line as the value that follows.
|
Char(i)); |
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 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.
"i=1"
"i=2"
"i=3"
For( i = 1, i <= 5, i++,
If( i < 3, Continue() );
Print( "i=" || Char( i ) );
);
"i=3"
"i=4"
"i=5"
For( |
Begins the For() loop.
|
i = 1, |
Sets i to 1.
|
i <= 5, |
|
i++, |
|
If( |
Begins the If() loop.
|
i < 3, Continue() |
|
); |
Ends the If() loop.
|
Print( |
|
"i=" |
Prints the string "i=" to the log.
|
|| |
Places "i=" on the same line as the value that follows.
|
Char(i)); |
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.
|
); |