A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/postgresql/postgresql-continue/ below:

PostgreSQL - Continue - GeeksforGeeks

PostgreSQL - Continue

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 PostgreSQL from 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.

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
  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;
$$;

Output:

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
  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;
$$;

Output:

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4