You are on page 1of 4

Scilab Programming

– FOR loops
Building complex algorithms often requires repetitive actions. For example we need to evaluate the
function f(x) defined below, for different values of x:

f(x)=x2+(√x)
for x = 1, 2, 3, 4, 5

Without knowing that we can use conditional loops to evaluate the function, the only way to evaluate the function, for
every value of x, is to use Scilab console, define x = 1 then evaluate the function, define x = 2 and evaluate the function
and so on.

-->x=1;

-->f(x) = x^2 + sqrt(x)

f=

2.

-->x=2;

-->f(x) = x^2 + sqrt(x)

f=

2.

5.4142136

-->x=3;

PTKI Medan – Komputasi dan Logika Pemrograman- 2023


-->f(x) = x^2 + sqrt(x)

f=

2.

5.4142136

10.732051

-->x=4;

-->f(x) = x^2 + sqrt(x)

f=

2.

5.4142136

10.732051

18.

-->x=5;

-->f(x) = x^2 + sqrt(x)

f=

2.

5.4142136

10.732051

18.

27.236068

-->

In the example above, the function f is a vector and x is function argument and index.

PTKI Medan – Komputasi dan Logika Pemrograman- 2023


Evaluating function f in the Scilab console, for different values of x is not very efficient and it’s time consuming. A better
way to do it is to use a conditional loop. This means that we will evaluate an instruction repeatedly until a condition
is satisfied.

A common conditional loop is the for loop. It’s named conditional because it’s executed depending if a conditions is
true or false. If the condition is true we execute the instruction, if it’s false we stop executing it.
Simple FOR loops
The general syntax of the for loop in Scilab is the following:

The expression contains a set of Scilab operations which perform:


 variable initialisation
 variable comparison (condition)
 variable incrementation each time the instruction is executed
The instruction is the piece of code that is evaluated each time the control variable is incremented as long as the
condition within the expression is satisfied.

Let’s apply the for loop structure to our function (in a Scilab script):

for x=1:1:5

f(x) = x^2 + sqrt(x);

end

In our example x is the index which gets incremented and also the function argument. The first value which is assigned
to x is 1. We evaluate the function f for x=1. After, we increment x with 1 so it becomes 2. We evaluate again the
function for x=2. We do the same operations until x=5.
The results are stored inside a vector f. The first value of f, f(1), corresponds to x=1. The last value of f, f(5),
corresponds to x=5.
If we run this piece of script and display the result of the f function in the console we’ll get:-->f

f=

2.

5.4142136

10.732051

18.

27.236068

-->

PTKI Medan – Komputasi dan Logika Pemrograman- 2023


We have the same result as the previous example but with a more compact code.

Bear in mind that the variable x used in the example above has two roles: as index and argument. We’ll give some
other examples in which there is a distinction between the index and the function argument.
In the image below you can see a logical diagram of the for loop, the events that happen during the loop and the
corresponding Scilab instructions for each event.

Image: Scilab programming – FOR loop logic diagram


Another example where for loops are very handy is the following. Let’s suppose you need to create a
vector t_vec which contains time values, from 0.1 to 10 seconds, in increments of 0.1. If you define the vector by hand,
you’ll need to enter 100 values, something like:

-->t_vec = [0.1 0.2 0.3 0.4 0.5 0.6...

This in not efficient at all. By using a for loop the definition of the vector is:

for i=1:100

t_vec(i) = i/10;

end

Nested FOR loops


Nested for loops means a for loop inside another for loop. The general syntax for nested for loops is:

PTKI Medan – Komputasi dan Logika Pemrograman- 2023

You might also like