> This page location: PostgreSQL PL/pgSQL > EXIT Statement
> Full Neon documentation index: https://neon.com/docs/llms.txt

# PL/pgSQL Exit Statement

**Summary**: in this tutorial, you will learn about the PL/pgSQL `exit` statement and how to use it to terminate a loop or exit a block.

## Introduction to the PL/pgSQL exit statement

The `exit` statement allows you to prematurely terminate a loop including an unconditional [loop](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-loop-statements), a [while loop](https://neon.com/postgresql/postgresql-plpgsql/pl-pgsql-while-loop), and a [for loop](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-for-loop).

The following shows the syntax of the `exit` statement:

```plsql
exit [label] [when boolean_expression]
```

In this syntax:

- The `label` is the loop label of the current loop where the `exit` is in or the loop label of the outer loop. Depending on the label, the `exit` statement will terminate the corresponding loop. If you don't use the label, the `exit` statement will terminate the enclosing loop.
- Use the `when boolean_expression` clause to specify a condition that terminates a loop. The `exit` statement will terminate the loop if the `boolean_expression` evaluates to `true`.

The following statements are equivalent:

```plsql
exit when counter > 10;
```

```plsql
if counter > 10 then
   exit;
end if;
```

The `exit when` is cleaner and shorter.

Besides terminating a loop, you can use the `exit` statement to exit a block specified by the `begin...end` keywords.

In this case, the control is passed to the statement after the `end` keyword of the current block:

```plsql
<<block_label>>
BEGIN
    -- some code
    EXIT [block_label] [WHEN condition];
    -- some more code
END block_label;
```

## PL/pgSQL Exit statement examples

Let's take some examples of using the PL/pgSQL `exit` statement.

### 1) Using PL/pgSQL Exit statement to terminate an unconditional loop

The following example illustrates how to use the `exit` statement in unconditional loops:

```plsql
do
$$
declare
   i int = 0;
   j int = 0;
begin
  <<outer_loop>>
  loop
     i = i + 1;
     exit when i > 3;
	 -- inner loop
	 j = 0;
     <<inner_loop>>
     loop
		j = j + 1;
		exit when j > 3;
		raise notice '(i,j): (%,%)', i, j;
	 end loop inner_loop;
  end loop outer_loop;
end;
$$
```

Output:

```
NOTICE:  (i,j): (1,1)
NOTICE:  (i,j): (1,2)
NOTICE:  (i,j): (1,3)
NOTICE:  (i,j): (2,1)
NOTICE:  (i,j): (2,2)
NOTICE:  (i,j): (2,3)
NOTICE:  (i,j): (3,1)
NOTICE:  (i,j): (3,2)
NOTICE:  (i,j): (3,3)
```

How it works.

This example contains two loops: outer and inner loops.

Since both `exit` statements don't use any loop labels, they will terminate the current loop.

The first `exit` statement terminates the outer loop when `i` is greater than `3`. That's why you see the value of `i` in the output is `1`, `2`, and `3`.

The second `exit` statement terminates the inner loop when `j` is greater than `3`. It is the reason you see that `j` is `1`, `2`, and `3` for each iteration of the outer loop.

The following example places the label of the outer loop in the second `exit` statement:

```plsql
do
$$
declare
   i int = 0;
   j int = 0;
begin
  <<outer_loop>>
  loop
     i = i + 1;
     exit when i > 3;
	 -- inner loop
	 j = 0;
     <<inner_loop>>
     loop
		j = j + 1;
		exit outer_loop when j > 3;
		raise notice '(i,j): (%,%)', i, j;
	 end loop inner_loop;
  end loop outer_loop;
end;
$$
```

Output:

```
NOTICE:  (i,j): (1,1)
NOTICE:  (i,j): (1,2)
NOTICE:  (i,j): (1,3)
```

In this example, the second `exit` statement terminates the outer loop when `j` is greater than 3.

### 2) Using the PL/pgSQL Exit statement to exit a block

The following example illustrates how to use the `exit` statement to terminate a block:

```plsql
do
$$
begin

  <<simple_block>>
   begin
  	 exit simple_block;
         -- for demo purposes
	 raise notice '%', 'unreachable!';
   end;
   raise notice '%', 'End of block';
end;
$$
```

Output

```
NOTICE:  End of block
```

In this example, the exit statement terminates the `simple_block` immediately:

```plsql
exit simple_block;
```

This statement will never be reached:

```plsql
raise notice '%', 'unreachable!';
```

## Summary

- Use the `exit` statement to terminate a loop including an unconditional `loop`, `while`, and `for` loop.
- Use the `exit` statement to exit a block.

---

## Related docs (PostgreSQL PL/pgSQL)

- [Introduction](https://neon.com/postgresql/postgresql-plpgsql/introduction-to-postgresql-stored-procedures)
- [Block Structure](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-block-structure)
- [Variables](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-variables)
- [Constants](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-constants)
- [IF-ELSE Statements](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-if-else-statements)
- [CASE Statement](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-case-statement)
- [Loop Statements](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-loop-statements)
- [FOR Loop](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-for-loop)
- [WHILE Loop](https://neon.com/postgresql/postgresql-plpgsql/pl-pgsql-while-loop)
- [CONTINUE Statement](https://neon.com/postgresql/postgresql-plpgsql/pl-pgsql-continue)
- [Cursor](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-cursor)
- [Function Parameters](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-function-parameters)
- [Function Overloading](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-function-overloading)
- [Function Returns Table](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-function-returns-a-table)
- [Returns SETOF](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-returns-setof)
- [Record Types](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-record-types)
- [Row Types](https://neon.com/postgresql/postgresql-plpgsql/pl-pgsql-row-types)
- [Error Messages](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-errors-messages)
- [Exception Handling](https://neon.com/postgresql/postgresql-plpgsql/postgresql-exception)
- [CREATE FUNCTION](https://neon.com/postgresql/postgresql-plpgsql/postgresql-create-function)
- [CREATE PROCEDURE](https://neon.com/postgresql/postgresql-plpgsql/postgresql-create-procedure)
- [DROP FUNCTION](https://neon.com/postgresql/postgresql-plpgsql/postgresql-drop-function)
- [DROP PROCEDURE](https://neon.com/postgresql/postgresql-plpgsql/postgresql-drop-procedure)
- [Stored Procedure with INOUT Parameters](https://neon.com/postgresql/postgresql-plpgsql/postgresql-stored-procedure-with-inout-parameters)
- [Introduction to Stored Procedures](https://neon.com/postgresql/postgresql-plpgsql/introduction-to-postgresql-stored-procedures)
- [SELECT INTO](https://neon.com/postgresql/postgresql-plpgsql/pl-pgsql-select-into)
- [ASSERT Statement](https://neon.com/postgresql/postgresql-plpgsql/pl-pgsql-assert)
- [Dollar-Quoted String Constants](https://neon.com/postgresql/postgresql-plpgsql/dollar-quoted-string-constants)
