<listitem>
<para><literal>IF ... THEN ... ELSIF ... THEN ... ELSE ... END IF</literal></para>
</listitem>
</itemizedlist>
and two forms of <command>CASE</command>:
<itemizedlist>
<listitem>
<para><literal>CASE ... WHEN ... THEN ... ELSE ... END CASE</literal></para>
</listitem>
<listitem>
<para><literal>CASE WHEN ... THEN ... ELSE ... END CASE</literal></para>
</listitem>
</itemizedlist>
</para>
<sect3 id="plpgsql-conditionals-if-then">
<title><literal>IF-THEN</literal></title>
<synopsis>
IF <replaceable>boolean-expression</replaceable> THEN
<replaceable>statements</replaceable>
END IF;
</synopsis>
<para>
<literal>IF-THEN</literal> statements are the simplest form of
<literal>IF</literal>. The statements between
<literal>THEN</literal> and <literal>END IF</literal> will be
executed if the condition is true. Otherwise, they are
skipped.
</para>
<para>
Example:
<programlisting>
IF v_user_id <> 0 THEN
UPDATE users SET email = v_email WHERE user_id = v_user_id;
END IF;
</programlisting>
</para>
</sect3>
<sect3 id="plpgsql-conditionals-if-then-else">
<title><literal>IF-THEN-ELSE</literal></title>
<synopsis>
IF <replaceable>boolean-expression</replaceable> THEN
<replaceable>statements</replaceable>
ELSE
<replaceable>statements</replaceable>
END IF;
</synopsis>
<para>
<literal>IF-THEN-ELSE</literal> statements add to
<literal>IF-THEN</literal> by letting you specify an
alternative set of statements that should be executed if the
condition is not true. (Note this includes the case where the
condition evaluates to NULL.)
</para>
<para>
Examples:
<programlisting>
IF parentid IS NULL OR parentid = ''
THEN
RETURN fullname;
ELSE
RETURN hp_true_filename(parentid) || '/' || fullname;
END IF;
</programlisting>
<programlisting>
IF v_count > 0 THEN
INSERT INTO users_count (count) VALUES (v_count);
RETURN 't';
ELSE
RETURN 'f';
END IF;
</programlisting>
</para>
</sect3>
<sect3 id="plpgsql-conditionals-if-then-elsif">
<title><literal>IF-THEN-ELSIF</literal></title>
<synopsis>
IF <replaceable>boolean-expression</replaceable> THEN
<replaceable>statements</replaceable>
<optional> ELSIF <replaceable>boolean-expression</replaceable> THEN
<replaceable>statements</replaceable>
<optional> ELSIF <replaceable>boolean-expression</replaceable> THEN
<replaceable>statements</replaceable>
...
</optional>
</optional>
<optional> ELSE
<replaceable>statements</replaceable> </optional>
END IF;
</synopsis>
<para>
Sometimes there are more than just two alternatives.
<literal>IF-THEN-ELSIF</literal> provides a convenient
method of checking several alternatives in turn.
The <literal>IF</literal> conditions are tested successively
until the first one that is true is found. Then the
associated statement(s) are executed, after which control
passes to the next statement after <literal>END IF</literal>.
(Any subsequent <literal>IF</literal> conditions are <emphasis>not</emphasis>
tested.) If none of the <literal>IF</literal> conditions is true,
then the <literal>ELSE</literal> block (if any) is executed.
</para>
<para>
Here is an example:
<programlisting>
IF number = 0 THEN
result := 'zero';
ELSIF number > 0 THEN
result := 'positive';
ELSIF number < 0 THEN
result := 'negative';
ELSE
-- hmm, the only other possibility is that number is null
result := 'NULL';
END IF;
</programlisting>
</para>
<para>
The key word <literal>ELSIF</literal> can also be spelled
<literal>ELSEIF</literal>.
</para>
<para>
An alternative way of accomplishing the same task is to nest