---
title: "Using the \"exit\" statement to jump out of a block statement"
url: "https://docs.yugabyte.com/stable/api/ysql/user-defined-subprograms-and-anon-blocks/language-plpgsql-subprograms/plpgsql-syntax-and-semantics/executable-section/compound-statements/loop-exit-continue/exit-from-block-statememt/"
---

# Using the "exit" statement to jump out of a block statement

Describes how to use the "exit" statement, not in a "loop" statement, to jump out of a block statement. [YSQL].

The *exit* statement is legal, and has well-defined semantics, within a simple block statement, even when it isn't inside a *loop* statement. This means that you can use it anywhere in the PL/pgSQL text that defines the implementation of a *do* statement or a *language plpgsql* subprogram. If you label the top-level block statement that defines the implementation with, for example, *top level block*, then use can use this:

```plpgsql
exit top_level_block when <boolean expresssion>:
```

as a more compact alternative to the bare *return* statement within an *if* statement:

```plpgsql
if <boolean expresssion> then
  return;
end if;
```

(The bare *return* statement is legal, and has well-defined semantics, in a *do* statement's implementation and in a *language plpgsql* procedure. The other forms of the *return* statement are legal only in a *language plpgsql* function.)

Try this:

```plpgsql
\c :db :u
drop schema if exists s cascade;
create schema s;

create function s.f(n in int)
  returns text
  language plpgsql
as $body$
declare
  v text not null := 'a';
begin
  <<b1>>begin
    exit b1 when length(v) >= n;
    v := v||'b';
    exit b1 when length(v) >= n;
    v := v||'c';
    exit b1 when length(v) >= n;
    v := v||'-impossible';
  end b1;
  return v;
end;
$body$;
```

This finishes without error. Now test it. First like this:

```plpgsql
select s.f(1);
```

This is the result:

```output
 a
```

Next like this:

```plpgsql
select s.f(2);
```

This is the result:

```output
 ab
```

And finally like this:

```plpgsql
select s.f(3);
```

This is the result:

```output
 abc
```

Now try this counter example:

```plpgsql
create procedure s.bad()
  language plpgsql
as $body$
begin
  exit;
end;
$body$;
```

It causes the *42601* syntax error:

```output
EXIT cannot be used outside a loop, unless it has a label
```

Finally try this counter example:

```plpgsql
create procedure s.bad()
  language plpgsql
as $body$
<<b1>>begin
  loop
    continue b1;
  end loop;
end b1;
$body$;
```

It, too, causes the *42601* syntax error, but now with this wording:

```output
block label "b1" cannot be used in CONTINUE
```

This is the meaning:

- Neither the *continue* statement (without a label) nor the *continue &lt;label&gt;* statement can be used outside a loop.
- When the *continue &lt;label&gt;* statement is used, as it must be, within a loop, the label must match that of an enclosing *loop* statement.

There's an example of the legal use of the *continue &lt;label&gt;* statement, where the label matches that of an enclosing *loop* statement, in the function *s.vowels\_from\_lines()* on the page «Two case studies: Using the "for" loop, the "foreach" loop, the "infinite" loop, the "exit" statement, and the "continue" statement» in [Case study #2](/stable/api/ysql/user-defined-subprograms-and-anon-blocks/language-plpgsql-subprograms/plpgsql-syntax-and-semantics/executable-section/compound-statements/loop-exit-continue/two-case-studies/#case-study-2-compose-a-string-of-a-specified-number-of-vowels-from-each-text-line-in-an-array-until-a-specified-number-of-such-vowel-strings-have-been-composed "Case study #2").

Look for this:

```plpgsql
continue lines when c = prev;
```
