Next: 3.1.3 Conditionals Up: 3.1 Programming Tools Previous: 3.1.1 Comparison Operators

## 3.1.2 Loops

Two types of loops exist in Scilab: the for  loop and the while  loop. The for loop steps through a vector of indices performing each time the commands delimited by end.
```
--> x=1;for k=1:4,x=x*k,end
x         =

1.
x         =

2.
x         =

6.
x         =

24.
```
The for loop can iterate on any vector or matrix taking for values the elements of the vector or the columns of the matrix.
```
--> x=1;for k=[-1 3 0],x=x+k,end
x         =

0.
x         =

3.
x         =

3.
```
The for loop can also iterate on lists. The syntax is the same as for matrices. The index takes as values the entries of the list.

```-->l=list(1,[1,2;3,4],'str')

-->for k=l, disp(k),end

1.

!   1.    2. !
!   3.    4. !

str
```

The while loop repeatedly performs a sequence of commands until a condition is satisfied.

```--> x=1; while x<14,x=2*x,end
x         =

2.
x         =

4.
x         =

8.
x         =

16.
```

A for or while loop can be ended by the command break :

```-->a=0;for i=1:5:100,a=a+1;if i > 10  then  break,end; end

-->a
a  =

3.
```
In nested loops, break exits from the innermost loop.

```-->for k=1:3; for j=1:4; if k+j>4 then break;else disp(k);end;end;end

1.

1.

1.

2.

2.

3.
```

Next: 3.1.3 Conditionals Up: 3.1 Programming Tools Previous: 3.1.1 Comparison Operators
Scilab Group