PL/SQL CONTINUE

Summary: in this tutorial, you will learn how to use the PL/SQL CONTINUE or CONTINUE WHEN statement to exit the current loop iteration unconditionally or conditionally.

PL/SQL CONTINUE statement

The CONTINUE statement allows you to exit the current loop iteration and immediately continue on to the next iteration of that loop.

The CONTINUE statement has a simple syntax:

CONTINUE;Code language: SQL (Structured Query Language) (sql)

Typically, the CONTINUE statement is used within an IF THEN statement to exit the current loop iteration based on a specified condition as shown below:

IF condition THEN
    CONTINUE;
END IF;Code language: SQL (Structured Query Language) (sql)

The CONTINUE can be used in all loop constructs including LOOP, FOR LOOP and WHILE LOOP.

PL/SQL CONTINUE statement example

The following is a simple example of using the CONTINUE statement to skip over loop body execution for odd numbers:

BEGIN
  FOR n_index IN 1 .. 10
  LOOP
    -- skip odd numbers
    IF MOD( n_index, 2 ) = 1 THEN
      CONTINUE;
    END IF;
    DBMS_OUTPUT.PUT_LINE( n_index );
  END LOOP;
END;Code language: SQL (Structured Query Language) (sql)

The output is:

2
4
6
8
10Code language: SQL (Structured Query Language) (sql)

PL/SQL CONTINUE WHEN statement

The CONTINUE WHEN statement exits the current loop iteration based on a condition and immediately continues to the next iteration of that loop.

The syntax of CONTINUE WHEN statement is:

CONTINUE WHEN condition;Code language: SQL (Structured Query Language) (sql)

The condition of the WHEN clause is evaluated each time the CONTINUE WHEN statement is reached. If the condition is TRUE, the current loop is skipped, and control is transferred to the next iteration of the loop. If the condition is not TRUE, either FALSE or NULL, the CONTINUE WHEN statement does nothing.

Essentially, the CONTINUE WHEN statement is the combination of  an IF THEN statement and CONTINUE statement:

IF condition THEN
    CONTINUE;
END IF;Code language: SQL (Structured Query Language) (sql)

Similar to the CONTINUE statement, you can use the CONTINUE WHEN statement in LOOP, FOR LOOP and WHILE LOOP.

PL/SQL CONTINUE statement example

The following example illustrates how to use the CONTINUE WHEN statement to skip over loop body execution for even numbers:

BEGIN
  FOR n_index IN 1 .. 10
  LOOP
    -- skip even numbers
    CONTINUE
  WHEN MOD( n_index, 2 ) = 0;
    DBMS_OUTPUT.PUT_LINE( n_index );
  END LOOP;
END;Code language: SQL (Structured Query Language) (sql)

Here is the output:

1
3
5
7
9Code language: SQL (Structured Query Language) (sql)

In this tutorial, you have learned how to exit the current loop iteration and continue to the next one using the PL/SQL CONTINUE or CONTINUE WHEN statement.

Was this tutorial helpful?