<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( https://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) - INRIA
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 * Copyright (C) 2018, 2023 - Samuel GOUGEON
 *
 * 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:ns5="http://www.w3.org/1999/xhtml"
        xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook"
        xmlns:scilab="http://www.scilab.org" xml:id="for" xml:lang="pt">
    <refnamediv>
        <refname>for</refname>
        <refpurpose>palavra-chave de linguagem para laços ("loops") (significa "para...")
        </refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Seqüência de Chamamento</title>
        <synopsis>
            for variable = expression, instruction ; .. ; instruction; end

            for variable = expression
                instruction
                ..
                instruction
            end

            for variable = expression do
                instruction,
                ..
                instruction
            end
        </synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Descrição</title>
        <para>
            Se <literal>expression</literal> for um array (homogêneo ou não), <literal>variable</literal>
            é definido para cada uma de suas colunas (para uma hipermatriz, como em
            <literal>matrix(expression, size(expression ,1), -1))</literal>), um por um.
        </para>
        <para>
            Um caso particular utiliza o operador <link linkend="colon">colon</link> para criar
            vetores linhas regularmente espaçados, e remonta a formas de laço "for" tradicionais : 
            <literal>for variable = n1:step:n2, ..., end</literal>
        </para>
        <para>
            Se <literal>expression</literal> é uma lista, <literal>variable</literal> toma como valores
            as entradas sucessivas da lista.
        </para>
        <warning>
            o número de caracteres usados para definir o corpo de qualquer instrução condicional
            (if, while, for ou select/case) deve ser limitado a 16 kB.
        </warning>
    </refsection>
    <refsection>
        <title>Exemplos</title>
        <para>Iterações triviais (sobre colunas de um vetor linha):</para>
        <programlisting role="example"><![CDATA[
for i = [2 4 5]
    factorial(i)
end

// Iterações sobre valores decrescentes:
for j = 4:-1:1
    j
end
 ]]></programlisting>
        <para>Laço "decrescente" :</para>
        <programlisting role="example"><![CDATA[
for j = 4:-1:1
    j
end
 ]]></programlisting>
        <para>Laço implícito sobre as colunas de uma matriz :</para>
        <programlisting role="example"><![CDATA[
M = [1 2 ; 3 4 ; 5 6]'
for  c = M, c, end
 ]]></programlisting>
    <screen><![CDATA[
-> M = [1 2 ; 3 4 ; 5 6]'
 M  =
   1.   3.   5.
   2.   4.   6.

--> for  c = M, c, end
 c  =
   1.
   2.

 c  =
   3.
   4.

 c  =
   5.
   6.
]]></screen>
        <para>Iterate on the columns of a cells array:</para>
        <programlisting role="example"><![CDATA[
C = {%pi, "This is πι" ; %z^2, %t}
for  c = C, c, end
 ]]></programlisting>
    <screen><![CDATA[
--> C = {%pi, "This is πι" ; %z^2, %t}
 C  =
  [1x1 constant  ]  [1x1 string ]
  [1x1 polynomial]  [1x1 boolean]


--> for  c = C, c, end
 c  =
  [1x1 constant  ]
  [1x1 polynomial]

 c  =
  [1x1 string ]
  [1x1 boolean]
]]></screen>
        <para>Laço em listas de entradas :</para>
        <programlisting role="example"><![CDATA[
for l = list([1 2;3 4], (1+%z)^3, 'example', [%F %T]); l, end
 ]]></programlisting>
    <screen><![CDATA[
--> for l = list([1 2;3 4], (1+%z)^3, 'example', [%F %T]); l, end
 l  =
   1.   2.
   3.   4.

 l  =
            2   3
   1 +3z +3z  +z

 l  =
 example

 l  =
  F T
]]></screen>

        <para>Lacetes iterativos comum :</para>
        <programlisting role="example"><![CDATA[
// laços "for" tradicionais
n=5;
for i = 1:n
    for j = 1:n
        a(i,j) = 1/(i+j-1);
    end;
end
for j = 2:n-1
    a(j,j) = j;
end;
a

for v = a, write(%io(2),v), end
for j = 1:n, v = a(:,j), write(%io(2),v), end
]]></programlisting>
    </refsection>
    <refsection role="see also">
        <title>Ver Também</title>
        <simplelist type="inline">
            <member>
                <link linkend="while">while</link>
            </member>
            <member>
                <link linkend="end">end</link>
            </member>
            <member>
                <link linkend="do">do</link>
            </member>
        </simplelist>
    </refsection>
    <refsection role="history">
        <title>Histórico</title>
        <revhistory>
            <revision>
                <revnumber>6.0.0</revnumber>
                <revdescription>
                    The for <literal>expression</literal> can now be a vector of graphic handles.
                    <literal>for</literal> is now protected:
                    Assignments like <literal>for=1</literal> are no longer possible.
                </revdescription>
            </revision>
            <revision>
                <revnumber>2023.0.0</revnumber>
                <revdescription>
                    The <literal>expression</literal> can be a hypermatrix. It is then processed
                    by column as <literal>matrix(expression, size(expression,1), -1)</literal>.
                </revdescription>
            </revision>
        </revhistory>
    </refsection>
</refentry>
