exit

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.

If you specify one of the following modifiers, the exit statement passes control out of the most recently entered loop of that type:

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.

If you specify the program modifier, you have several options:

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.

Syntax

Syntax diagram for the exit statement
statement
You can specify the kind of code block to exit (case, for, forEach, if, openUI, or while). This option is useful when you are deep in nested blocks. Processing continues after the specified statement.
label1
A label that is attached to an enclosing case, for, forEach, if, or while statement. Processing continues with the referenced statement.
returnValue
A literal integer or a variable, constant, or numeric expression that resolves to an integer. In main programs (but not called programs), the return value is made available to the operating system. For generated Java™ programs, the value must be in the range of -2147483648 to 2147483647, inclusive. For generated COBOL programs, the value must be in the range of 0 to 512, inclusive.

For other details on return values, see returnCode.

sysVar.returnCode
The system variable that includes the value returned to the operating system.

For details, see sysVar.returnCode.

label2
This syntax is provided only for compatibility with migrated programs. Use this syntax to jump to a specified label within the main function. Do not use labels in new code.

Examples

The following is an example of nested code blocks:
// 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.

Error conditions

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.

Compatibility

Table 1. Compatibility considerations for exit
Platform Issue
JavaScript generation Rich UI supports the following variations of the exit statement and no others: exit for, exit if, exit while, and exit case.

Feedback