PostgreSQL Continue (original) (raw)

Last Updated : 23 Jul, 2025

The **CONTINUE statement in **PostgreSQL is used to prematurely skip the current iteration of a loop and proceed directly to the next iteration. This functionality applies to all types of loops, including unconditional loops, **WHILE loops, and **FOR loops.

Let us get a better understanding of the CONTINUE statement in **PostgreSQLfrom this article.

CONTINUE Statement in PostgreSQL

The **CONTINUE statement is used to skip the remaining statements in the current iteration and move to the next iteration. It can be used with all types of loops: **unconditional, **WHILE, and **FOR loops.

PostgreSQL - Continue

**Syntax

CONTINUE [ label ] [ **WHEN boolean-expression ];

If we analyze the above syntax:

Both the **label and **WHEN condition is optional and may or may not be used with the continue statement.

PostgreSQL CONTINUE Statement Examples

Let us take a look at some of the examples of **CONTINUE Statement in **PostgreSQL to better understand the concept.

**Example 1: Displaying Even Numbers from 1 to 10

The following example will be used to display the even numbers from 1 to 10.

**Query:

do ∗∗∗∗declare∗∗cntint=0;∗∗∗∗begin∗∗loop−−incrementofcntcnt=cnt+1;−−exittheloopifcnt>10exit∗∗∗∗when∗∗cnt>10;−−skiptheiterationifcntisanoddnumbercontinue∗∗∗∗when∗∗mod(cnt,2)=1;−−printoutthecntraisenotice′∗∗∗∗end∗∗loop;∗∗∗∗end∗∗;**declare cnt int = 0; **begin loop -- increment of cnt cnt = cnt + 1; -- exit the loop if cnt > 10 exit **when cnt > 10; -- skip the iteration if cnt is an odd number continue **when mod(cnt,2) = 1; -- print out the cnt raise notice '%', cnt; **end loop; **end;declarecntint=0;beginloopincrementofcntcnt=cnt+1;exittheloopifcnt>10exitwhencnt>10;skiptheiterationifcntisanoddnumbercontinuewhenmod(cnt,2)=1;printoutthecntraisenoticeendloop;end;;

**Output:

PostgreSQL CONTINUE Statement Example

**Explanation: In the above example, we use the continue statement to skip the odd numbers by using the fact that the remainder when an odd number is divided by 2 is 1.

Example 2: Skipping a Specific Number

The following example will be used to display all numbers from 1 to 10 without displaying the number 6.

do ∗∗∗∗declare∗∗cntint=0;∗∗∗∗begin∗∗loop−−incrementofcntcnt=cnt+1;−−exittheloopifcnt>10exit∗∗∗∗when∗∗cnt>10;−−skiptheiterationifcntisanoddnumbercontinue∗∗∗∗when∗∗cnt=6;−−printoutthecntraisenotice′∗∗∗∗end∗∗loop;∗∗∗∗end∗∗;**declare cnt int = 0; **begin loop -- increment of cnt cnt = cnt + 1; -- exit the loop if cnt > 10 exit **when cnt > 10; -- skip the iteration if cnt is an odd number continue **when cnt = 6; -- print out the cnt raise notice '%', cnt; **end loop; **end;declarecntint=0;beginloopincrementofcntcnt=cnt+1;exittheloopifcnt>10exitwhencnt>10;skiptheiterationifcntisanoddnumbercontinuewhencnt=6;printoutthecntraisenoticeendloop;end;;

**Output:

PostgreSQL CONTINUE Statement Example

**Explanation: In the above example, we use the continue statement to skip the iteration when the value of the '**cnt' variable reaches 6.

Important Points About PostgreSQL CONTINUE Statement