SWITCH Statement
The SWITCH statement is used to decide between multiple values of an expression.
SWITCH expression CASE [ expression | expression .. expression ] { , ... } statements; END; ... { DEFAULT statements; END; } END;
The main expression is matched against each CASE clause in turn, until a match is found. The statements in the matching clause are then executed. If there is more than one matching clause, only the first one encountered is executed. If no CASE clauses are matched, then the statements in the DEFAULT clause are executed, if the clause is present. The DEFAULT clause does not necessarily have to be the final clause. However, only one DEFAULT clause is allowed.
Each CASE clause contains one or more expressions that are tested against the main expression. There are two types of matches that can be made: equality with a single value or a match against a range of values specified using two dots. When matching against a range, the range is inclusive, i.e. the expression is matched if it is equal to either the upper or lower bound, or any value in between.
The expression to match against may be either an INT or a STRING. The matching expressions must be of the same type.
Execution of the statements in a CASE clause can be terminated early using the BREAK statement.
Example
SwitchExample(INT value, STRING str) SWITCH value CASE 0 PRINT("Matched 0\n"); END; CASE 1, 2 PRINT("Value is 1"); IF value = 1 THEN PRINT("\n"); BREAK; END; PRINT(" or 2\n"); END; CASE 3..7, 9..11 PRINT("3 <= value <= 7 or 9 <= value <= 11\n"); END; DEFAULT PRINT("No match\n"); END; END; SWITCH str CASE "y", "Y", "Yes", "yes" result := TRUE; END; CASE "n", "N", "No", "no" result := FALSE; END; DEFAULT PRINT("Unexpected value in string"); END; END; END;
See Also
XJTAG v4.1.100