The EGL exit statement provides an immediate exit from all or part of a function or of a block of code controlled by a conditional or loop statement.
If you do not specify a modifier, exit transfers control out of the most recently entered block of code governed by a case, for, forEach, if, openUI, or while statement. If the exit statement is not inside a loop, it transfers control out of the most recently entered function.
These modifiers become powerful when you use nested loops; see the first example in the "Examples" section below. If you are not inside a loop of the specified type when you specify one of these modifiers, EGL displays a validation error.
exit program (sysVar.returnCode);
exit program (myCustomer.customerNumber);
EGL also provides a stack modifier for compatibility with older VisualAge® Generator programs. Do not use this modifier when developing new programs; instead, keep tighter control over program flow by throwing an exception. For more information, see "Exception handling." The stack modifier returns control directly to the main function. In the process, EGL removes all references to the intermediate functions in the runtime stack, which lists the series of functions that led from the main function to the current one. If the main function called a function with out or inOut modifiers, the current values of those arguments are available to the main function after the exit stack statement. If you use the stack modifier in the main function, control simply passes to the next sequential statement (even if you specify a label).
When you use the stack modifier, you can specify a label to jump to when you return to the main function. EGL provides this capability solely for compatibility with migrated programs; do not use labels or goTo statements in new code because they do not conform to structured programming standards.
For other details on return values, see returnCode.
For details, see sysVar.returnCode.
// print 1st 100 customers with non-zero balance while (getCustomer() != 0) for(i from 1 to 100 by 1) if (myCustomer.customerBalance == 0) exit for; end printBalance(myCustomer); end end
Here the exit for statement sends control back to the initial while statement, causing it to loop again. Without the for modifier, the exit statement breaks the if statement and returns to the for loop.
A validation error occurs if you specify a type of block to exit when the exit statement is not inside the specified loop.
The exit statement must also be in the same function as the specified block. In other words, if you call functionA from inside a while loop, and functionA contains no while loops of its own, you cannot put an exit while statement inside the function.