<literal>CASE_NOT_FOUND</literal> exception is raised.
</para>
<para>
Here is a simple example:
<programlisting>
CASE x
WHEN 1, 2 THEN
msg := 'one or two';
ELSE
msg := 'other value than one or two';
END CASE;
</programlisting>
</para>
</sect3>
<sect3 id="plpgsql-conditionals-searched-case">
<title>Searched <literal>CASE</literal></title>
<synopsis>
CASE
WHEN <replaceable>boolean-expression</replaceable> THEN
<replaceable>statements</replaceable>
<optional> WHEN <replaceable>boolean-expression</replaceable> THEN
<replaceable>statements</replaceable>
... </optional>
<optional> ELSE
<replaceable>statements</replaceable> </optional>
END CASE;
</synopsis>
<para>
The searched form of <command>CASE</command> provides conditional execution
based on truth of Boolean expressions. Each <literal>WHEN</literal> clause's
<replaceable>boolean-expression</replaceable> is evaluated in turn,
until one is found that yields <literal>true</literal>. Then the
corresponding <replaceable>statements</replaceable> are executed, and
then control passes to the next statement after <literal>END CASE</literal>.
(Subsequent <literal>WHEN</literal> expressions are not evaluated.)
If no true result is found, the <literal>ELSE</literal>
<replaceable>statements</replaceable> are executed;
but if <literal>ELSE</literal> is not present, then a
<literal>CASE_NOT_FOUND</literal> exception is raised.
</para>
<para>
Here is an example:
<programlisting>
CASE
WHEN x BETWEEN 0 AND 10 THEN
msg := 'value is between zero and ten';
WHEN x BETWEEN 11 AND 20 THEN
msg := 'value is between eleven and twenty';
END CASE;
</programlisting>
</para>
<para>
This form of <command>CASE</command> is entirely equivalent to
<literal>IF-THEN-ELSIF</literal>, except for the rule that reaching
an omitted <literal>ELSE</literal> clause results in an error rather
than doing nothing.
</para>
</sect3>
</sect2>
<sect2 id="plpgsql-control-structures-loops">
<title>Simple Loops</title>
<indexterm zone="plpgsql-control-structures-loops">
<primary>loop</primary>
<secondary>in PL/pgSQL</secondary>
</indexterm>
<para>
With the <literal>LOOP</literal>, <literal>EXIT</literal>,
<literal>CONTINUE</literal>, <literal>WHILE</literal>, <literal>FOR</literal>,
and <literal>FOREACH</literal> statements, you can arrange for your
<application>PL/pgSQL</application> function to repeat a series of commands.
</para>
<sect3 id="plpgsql-control-structures-loops-loop">
<title><literal>LOOP</literal></title>
<synopsis>
<optional> <<<replaceable>label</replaceable>>> </optional>
LOOP
<replaceable>statements</replaceable>
END LOOP <optional> <replaceable>label</replaceable> </optional>;
</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>