<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>.
</para>
<para>
<literal>CONTINUE</literal> can be used with all types of loops; it
is not limited to use with unconditional loops.
</para>
<para>
Examples:
<programlisting>
LOOP
-- some computations
EXIT WHEN count > 100;
CONTINUE WHEN count < 50;
-- some computations for count IN [50 .. 100]
END LOOP;
</programlisting>
</para>
</sect3>
<sect3 id="plpgsql-control-structures-loops-while">
<title><literal>WHILE</literal></title>
<indexterm>
<primary>WHILE</primary>
<secondary>in PL/pgSQL</secondary>
</indexterm>
<synopsis>
<optional> <<<replaceable>label</replaceable>>> </optional>
WHILE <replaceable>boolean-expression</replaceable> LOOP
<replaceable>statements</replaceable>
END LOOP <optional> <replaceable>label</replaceable> </optional>;
</synopsis>
<para>
The <literal>WHILE</literal> statement repeats a
sequence of statements so long as the
<replaceable>boolean-expression</replaceable>
evaluates to true. The expression is checked just before
each entry to the loop body.
</para>
<para>
For example:
<programlisting>
WHILE amount_owed > 0 AND gift_certificate_balance > 0 LOOP
-- some computations here
END LOOP;
WHILE NOT done LOOP
-- some computations here
END LOOP;
</programlisting>
</para>
</sect3>
<sect3 id="plpgsql-integer-for">
<title><literal>FOR</literal> (Integer Variant)</title>
<synopsis>
<optional> <<<replaceable>label</replaceable>>> </optional>
FOR <replaceable>name</replaceable> IN <optional> REVERSE </optional> <replaceable>expression</replaceable> .. <replaceable>expression</replaceable> <optional> BY <replaceable>expression</replaceable> </optional> LOOP
<replaceable>statements</replaceable>
END LOOP <optional> <replaceable>label</replaceable> </optional>;
</synopsis>
<para>
This form of <literal>FOR</literal> creates a loop that iterates over a range
of integer values. The variable
<replaceable>name</replaceable> is automatically defined as type
<type>integer</type> and exists only inside the loop (any existing
definition of the variable name is ignored within the loop).
The two expressions giving
the lower and upper bound of the range are evaluated once when entering
the loop. If the <literal>BY</literal> clause isn't specified the iteration
step is 1, otherwise it's the value specified in the <literal>BY</literal>
clause, which again is evaluated once on loop entry.
If <literal>REVERSE</literal> is specified then the step value is
subtracted, rather than added, after each iteration.
</para>
<para>
Some examples of integer <literal>FOR</literal> loops:
<programlisting>
FOR i IN 1..10 LOOP
-- i will take on the values 1,2,3,4,5,6,7,8,9,10