Flow Control
SCILAB has the following flow control constructs,
if Statements |
|
select statements |
|
for loops |
|
while loops |
|
break statements |
if
The if
statement evaluates a
logical expression and executes a group of statements when the expression is true.
The optional elseif
and else
keywords
provide for the execution of alternate groups of statements.
An end
keyword,
which matches the if
, terminates the last group of statements. The optional elseif and else provide for the execution of alternate
groups of statements. The line structure given above is not significant, the only constraint is that each then keyword must be on the
same line line as its corresponding if or elseif keyword.
The general expression is,
if condition
// code
elseif condition
// code
else
end
For example,
if modulo(num,2) == 0
disp('The Number is Even');
elseif modulo(num,2) ~=0
disp('The Number is Odd');
else
disp('Invalid Number');
end
In this code there are some logical expressions like greater than, less than,etc..., these are used for the if conditions. The below is a table which gives a list of logical expressions
= = | Equal to |
~ = | Not equal to |
> = | Greater than or equal to |
< = | Less than or equal to |
> | Greater than |
< | Less than |
select
Theselect
statement executes
groups of statements based on the value of a variable or expression. The
keywords case
delineate the groups. Only the first matching
case is executed. There must always be an end
to match the select.
The general expression is,
select condition
case 1
// code
case N
// code
end
The above example can be written using select as follows,
select modulo(num,2)
case 0
disp('The Number is Even');
case 1
disp('The Number is Odd');
case 2
disp('Inavlid Number');
end
Note: The select instruction can be used instead of multiple if statements. This has definite advantage over the multiple if statements.
for
Thefor
loop repeats a group of
statements a fixed, predetermined number of times. A matching end
delineates the statements.
The general expression is,
for variable = n1:step:n2
// code ;
end
The semicolon terminating the inner statement suppresses printing of the result.
For example,
//Program to generate Bipolar signal +1 / -1
mat = rand(1,10,'normal');
binary =zeros(size(mat));
for count = 1:1:length(mat)
if mat(count) >= 0
binary(count) =1;
else
binary(count) =-1;
end
end
while
Thewhile
loop repeats a group
of statements an indefinite number of times under control of a logical
condition. A matching end
delineates the statements.
The general expression is,
while condition
// code
// loop counter i.e., count =count +1;
end
The above can be written using a while loop as ,
mat = rand(1,10,'normal');
binary =zeros(size(mat));
count = 1;
while( count <= length(mat))
if mat(count) >= 0
binary(count) =1;
else
binary(count) =-1;
end
count =count+1;
end
break
Thebreak
statement lets you
exit early from a for
or while
loop. In nested
loops, break
exits from the innermost loop only.
Let us take the previous example, if we want to exit the while loop when the value of count reaches 5. Using break statement we can achieve this.
mat = rand(1,10,'normal');
binary =zeros(size(mat));
count = 1;
while( count <= length(mat))
if mat(count) >= 0
binary(count) =1;
else
binary(count) =-1;
end
count =count+1;
// break condition
if count == 5
break;
end
end