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:
See also
History
Version | Description |
6.0.0 |
|
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 >> |