3. Statements : OpenROAD Language Statements : For Statement : Breaking Out of For Loops
 
Share this page                  
Breaking Out of For Loops
To break out of a for 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 endfor statement. For example:
for i = 1 to arr.Lastrow do
     /* statementlist1 */
     if arr.Name = '' then
          endloop;
     endif;
     /* statementlist2 */
endfor;
If condition is true, statementlist2 is 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, increments or decrements the index variable, and begins the next iteration of the loop. For example:
for i = 1 to arr.Lastrow do
     /* statementlist1 */
     if arr.Name = '' then
          continue;
     endif;
     /* statementlist2 */
endfor;
If condition is true, statementlist2 is not executed in that pass through the loop, and execution continues with the next iteration of the 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 for 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: for i = 1 to arr1.Lastrow do
     /* statementlist1 */
     label2: for j = 1 to arr2.Lastrow do
          /* statementlist2 */
          if arr.Name = '' then
               endloop label1;
          elseif arr.FullTime = FALSE then
               endloop label2;
          endif;
          /* statementlist3 */
     endfor;
     /* statementlist4 */
endfor;
There are two possible breaks out of the inner loop. If condition1 is true, both loops are closed, and control resumes at the statement following the outer loop. If condition2 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 which loop you want to continue. For example:
loop1: for i = 1 to arr1.Lastrow do
     /* statementlist1 */
     loop2: for j = 1 to arr2.Lastrow do
          /* statementlist2 */
          if arr.FullTime = FALSE then
               continue loop1;
          endif;
          /* statementlist3 */
     endfor;
     /* statementlist4 */
endfor;
Whenever condition occurs, loop2 is abandoned and control goes to the next iteration of loop1.