﻿Selection blocks

# Selection blocks

GoManga MathStudio Language has three constructs to make selections: if-then, if-then-else and switch-case.

if-then selection

The if-then selection is a simple selection construct. The code between then and endif will be executed only if the condition between if and then is satisfied. Example:

```//Input a variable
[InVar] = input

if {InVar} = 0 then
output The input value is 0.
endif

pause
//The code will output the string "The input value is 0." only if the value of InVar is "0"```
if-then-else selection

The if-then-else selection is a binary selection construct. The code between then and else will be executed only if the condition between if and then is satisfied, otherwise, the code between else and endif will be executed. Example:

```//Input variable
[InVar] = input

//if number mod 2 is equal to 0 (% is the modulo operator)
if {InVar} % 2 = 0 then
output {InVar} is even
else
output {InVar} is odd
endif

pause

//This code checks if an input number is even or odd```
switch-case selection

The switch-case selection is a multiple selection construct. The code between case and endcase will be executed only if the variable between switch and then is equal to the value indicated between case and then. For example:

```[InVar] = input

switch {InVar}%4 then
case 0 then
output {InVar} is 0 modulo 4
endcase
case 1 then
output {InVar} is 1 modulo 4
endcase
case 2 then
output {InVar} is 2 modulo 4
endcase
case 3 then
output {InVar} is 3 modulo 4
endcase
endswitch

pause

//The code will output the mod4 value of the input variable```

Please note that differently from C-like languages, you don't need to use the break keyword to jump to the end of the switch block. You can add a default case to manage the instance if no one of the case before are satisfied. The default block should always be the last one in the switch.

```[InVar] = input

switch {InVar}%4 then
case 0 then
output {InVar} is 0 modulo 4
endcase
case 1 then
output {InVar} is 1 modulo 4
endcase
case 2 then
output {InVar} is 2 modulo 4
endcase
case 3 then
output {InVar} is 3 modulo 4
endcase
default then
output Invalid input
enddefault
endswitch

pause

//The code will output the mod4 value of the input variable. If the input value is not valid, the code will return the "Invalid input" string.```

Note that you can't use a switch inside another switch and the switch work only with numerical values.