Breaking Out of While Loops
To break out of a while loop, you can use the endloop, continue, resume, or return statement.
If you use the endloop statement, OpenROAD closes the loop immediately and continues execution with the first statement following the endwhile statement. For example:
while name like pattern do
name = left(name, length(name)-1);
if name = '' then
endloop;
endif;
/* other statements */
endwhile;
If the name is empty, the other statements are not executed in that pass through the loop, and the entire loop is closed.
If you use the continue statement, OpenROAD skips the remaining statements in the statement list and begins the next iteration of the loop. For example:
while name like pattern do
name = left(name, length(name)-1);
if name like '% ' then
continue;
endif;
/* other statements */
endwhile;
If the name has trailiing blanks, the other statements are not executed in that pass through the loop, and execution continues with the next iteration of the while loop.
If you use the resume statement, OpenROAD terminates both the loop and the current event block.
If you use the return statement, OpenROAD terminates the current frame, procedure, or method.
You can use nested while statements in 4GL, and you can control breaking out of nested loops by using labels. Both the endloop and the continue statements let you specify labels. Specifying a label with the endloop statement lets you break to a specific level. For example:
label1: while name != '' do
label2: while name like '% ' do
name = left(name, length(name)-1);
if name = '' then
endloop label1;
elseif length(name) < 3 then
endloop label2;
endif;
/* statementlist3 */
endwhile;
/* statementlist4 */
endwhile;
There are two possible breaks out of the inner loop. If condition3 is true, both loops are closed, and control resumes at the statement following the outer loop. If condition4 is true, only the inner loop is closed, and execution continues at the beginning of statementlist4.
If no label is specified after endloop, OpenROAD terminates only the loop that issued the endloop statement.
Specifying a label with the continue statement lets you specify with which loop you want to continue. For example:
loop1: while condition1 do
/* statementlist1 */
loop2: while condition2 do
/* statementlist2 */
if condition3 then
continue loop1;
endif;
/* statementlist3 */
endwhile;
/* statementlist4 */
endwhile;
Whenever condition3 occurs, loop2 is abandoned and control passes to loop1.