Summary: In this tutorial, you’ll learn how to use the PL/pgSQL CONTINUE
statement to skip the remaining statements in the current loop iteration and begin the next iteration.
Overview of the PL/pgSQL CONTINUE statement #
The CONTINUE
statement skips the remaining statements in the current iteration of a loop and starts a new iteration based on a condition.
Here’s the most basic syntax of the PL/pgSQL CONTINUE
statement:
CONTINUE;
Code language: PHP (php)
If you want to refer to a specific loop (in nested loops), you can use a loop label in the CONTINUE
statement:
CONTINUE loop_label;
Code language: PHP (php)
Typically, you specify a condition to skip the current iteration:
IF condition THEN
CONTINUE;
END IF;
Code language: PHP (php)
However, the CONTINUE
statement has a more concise syntax to do so:
CONTINUE WHEN condition;
Code language: PHP (php)
The statement skips the current iteration only when the condition is true.
Using CONTINUE statement with LOOP statement #
The following example uses the CONTINUE
statement to skip the even numbers between 1 and 10:
DO
$$
DECLARE
n INT := 1;
BEGIN
LOOP
IF n % 2 = 0 THEN
n := n + 1;
CONTINUE;
END IF;
RAISE NOTICE 'Odd number: %', n;
n := n + 1;
EXIT WHEN n > 10;
END LOOP;
END;
$$;
Code language: PHP (php)
How it works:
- The
LOOP
iterates over numbers from 1 to 10. - The
CONTINUE
statement skips the remaining statements of the iteration where the counter is even. - The
RAISE NOTICE
statement displays only odd numbers.
Output:
NOTICE: Odd number: 1
NOTICE: Odd number: 3
NOTICE: Odd number: 5
NOTICE: Odd number: 7
NOTICE: Odd number: 9
Code language: HTTP (http)
Using CONTINUE statement with a FOR Loop #
The following example uses the CONTINUE
statement to skip iterations where the number is even:
DO
$$
BEGIN
FOR n IN 1..10 LOOP
CONTINUE WHEN n % 2 = 0;
RAISE NOTICE 'Odd number: %', n;
END LOOP;
END
$$;
Code language: PHP (php)
How it works:
- The
FOR
loop iterates over numbers from 1 to 10. - The
CONTINUE
skips the current iteration if the number is even. - The
RAISE NOTICE
displays the odd numbers.
Output:
NOTICE: Odd number: 1
NOTICE: Odd number: 3
NOTICE: Odd number: 5
NOTICE: Odd number: 7
NOTICE: Odd number: 9
Code language: HTTP (http)
Using CONTINUE statement with a WHILE Loop #
The following example uses the CONTINUE
statement to count numbers while skipping multiples of 3:
DO
$$
DECLARE
counter INTEGER := 1;
BEGIN
WHILE counter <= 10 LOOP
IF counter % 3 = 0 THEN
counter := counter + 1;
CONTINUE;
END IF;
RAISE NOTICE 'Number: %', counter;
counter := counter + 1;
END LOOP;
END;
$$;
Code language: PHP (php)
How it works:
- The
WHILE
loop runs until the counter exceeds 10. CONTINUE
skips the iteration if the counter is a multiple of 3.- The
RAISE NOTICE
displays only non-multiples of 3.
Output:
NOTICE: Number: 1
NOTICE: Number: 2
NOTICE: Number: 4
NOTICE: Number: 5
NOTICE: Number: 7
NOTICE: Number: 8
NOTICE: Number: 10
Code language: JavaScript (javascript)
Summary #
- Use the
CONTINUE
statement to skip the remaining statements in the current iteration of a loop and begin the next iteration based on a condition.