The HLA exit
and exitif
statements let you return from a procedure without having to fall into the corresponding end
statement in the procedure. These statements behave a whole lot like the break
and breakif
statements for loops, except that they transfer control to the bottom of the procedure rather than out of the current loop. These statements are quite useful in many cases.
The syntax for these two statements is the following:
exitprocedurename
; exitif(boolean_expression
)procedurename
;
The procedurename
operand is the name of the procedure you wish to exit. If you specify the name of your main program, the exit
and exitif
statements will terminate program execution (even if you're currently inside a procedure rather than the body of the main program).
The exit
statement immediately transfers control out of the specified procedure or program. The conditional exitif
statement first tests the boolean expression and exits if the result is true. It is semantically equivalent to the following:
if(boolean_expression
) then exitprocedurename
; endif;
Although the exit
and exitif
statements are invaluable in many cases, you should avoid using them without careful consideration. If a simple if
statement will let you skip the rest of the code in your procedure, then by all means use the if
statement. Procedures that contain a lot of exit
and exitif
statements will be harder to read, understand, and maintain than procedures without these statements (after all, the exit
and exitif
statements are really nothing more than goto
statements, and you've probably heard already about the problems with goto
s). exit
and exitif
are convenient when you have to return from a procedure inside a sequence of nested control structures, and slapping an if..endif
around the remaining code in the procedure is impractical.