Project: YugabyteDB (App Definition and Development / Database)
File: exit-from-block-statememt.md

title: Using the "exit" statement to jump out of a block statement [YSQL] headerTitle: Using the "exit" statement to jump out of a block statement linkTitle: > Jumping out of a block statement with "exit" description: Describes how to use the "exit" statement, not in a "loop" statement, to jump out of a block statement. [YSQL]. menu: stable: identifier: exit-from-block-statememt parent: loop-exit-continue weight: 50 type: docs showRightNav: true 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 toplevelblock 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 \<label\> statement can be used outside a loop. When the continue \<label\> 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 \<label\> statement, where the label matches that of an enclosing loop statement, in the function s.vowelsfromlines() on the page Two case studies: Using the "for" loop, the "foreach" loop, the "infinite" loop, the "exit" statement, and the "continue" statement in . Look for this: ```plpgsql continue lines when c = prev; ```