Table of Contents
1. exit
Terminates the innermost enclosing loop.
1.3. Description
exit terminates execution of the innermost, dynamically enclosing looping context, regardless of lexical relationship.
Valid looping contexts created by:
exit pops the execution stack to the level of the loop operator, then resumes execution at the next object after that operator.
1.5. Examples
Exit from for loop
0 1 10 {
dup 5 eq { exit } if
=
} for
% Prints: 0 1 2 3 4 (stops at 5)
Exit from repeat
10 {
randomCondition { exit } if
doWork
} repeat
Exit from loop
{
getData
dup endMarker eq { pop exit } if
process
} loop
Exit from forall
[1 2 3 4 5] {
dup 3 eq { exit } if
10 mul
} forall
% Stack: 10 20 (stopped at 3)
1.6. Common Use Cases
1.7. Common Pitfalls
Must Be in Loop - exit only works within a looping context.
|
{ exit } if % Error: invalidexit (not in loop)
| Exits Innermost Loop Only - In nested loops, only exits the immediately enclosing loop. |
1 1 10 {
1 1 10 {
exit % Only exits inner loop
} for
} for
Cannot Exit run or stopped - Attempting to exit these contexts causes [invalidexit].
|
{ { exit } loop } stopped % Error: invalidexit
| Use Flag for Outer Loop Exit - Set a flag variable to exit multiple levels. |
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
No enclosing loop context, or attempted to exit |