<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( https://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2008 - INRIA
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 -->
<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
          xmlns:svg="http://www.w3.org/2000/svg" xmlns:mml="http://www.w3.org/1998/Math/MathML"
          xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org"
          xml:lang="en" xml:id="if">
    <refnamediv>
        <refname>if</refname>
        <refpurpose>keyword for conditional execution</refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>
            if expr1 then
                statements
            elseif expri then
                statements
                ....
            else
                statements
            end
        </synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>expr1, expri</term>
                <listitem>
                    <para>
                        logical expressions
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>statements</term>
                <listitem>
                    <para>
                        blocks of valid statements.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        <para>
            The <literal>if</literal> statement evaluates a logical expression and executes a group
            of statements when the expression is true.
        </para>
        <para>
            The  <literal>expri</literal> are expressions with numeric or boolean
            values. If <literal>expri</literal> is a non-scalar value then the condition is true only if
            all entries of this value are true or different from zero.
        </para>
        <para>
            The optional <literal>elseif</literal> and <literal>else</literal> provide for the
            execution of alternate groups of statements. An <literal>end</literal> keyword,
            which matches the <literal>if</literal>, terminates the last group of
            statements. The line structure of <literal>if</literal> is not significant, the
            only constraint is that each <literal>then</literal> keyword must be on the same
            line as its corresponding <literal>if</literal> or <literal>elseif</literal> keyword.
        </para>
        <para>
            The keyword <literal>then</literal> can be replaced by a carriage return or a
            comma.
        </para>
        <para>
            According to the Code Conventions for the Scilab Programming Language it is recommended:
        </para>
        <itemizedlist>
            <listitem>
                <para>
                    Start each statement on a new line.
                </para>
            </listitem>
            <listitem>
                <para>
                    Write no more than one simple statement per line.
                </para>
            </listitem>
            <listitem>
                <para>
                    Break compound statements over multiple lines.
                </para>
            </listitem>
        </itemizedlist>
        <para>
            For example, use:
        </para>
        <programlisting role=""><![CDATA[
if rand(1,1) > 0.5 then
    disp("True");
else
    disp("False");
end
]]></programlisting>
        <para>
            rather than
        </para>
        <programlisting role=""><![CDATA[
      if rand(1,1) > 0.5 then disp("True"); else disp("False"); end
]]></programlisting>
    </refsection>
    <refsection>
        <title>Examples</title>
        <programlisting role="example"><![CDATA[
i=2
for j = 1:3,
  if i == j then
    a(i,j) = 2;
  elseif abs(i-j) == 1 then
    a(i,j) = -1;
  else a(i,j) = 0;
  end,
end
 ]]></programlisting>
    </refsection>
    <refsection role="see also">
        <title>See also</title>
        <simplelist type="inline">
            <member>
                <link linkend="then">then</link>
            </member>
            <member>
                <link linkend="else">else</link>
            </member>
            <member>
                <link linkend="elseif">elseif</link>
            </member>
            <member>
                <link linkend="end">end</link>
            </member>
            <member>
                <link linkend="while">while</link>
            </member>
            <member>
                <link linkend="select">select</link>
            </member>
            <member>
                <link linkend="boolean">boolean</link>
            </member>
            <member>
                <link linkend="comparison">comparison</link>
            </member>
            <member>
                <link linkend="or_op">|, ||</link>
            </member>
            <member>
                <link linkend="and_op">&amp;, &amp;&amp;</link>
            </member>
        </simplelist>
    </refsection>
    <refsection role="history">
        <title>History</title>
        <revhistory>
            <revision>
                <revnumber>6.0.0</revnumber>
                <revdescription>
                    <itemizedlist>
                        <listitem>
                            <literal>if</literal> is now protected:
                            Assignments like <literal>if=1</literal> are no longer possible.
                        </listitem>
                        <listitem>
                            The number of characters used to define the body of any conditional instruction (<literal>if</literal>, <literal>while</literal>, <literal>for</literal>, or <literal>select/case</literal>) is no more limited to 16k.
                        </listitem>
                    </itemizedlist>
                </revdescription>
            </revision>
        </revhistory>
    </refsection>
</refentry>
