13.5 Basic LOOP Statement

With each iteration of the basic LOOP statement, its statements run and control returns to the top of the loop. The LOOP statement ends when a statement inside the loop transfers control outside the loop or raises an exception.






To prevent an infinite loop, at least one statement must transfer control outside the loop. The statements that can transfer control outside the loop are:


A label that identifies basic_loop_statement (see "statement ::=" and "label"). CONTINUE, EXIT, and GOTO statements can reference this label.

Labels improve readability, especially when LOOP statements are nested, but only if you ensure that the label in the END LOOP statement matches a label at the beginning of the same LOOP statement (the compiler does not check).


  • Example 1-2, "Processing Query Result Rows One at a Time"

  • Example 4-9, "Basic LOOP Statement with EXIT Statement"

  • Example 4-10, "Basic LOOP Statement with EXIT WHEN Statement"

  • Example 4-11, "Nested, Labeled Basic LOOP Statements with EXIT WHEN Statements"

  • Example 4-13, "CONTINUE Statement in Basic LOOP Statement"

  • Example 4-14, "CONTINUE WHEN Statement in Basic LOOP Statement"

Related Topics

In this chapter:

In other chapters: