</synopsis>
<para>
<literal>LOOP</literal> defines an unconditional loop that is repeated
indefinitely until terminated by an <literal>EXIT</literal> or
<command>RETURN</command> statement. The optional
<replaceable>label</replaceable> can be used by <literal>EXIT</literal>
and <literal>CONTINUE</literal> statements within nested loops to
specify which loop those statements refer to.
</para>
</sect3>
<sect3 id="plpgsql-control-structures-loops-exit">
<title><literal>EXIT</literal></title>
<indexterm>
<primary>EXIT</primary>
<secondary>in PL/pgSQL</secondary>
</indexterm>
<synopsis>
EXIT <optional> <replaceable>label</replaceable> </optional> <optional> WHEN <replaceable>boolean-expression</replaceable> </optional>;
</synopsis>
<para>
If no <replaceable>label</replaceable> is given, the innermost
loop is terminated and the statement following <literal>END
LOOP</literal> is executed next. If <replaceable>label</replaceable>
is given, it must be the label of the current or some outer
level of nested loop or block. Then the named loop or block is
terminated and control continues with the statement after the
loop's/block's corresponding <literal>END</literal>.
</para>
<para>
If <literal>WHEN</literal> is specified, the loop exit occurs only if
<replaceable>boolean-expression</replaceable> is true. Otherwise, control passes
to the statement after <literal>EXIT</literal>.
</para>
<para>
<literal>EXIT</literal> can be used with all types of loops; it is
not limited to use with unconditional loops.
</para>
<para>
When used with a
<literal>BEGIN</literal> block, <literal>EXIT</literal> passes
control to the next statement after the end of the block.
Note that a label must be used for this purpose; an unlabeled
<literal>EXIT</literal> is never considered to match a
<literal>BEGIN</literal> block. (This is a change from
pre-8.4 releases of <productname>PostgreSQL</productname>, which
would allow an unlabeled <literal>EXIT</literal> to match
a <literal>BEGIN</literal> block.)
</para>
<para>
Examples:
<programlisting>
LOOP
-- some computations
IF count > 0 THEN
EXIT; -- exit loop
END IF;
END LOOP;
LOOP
-- some computations
EXIT WHEN count > 0; -- same result as previous example
END LOOP;
<<ablock>>
BEGIN
-- some computations
IF stocks > 100000 THEN
EXIT ablock; -- causes exit from the BEGIN block
END IF;
-- computations here will be skipped when stocks > 100000
END;
</programlisting>
</para>
</sect3>
<sect3 id="plpgsql-control-structures-loops-continue">
<title><literal>CONTINUE</literal></title>
<indexterm>
<primary>CONTINUE</primary>
<secondary>in PL/pgSQL</secondary>
</indexterm>
<synopsis>
CONTINUE <optional> <replaceable>label</replaceable> </optional> <optional> WHEN <replaceable>boolean-expression</replaceable> </optional>;
</synopsis>
<para>
If no <replaceable>label</replaceable> is given, the next iteration of
the innermost loop is begun. That is, all statements remaining
in the loop body are skipped, and control returns
to the loop control expression (if any) to determine whether
another loop iteration is needed.
If <replaceable>label</replaceable> is present, it
specifies the label of the loop whose execution will be
continued.
</para>
<para>
If <literal>WHEN</literal> is specified, the next iteration of the
loop is begun only if <replaceable>boolean-expression</replaceable> is
true. Otherwise, control passes to the statement after
<literal>CONTINUE</literal>.