In my last introductory article to Scilab we have discussed very basic commands. We won’t discuss the commands further as we will discuss the required commands as we will come across them. In this article we will discuss the control statements of Scilab.
Control statements are very essential part of any language. Is Scilab a language? Yes, you can consider as a scripting language for your numerical tasks. So as a language it is also having control statements.
So we will begin with listing our type of control statements in Scilab :
- if then else
- select ? case statement
- for
- while
Lets begin our discussion with our first control statement. If else then statement is similar to any language on the functioning level. Syntax is what varies from language to language. So here is calling sequence for if then else :
if expr1 then statements
elseif exprN then statements
....
else statements
end
As you can observe the statement block is ended with end
keyword. In Scilab every control statement block is ended by end
keyword. First statement
if expr1 then statements
is starting of block. The if
statement evaluates a logical expression and executes a group of statements when the expression is true. The else
and elseif
statements are optional. elseif
provide alternate set of statements if expression accompanied by elseif
is true. else
provides alternate group of statements if no condition is true. It will be more clear by following example:
//if_then_else.ece
day = input("Enter the day number")
if (day == 1) then
printf("Day is Monday")
elseif (day == 2) then
printf("Day is Tuesday")
elseif (day == 3) then
printf("Day is Wednesday")
elseif (day == 4) then
printf("Day is Thursday")
elseif (day == 5) then
printf("Day is Friday")
elseif (day == 6) then
printf("Day is Saturday")
elseif (day == 7) then
printf("Day is Sunday")
else
printf("Wrong Day number")
end
As you can notice the program is a simple one based on converting week number in to days. First statement of the program takes input from the user in form of number and store it into variable named ?day?. Then it keeps comparing the value of variable from 1 through 7 and outputs respective day number. If you are wondering how to run this program, then scroll down to bottom of this post.
Now lets move on to select?case statement. The calling sequence for this is:
select expr,
case expr1 then instructions1,
case expr2 then instructions2,
...
case exprN then instructionsN,
[else instructions],
end
In above block of statement select
is keyword. Expression following select
is evaluated and matched with expressions following case keyword. If any of the expression is true the set of statements following that case will be executed. If none of case expressions match then instructions following else are executed, if it is included. Else no execution in block will take place. Following is the program for realizing above if_then_else program with select .. case statements:
//select_case.ece
day = input("Enter the day number")
select day
case 1 then
printf("Day is Monday")
case 2 then
printf("Day is Tuesday")
case 3 then
printf("Day is Wednesday")
case 4 then
printf("Day is Thursday")
case 5 then
printf("Day is Friday")
case 6 then
printf("Day is Saturday")
case 7 then
printf("Day is Sunday")
else
printf("Wrong Day number")
end
Now you would have observed that I used the variable in place of expression following the select
keyword. You can use either expression or variable name after select
keyword. Similar is the method for case
expression. Rest of the program is straightforward.
Now we will move towards for
loop control statement. Earlier two control statements are used for decision making. Now coming are looping statement. First we will discuss the for
loop. Its syntax is as follow :
//for_loop.sce
for i = 1:10
printf("Square of %d is %dn",i,i*i)
end
So comparing the example with the syntax we can find that i
is variable name. Expression is 1:10
which forms a row vector of values from 1 through 10. So the loop runs for 10 times. Each time it executes the printf
statement, which prints the given statement,each time changing the value of variable as supplied by the loop.
Now here is last but not least while
loop. Syntax for while
loop is as follow:
while expr ,instructions,...[,else instructions], end
In this looping statement, the expression following while
keyword is evaluated each time and if it is true the statements inside loop are executed. Again the task performed by for loop in above example is repeated by while
loop in below program.
//while_loop.sce
i=1;
while i<=10
printf("Square of %d is %dn",i,i*i)
i = i+1;
end
Variable i
is assigned a value and then it evaluated to be less than 10. Then same statement is executed to print number and its square. Each time value of i
is increased in loop. Finally when it becomes grater then 10 loop exits.
Note: The number of characters used to define the body of any conditional instruction (if, while, for or select/case) must be limited to 16k.
Running the programs: For running the above programs, you can create program files with any of the text editor and save them as filename.sce
. Or you can use editor in Scilab (SciNotes) and enter the program lines in that. Save the file and execute is by pressing F5
.
Note: Some of data in this article is taken from the Scilab help manual. You can access by pressing F1
or using help menu in your Scilab Window.