logo for matrixlab-examples.com
leftimage for matrixlab-examples.com

The common Switch statement becomes the Select Case statement in Scilab

The typical switch statement (also known as the switch-case-otherwise structure in other  programming languages, 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 a select case statement to perform the same task.

The 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 Scilab keywords, namely case and end. 

Here is the syntax for a select case statement:

select expr
   case expression1 then instructions1
   case expression2 then instructions2
   case expression then instructionsn
   [else instructions]

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. Scilab uses 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 followed by one or more Scilab functions or statements that are executed if the value in the preceding case clause matches the value of the expr following select.

select branches in Scilab 

Here's an examp1e that you can run:  

n = x_dialog('Please enter a sign: ');
select n
  case '+'
    disp('You chose +')
  case '-'
    disp('You chose -')
    disp('Sorry, you did not chose a sign!')


The x_dialog function creates an X-window multi-lines dialog, and in this case you can enter any alphanumeric character. Notice that you’re entering a string, not a number. x_dialog is prepared to receive a string (you 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

illustration of the select-case in Scilab

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 the value of the variable n, Scilab:

  • Executes the statements following that case until it encounters the next case or the end statement.
  • Jumps down to the statement following end and continues executing the program.


If the value in the first case clause doesn’t match the value of the
variable n, 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 statement following end, and continues executing the program.

 From 'Switch Statement Scilab' to home

 From 'Switch Statement Scilab' to Scilab Menu


footer for switch statement scilab page