Labeled WHILE Loops

To create a labeled WHILE loop, you can declare a loop label before the initial WHILE keyword, and repeat the label after the END WHILE keywords, as in the two WHILE loops of the following example:
CREATE PROCEDURE ex_cont_ex()
   DEFINE i,s,j, INT;
   <<while_jlab>>
   WHILE j < 20
      IF j > 10 THEN 
         CONTINUE WHILE;
      END IF
      LET i,s = j,0;
      <<while_slab>>
      WHILE i > 0
         LET i = i -1;
         IF i = 5 THEN
            EXIT while_jlab;
         END IF
      END WHILE while_slab
   END WHILE while_jlab
END PROCEDURE;

Here the EXIT while_jlab statement has the same effect that the EXIT or EXIT FOR keywords would have, terminating both the outer WHILE loop and the routine. In this example, the statement that includes the EXIT while_jlab statement has the same effect that EXIT while_jlab WHEN i = 5 would have.

You can also label a LOOP statement that begins with a loop <<label>> specification that immediately precedes the initial WHILE keyword and condition. In this type of loop, the CONTINUE LOOP, EXIT LOOP, and END LOOP keywords replace the CONTINUE WHILE, EXIT WHILE, and END WHILE keywords. Both the LOOP and WHILE keywords are optional after the CONTINUE and EXIT keywords, but the END LOOP keywords are required in SPL loop statements that include the LOOP keyword.

You can use similar syntax to create an unlabeled loop that omits the <<label>> declaration that immediately precedes the WHILE condition specification. In this case, you must also omit the undelimited loop label identifier that follows the END LOOP keywords. See the LOOP statement for a description and examples of these forms of labeled and unlabeled loop statements that enable you to combine WHILE statement syntax, with its condition-based number of loop iterations, with the "loop forever" syntax of the LOOP statement.

Related Statements

<< Label >> statement, CONTINUE, EXIT, LOOP