Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2025.0.0 - English


for

keyword entering a non-conditional loop

Syntax

for variable = expression, instruction ; .. ; instruction; end

for variable = expression
instruction
..
instruction
end

for variable = expression do
instruction,
..
instruction
end

Description

If expression is an array (homogeneous or not), variable is set to each of its columns (for a hypermatrix, as matrix(expression, size(expression,1), -1))), one by one.

A particular case uses the colon operator to create regularly spaced row vectors, and is similar to traditional for loop forms: for variable = n1:step:n2, ...,end

If expression is a list, variable takes as values the successive entries of the list.

According to the Code Conventions for the Scilab Programming Language it is recommended:

  • Start each statement on a new line.

  • Write no more than one simple statement per line.

  • Break compound statements over multiple lines.

For example, preferably use:

for i = 1:5
    disp(i);
end

rather than:

for i = 1:5, disp(i); end

Examples

Trivial iterations (over columns of a row vector):

for i = [2 4 5]
    factorial(i)
end

// By decreasing values:
for j = 4:-1:1
    j
end

Iterate on the columns of a matrix:

M = [1 2 ; 3 4 ; 5 6]'
for  c = M, c, end
-> 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.

Iterate on the columns of a cell array:

C = {%pi, "This is πι" ; %z^2, %t}
for  c = C, c, end
--> 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]

Loop on the elements of a list:

for l = list([1 2;3 4], (1+%z)^3, 'example', [%F %T]); l, end
--> 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

Common and nested loops:

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

See also

  • while — Opens a block of instructions iterated on a heading condition
  • end — end keyword
  • do — language keyword for loops

History

VersionDescription
6.0.0
  • The for expression can now be a vector of graphic handles. for is now protected: Assignments like for=1 are no longer possible.
  • The number of characters used to define the body of any conditional instruction (if, while, for, or select/case) is no more limited to 16k.
2023.0.0 The expression can be a hypermatrix. It is then processed by column as matrix(expression, size(expression,1), -1).
Report an issue
<< end Control flow halt >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Oct 24 11:13:05 CEST 2024