The common Switch statement becomes the Select
statement in Scilab
The typical switch statement
(also known as the switch-case-otherwise
structure in other programming
for example Matlab) becomes the “select case”
statement in Scilab. This
function allows us to work with conditional statements.
It’s similar in function to the if-elseif-end
statement. In fact, in many cases you can use either an if statement or
case statement to perform the same task.
Switch Statement in Matlab
The Case and End Keywords
Just as the if statement
needs other keywords in order to do
its job properly, the select statement needs to be used with other
keywords, namely case
Here is the syntax for a
select case statement:
The keyword "then" can be
replaced by a carriage
return or a comma.
expr is the
expression to be compared with. If expression1 =
expr, then instructions1
are executed. You can think of the expression
following select as the key to the individual case clauses below it.
the value of expr to determine which case clause to use.
The case clauses are
separate conditional statements. The
value of each is compared to the expression in variable expr. There is
no limit to the number of
individual case clauses you can put before the end. Each case is
one or more Scilab functions or statements that are executed if the
the preceding case clause matches the value of the expr following
Here's an examp1e that
you can run:
n = x_dialog('Please enter a sign: ');
did not chose a sign!')
function creates an X-window multi-lines dialog,
and in this case you can enter any alphanumeric character. Notice that
entering a string, not a number. x_dialog is prepared to receive a
could use evstr
for evaluation of expressions, but that’s another story that we
won’t explore now).
This window pops-up after the x_dialog function is executed
When Scilab finds the select case statement, it takes note
of the value of the variable (in this example, the value of n) and then
examines the value specified in the first case. If that value matches
of the variable n, Scilab:
the statements following that case until it encounters the next case or
the end statement.
down to the statement following end and continues executing the
If the value in the first
case clause doesn’t match the
value of the
Scilab checks the value of each clause until it
finds a match.
If none of the case
clauses contain a value matching the
value of the variable following select (n), Scilab jumps down to the
following end, and continues executing the program.
'Switch Statement Scilab' to home
'Switch Statement Scilab' to Scilab Menu