|
| Previous: 2.7.4 Loop Statements | TOC | Index | Back | Next: 2.7.6 Compound Statements |
A branch statement passes control to a specific point in a program. Typical destinations for branch commands are the beginnings or the ends of loops or the ends of procedures or programs. There is no branch command with a freely definable destination like Goto in BCPL.
The LEAVE command causes the immediate termination of the innermost WHILE or FOR loop. There are no operands to LEAVE.
The following code compares the characters of two strings A and B. The loop is left at the first position where the strings differ, but in any case after 100 steps:
FOR (i=0, 100) IF (a::i \= b::i) LEAVE;
The loop is set up for 100 passes and the LEAVE command makes the loop terminate, as soon as a mismatch is found.
The LOOP command transfers control to the beginning of the innermost loop. Like LEAVE, it has does not have any operands. If LOOP is used inside of a FOR loop, it branches to the increment part where the value of the index variable is modified. In WHILE loops, it branches directly to the point where the exit condition is checked.
To leave a procedure, a RETURN statement may be used. It has the general forms
RETURN expression;
and
RETURN ;
The statement evaluates the specified expression, if any, and passes it back to the calling procedure. Then, it performs a branch to the end of the procedure where local storage is released and the procedure is left. The value received by the calling procedure is the value of expression:
P(x) RETURN x*x;
Q() DO VAR y;
y := P(5);
END
In this short example program, 5 is passed as an argument to the procedure P. The procedure computes the square of its argument and returns it to Q where the result (25) will be stord in y.
When no expression is specified after RETURN, a zero value will be assumed. Therefore,
RETURN;
is the short form of
RETURN 0;
All the above branch statements take care of locally allocated storage. If, for example, local symbols are defined in the bodies of loops, LOOP and LEAVE will release this storage before branching to their respective destinations. This allows the use of these commands in any loop context, even if local symbols are present.
The HALT statement with the general forms
HALT constant-expression ;
and
HALT ;
branches to the end of the entire program.
If necessary, the command cleans up the runtime environment of the program The value of the specified constant expression is returned to the calling process. Only the least significant eight bits are guaranteed to be returned to the caller.
The argument of HALT may be omitted. In this case, zero will be delivered to the caller.
| Previous: 2.7.4 Loop Statements | TOC | Index | Back | Next: 2.7.6 Compound Statements |