Switch Case
- Aktualisiert2026-07-22
- 3 Minute(n) Lesezeit
Verwenden Sie die switch case-Steuerstruktur als Alternative zur if-Anweisung.
Die switch-Anweisung beschreibt eine Mehrfachauswahl. Ein Ausdruck wird einmal ausgewertet und mit Konstanten verglichen. Wenn die Ausdrücke gleich sind, werden die Anweisungen nach der Konstante verarbeitet. Wenn keine case-Konstante mit dem Wert der Variablen übereinstimmt, werden die default-Anweisungen (falls vorhanden) ausgeführt. Wenn die default-Anweisung fehlt, wird kein Code ausgeführt, sofern keine Übereinstimmung vorliegt.
Die Syntax lautet:
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
- Die Konstanten dürfen nur einmal in einer switch-Steuerstruktur vorkommen.
- Konstanten können Text-, Integer- oder Hexadezimalkonstanten, Enums oder Literale sein.
- Wenn Sie Literale verwenden, müssen Sie die Variable mit dem Stichwort const deklarieren. Weisen Sie der Variablen einen festen Wert zu.
- Geben Sie Konstanten als Wert, Liste oder Wertebereich an:
- Einzelne Elemente in Listen durch Kommas getrennt:
const1, const2, ..., constN /* the following code is executed when the comparison variable takes one of the values in the enumeration */
- Wertebereiche, die durch zwei aufeinander folgende Doppelpunkte gekennzeichnet sind:
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 */
- Einzelne Elemente in Listen durch Kommas getrennt:
- Der Datentyp der Konstanten basiert auf dem Datentyp der Variablen switch. Die Datentypen müssen übereinstimmen.
- Setzen Sie Konstanten des Datentyps text in doppelte Anführungszeichen. Beispiel: case "Text1".
- Sie können in jedem Codebereich zusätzliche switch- oder if-Bedingungen haben.
- Der Code für jeden Case wird eigenständig ausgeführt und nicht mit dem nächsten Case fortgesetzt. Es gibt keinen Fallthrough.
- Der Codebereich nach einer case-Anweisung kann leer sein.
Beispiel: Integer-Konstanten
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
Beispiel: Literale
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
Beispiel: Listen
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
Beispiel: Wertebereiche
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