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

Change language to:
Français - 日本語 - Português - Русский

Please note that the recommended version of Scilab is 2024.1.0. This page might be outdated.
See the recommended documentation of this function

Scilab Help >> Scilab > Control flow > for


language keyword for loops


Used to define loops. Its syntax is: for variable=expression ,instruction, .. ,instruction,end

for variable=expression do instruction, ,instruction,end

If expression is a matrix or a row vector, variable takes as values the values of each column of the matrix.

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, use:

for i = 1:5 

rather than

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

The number of characters used to define the body of any conditional instruction (if while for or select/case) must be limited to 16k.


// "traditional" for loops
for i = 1:n
    for j = 1:n
        a(i,j) = 1/(i+j-1);
for j = 2:n-1
    a(j,j) = j; 
for j= 4:-1:1
end // decreasing loop

//loop on matrix columns
for  e=eye(3,3), e, end  
for v=a, write(6,v), end
for j=1:n, v=a(:,j), write(6,v), end 

//loop on list entries
for l=list(1,2,'example'); l, end

See Also

  • while — while keyword
  • end — end keyword
  • do — language keyword for loops
Report an issue
<< do Control flow if >>

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:
Wed Apr 01 10:13:49 CEST 2015