Switch Case
- Updated2025-10-31
- 3 minute(s) read
Use the switch case control structure as an alternative to the if instruction.
The switch instruction describes a multiple selection. An expression is evaluated once and compared with constants. If the expressions are equal, the instructions that come after the constant are processed. If no case constant matches the value of the variable, the default instructions (if any) are executed. If the default statement is missing, no code is executed if there is no match.
The syntax is:
switch variable
case constant_1
// Code
case constant_2, constant_3, constant_4
// Code
casec onstant_5::constant_6
// Code
case...
// Code
default
// Code
endswitch
- The constants must only occur once within a switch control structure.
- Constants can be text, integer or hexadecimal constants, enumerations or literals.
- If you use literals, you must declare the variable with the keyword const. Assign the variable a fixed value.
- Specify constants as a value, a list, or a value range of:
- Individual elements in listings separated by
commas:
const1, const2, ..., constN /* the following code is executed when the comparison variable takes one of the values in the enumeration */
- Value ranges indicated by two consecutive
colons:
const1::const2 /* the following code is executed when the comparison variable is >= const1 and <= const2*/ const1:: /* the following code is executed if the comparison variable is >= const1 */ ::const1 /* the following code is executed if the comparison variable is <= const1 */
- Individual elements in listings separated by
commas:
- The data type of the constants is based on the data type of the switch variable. The data types must match.
- Specify constants of the text data type in double quotation marks. For example, case "Text1".
- You can have additional switch or if conditions in any code area.
- The code for each case runs by itself and does not continue into the next case. There is not a fall-through.
- The code area after a case can be empty.
Example: Integer Constants
program
section globals
int32 a, b
real64 c
endsection
switch a
case 1
b = 5 * a
c = sin( real64( b ) )
case 2
b = 0
c = cos ( real64 ( b ) )
default
b = -1
endswitch
endprogram
Example: Literals
program
section globals
int32 input.CNT
endsection
int32 _a = 3 /* missing const declaration => NO literal */
const int32 _b = 3 /* konst. Declaration with const. value => Literal */
const int32 _c = 4 + _b /* constant declaration with constant values => literal */
const int32 _d = 5 + _a /* _a is no literal => _d is also NOT a literal */
switch input.CNT
case _a
/* Error because _a is not a literal*/
case _b
/* OK, because literal _b corresponds to the value 3 */
case _c
/* OK, because literal _c corresponds to the value 7 */
case _d
/* Error because _d is not a literal*/
endswitch
endprogram
Example: Listings
program
section globals
int32 input.CNT
endsection
const int32 _a = 3 /* const. Declaration with const. value => Literal */
const int32 _b = 3 /* konst. Declaration with const. value => Literal */
const int32 _c = 4 + _b /* constant declaration with constant values => literal */
const int32 _d = 5 + _a /* _a is a literal => _d is also a literal */
switch input.CNT
case _a, 1
/* OK */
case _b, 4, 5
/* OK*/
case _c, _d
/* OK */
case 3
/* Error, because _b = 3 is already included in the listing */
endswitch
endprogram
Example: Value Ranges
program
section globals
int32 input.CNT
endsection
switch input.CNT
case 20::
/* input.CNT >= 20 */
case ::0
/* input.CNT <= 0 */
case 4::10
/* 4 >= input.CNT <= 10 */
case 25
/* Error: 25 is within the value range of '20::' */
endswitch
endprogram