Scilab

Chapter 5. Control instructions  if…then…else , while… ,for… 

Chapter 5.1 Introduction

We will discuss control instructions without which Scilab, like any other language, would be pointless.

Namely:
– “If…then…else…elseif…”
– “while…”
– “for…”

Chapter 5.1 “If…then…else” control statement
In the dots… there are the so-called logical conditions e.g. x>5 or more extensive as x>5 & x<10
So far, instructions were executed one by one. Otherwise, one instruction was always followed by another. It’s as if the train from Warsaw to Szczecin ran on only one track! It is known that he will always reach Szczecin eventually. And yet there are railroad switches on the actual route. When the railroad switch is set to Szczecin, we will reach Szczecin, when it is set to Kraków, we will not reach Szczecin.
Just like in this program
P5-1 program

clear;clc;
zw="Szczecin"
if zw=="Szczecin" then 
    disp("I’m going to Szczecin")
else
    disp("I’m not going to Szczecin")

Program analysis
Instruction
if (logical condition is fullfilled) then (execute the statement) else (execute the instruction)
In our program, the variable “railroad switch” zw has the text value “Szczecin”. Therefore, we expect that the text “I’m going to Szczecin” will be displayed. By the way, note that you do not have to declare the type of the variable as in other languages, e.g. C++. The Scilab language is smart enough to recognize this on its own.
After executing the program there will be something like this.
“I’m going to Szczecin!”
And after changing the instruction zw=”Szczecin” to zw=”Kraków”.
“I’m not going to Szczecin!”
I guess everything’s clear.
Note:
The program is an opportunity to distinguish 2 very similar expressions:
zw=”Szczecin” // Instructions for entering the text “Szczecin” into the zw variable
zw==”Szczecin”// An expression that has a logical value
-T (True) when the text “Szczecin” is included
-F (False) when the other text is included, e.g. “Kraków”
A beginner programmer can write
if zw=”Szczecin” then..
And then he is surprised that the program does not work. Guess why?

Instruction
if (logical condition) then (instruction) else (instruction)
may be available in a more advanced version, i.e.
– Instead of one instruction after if or else, there may be several of them
– A logical condition may be a more complex logical function
That is
if (logical condition is met) then //execute the next instructions
instruction 1
instruction 2
….
instruction n
else //execute other subsequent statements

instruction 1
instruction 2
….
instruction k
end

An example with a more extensive logical function and several statements after then,  else.
Here the logical function will be the sum x>5 | x<2
Note:
The symbol “| “ is the “or” 
P5-2 program

clear;clc; 
x=7
if x>5|x<2 then 
    x=x+1
    disp("addition")
else 
    x=x-1
    disp("subtraction")
end
disp("x=",x)

After executing the program, a text was created
“addition”
“x=”
8.
Change the instruction x=7 to x=4
The effect will be
“subtraction”
“x=”
3.
Note:
When the condition after if… is not met, the instruction (or instructions after else… is executed. What if after if… we want to check further? Then after if… we give elseif. Such instructions were used in Chapter 5.4.3.

Chapter 5.2 The while… control instruction
In the example it is i<5. It is  a loop instruction that will be executed 3 times. Before doing it, do it “dry”. Like any programmer, imagine you are Mr. Computer.
P5-3 program

clear;clc;
i=2
while i<5
    disp(i)
    i=i+1
end

After executing the program, a text was created
2.
3.
4.
Note that for i=5 the condition i<5 is not met. This means that the program will only execute 3, not 4, loops.
Play around a bit, e.g. i=2 change for i=0 and while i<5 change for while <10 etc.
Maybe it’s a more interesting program. E.g. calculating n! (n factorial) We know that, for example, 3!=1*2*3=6
P5-4 program

clear;clc;
i=0
s=1
while i<3 
     s=s*(i+1)
     disp('s=',s)
     i=i+1
end
disp('The result is ',s)

The result is .

s=”
1.
“s=”
2.
“s=”
6.
“The result is“
6.
It’s ok.

Chapter 5.3 Control instruction for…
Also a loop instruction, but the number of loops can be determined without executing the program!
Here the condition is x==2. That is, when x=2. By the way, remember what is the difference between the expressions x==2 and x=2
Program P5-4

clear;clc;
for x=0:0.5:2
    x=x^2
   disp(x)
end

Description of the program
for x=0:0.5:2
The function f(x) will be calculated, here f(x)=x² for x=0, 0.5, 1, 1.5, 2
So for each x from 0 to 2 with an interval of 0.5
0.
0.25
1.
2.25
4.
Note that the number of loops 5 could be determined from the for x=0:0.5:2 instruction itself.
Analysis of the program itself was not necessary for this.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top