Before going through this article, it is recommended to have a basic understanding about:
- Basic operators in Scilab
- How to create a Scilab script file
- Scilab Variables – naming, real numbers, constants
When designing an algorithm we might come up with a situation in which we would like to define some actions function of the state/value of another variable. If the control variable has multiple states, for a better structure and readability of the code, it is better to use SELECT CASE instead of IF ELSEIF conditional statements.
The general syntax of the SELECT CASE conditional statement is:
The current value of the control variable (controlVariable
) is evaluated and compared with each of the cases starting with the fist one (value_1
). When a match case is found, the corresponding instruction is executed.
For example, if controlVariable == value_1
the instructions_1
are executed. If controlVariable
is different than value_1
, the following value (value_2
) is used for comparison. If controlVariable == value_2
, the instructions_2
are executed.
If none of the cases are found TRUE then the else
instructions are evaluated (default instructions).
Note: The keyword then
must be on the same line with the corresponding case
keyword. If the keyword then
is not on the same line with the corresponding case
, a continuation mark must be used.
The keyword then
is optional. In the examples below we’ll use different types of syntax which will output the same result.
When using SELECT CASE conditional statements, in order to have a readable code, it is recommended to:
- put each
case
statement on a new line - put each
then
statement on a new line - use as less keywords as possible
As an example, we will write a script which displays in the Scilab console the corresponding decimal number (10 … 15) for a hexadecimal letter (A … F). The same output is obtained using different syntax variants for the SELECT CASE conditional statements.
1. Recommended syntax for best code readability (each statement on a new line, then
keyword is not used):
sHex = 'A' select sHex case 'A' disp(10) case 'B' disp(11) case 'C' disp(12) case 'D' disp(13) case 'E' disp(14) case 'F' disp(15) else disp('Not a HEX alpha numeric character!') end
2. Writing the case
values and then
instructions on the same line. The code is more compact but can become hard to read for more complex instructions:
sHex = 'A' select sHex case 'A' then disp(10) case 'B' then disp(11) case 'C' then disp(12) case 'D' then disp(13) case 'E' then disp(14) case 'F' then disp(15) else disp('Not a HEX alpha numeric character!') end
3. The same as the previous example except that the then
keyword is replace by a comma “,"
sHex = 'A' select sHex case 'A', disp(10) case 'B', disp(11) case 'C', disp(12) case 'D', disp(13) case 'E', disp(14) case 'F', disp(15) else disp('Not a HEX alpha numeric character!') end
4. The same as the previous example but without using a comma “,”
sHex = 'A' select sHex case 'A' disp(10) case 'B' disp(11) case 'C' disp(12) case 'D' disp(13) case 'E' disp(14) case 'F' disp(15) else disp('Not a HEX alpha numeric character!') end
5. Defining several cases on the same line (not recommended)
sHex = 'A' select sHex case 'A' then disp(10), case 'B' then disp(11) else disp('Not a HEX alpha numeric character!') end
6. Usage of a double precision number as “select” and “case” expressions:
select 1.1 case 1.1 disp('ok') else disp('nok') end
7. Usage of a matrix as “select” and “case” expressions:
select [1 2] case [1 2] disp('ok') else disp('nok') end
8. Double definition of the same case. In the example below there are two identical cases A
but with different instructions. This means that case A
is defined twice, first time displays value 10
, second time displays value 11
. The second definition never gets executed because when a match is found the corresponding instruction is executed and the SELECT CASE statement is exited.
sHex = 'A' select sHex case 'A' disp(10) case 'A' disp(11) case 'C' disp(12) case 'D' disp(13) case 'E' disp(14) case 'F' disp(15) else disp('Not a HEX alpha numeric character!') end
SELECT CASE conditional statements are not as much used in programming as IF ELSE statements. Nevertheless, for some particular cases, they are recommended in order to have a good code structure and better readability.
For any questions, observations and queries regarding this article, use the comment form below.
Don’t forget to Like, Share and Subscribe!