Session 9: Scilab Programming Language
Objectives
In this session you will learn the following:
- Program control structures for looping and branching
- Boolean operators, expressions and statements
Introduction
During our interaction with Scilab, we have been using the Scilab programming language even though we have been thinking we are using Scilab commands and not the programming language. That is because, we have not interactively used any program flow control structures such as if(), for etc. Nevertheless, we already know a little bit about Scilab programming language, such as, creating variables, writing expressions and statements, calling functions. Th eadditional things we need to learn are:
- Program flow control structures.
- Boolean operations, expressions and statements.
- Writing our own functions and using them within Scilab
- File input and output
The best thing about an interactive programming environment such as Scilab is that even program control statements can be tried interactively and results observed immediately without having to go through the steps of compiling, linking and executting programs to understand how these work in a programming language.
Program Flow Control - Loops
The for Loop
Let us display the numbers from 1 to 5 using the for loop:
-->for i = 1:5
-->disp(i)
-->end
1.
2.
3.
4.
5.
Let us display all odd numbers between 1 and 5:
-->for i = 1:2:5
-->disp(i)
-->end
1.
3.
5.
The values of the loop index variable i are the values of the range used to define loop. Hence the increment can be a fraction, or it can be negative.
The while Loop
Let us calculate the machine epsilon using the while loop:
-->e = 1; k = 0;
-->while 1+(e/2) ~= 1 do
-->e = e / 2; k = k + 1;
-->end
-->e, k
e =
2.200D-16
k =
52.
Starting from e = 1, it takes 52 iterations to arrive at the machine epsilon, namely, 2.200D-16.
The do in the while loop after the expression 1+(e/2) ~= 1 can be replaced with then, or alternately, it can also be completely dropped.
Program Flow - Branching and Decision Making
Branching using if-then-elseif-else
The following code segment shows how to use the if-then-elseif-else control structure:
-->x = int(rand(1) * 100 - 50);
-->if x > 0 then
-->disp('Positive')
-->elseif x == 0 then
-->disp('Zero')
-->else
-->disp('Negative')
-->end
Output of the above statements is one of Positive, Zero or Negative depending on the value of x. You will also notice that when you type the above statements interactively, the result will be displayed even before the closing end is typed. This is due to the short circuiting of boolean expression evaluation - when one of the conditions being tested is met, there is no need to evaluate the remaining tests.
Branching using select
Branching using select is similar to the switch statement in C/C++. It can compare the value of a variable to a constant and and if found to be equal, can execute a set of statements.
-->x = int(rand(1) * 3;
-->select n
-->case 0 then
-->disp('Zero')
-->case 1 then
-->disp('One')
-->case 2 then
-->disp('Two')
-->else
-->disp('Too big')
-->end
Here too boolean short circuiting occurs and as soon as one of the expressions turns out to be true, the rest are not tested.
Boolean Operations, Expressions and Statements
The list of boolean operators is given below:
== | Equal to |
~= |
Not equal to |
> | Greater than | >= | Greater than or equal to |
< | Less than | <= | Less than or equal to |
~ | Negation | <> | Same as ~= |
& | Elementwise AND | | | Elementwise OR |
Boolean constants are %f or %F representing FALSE and %t and %T representing TRUE. When these operators are applied to a matrix, the result is a matrix having the same size as the given matrix with elements either T or F depending on what the result would be if the operator were applied to that element instead of to the whole matrix.
-->a = int(rand(3,4)*20; a > 10, bool2s(a > 10)
returns a 3x4 matrix of boolean values. Elements of the result corresponding to an element in the given matrix a will be TRUE if that element is greater than 10, otherwise FALSE. Function bool2s(a > 10) is similar to a > 10, except that it returns a matrix of integers 0 or 1 instead of returning a matrix of TRUE or FALSE values.
Function find(boolean_expr) returns a vector containing indices of those elements of a given matrix that satisfy the specified logical condition. Using these indices, it is possible to print the elements at these locations.
-->a = int(rand(3, 4)*100);
-->x = find(a < 20);
-->a(x)
Note that x is a vector, even if a is a matrix. Therefore a(x) is also a vector. Indices in x treat the matrix a as a vector with the elements indexed column-wise.
A compound logical statement can be constructed as follows:
-->x = find(a < 20 | a > 70); a(x) // logical OR
-->y = find(a > 20 & a < 70); a(y) // logical AND
Function and() returns the logical AND of the elements of a matrix. Function or() returns the logical OR of a matrix.
-->and(a > 10) // TRUE if all elements of a are greater than 10
-->or(a > 10) // TRUE if at least one element of a is greater than 10