18.6.9 switch-endswitch
The switch statement selects the processing according to the result of the expression. The expression is specified within parentheses.
The switch statement can contain case labels and a default label, which are options of processing to be executed. The case labels can have unique constants. For the switch statement to be meaningful, it must contain at least one case label.
You can specify only one default label. This label is not a required item. When you use this, do not specify any value.
- Organization of this subsection
(1) Format
switch (expression) [case constant-value:]+ [expression;]* ... ... [default:] [expression;]* endswitch;
(2) Description
-
The expression is evaluated.
-
The control moves to the block that has the same case label value as the result of the expression. Until the break statement is encountered, the subsequent statements are executed (dependent of the case label value).
-
If the break statement is encountered, the control leaves the switch statement.
-
If the result of the expression is not equal to the case label value, the control will move to the default label, when it is specified.
When specifying the switch-endswitch statement, you have to follow the rules below:
-
The data type of the value returned by the expression in the switch statement must be the same as the constants specified in case labels.
-
You must not nest more than 255 switch statements.
-
case labels do not need to have associated executable statements (with the exception of last case statement, which must have at least one associated executable statement).
-
The switch-endswitch statement can have up to 255 case labels.
-
In case labels, you can specify a numeric constant, a string constant, or an AIT language macro. You cannot specify any expressions in them.
Example
case -5: // Valid case +6: // Valid case "String": // Valid case intvar: // Invalid case 3+2: // Invalid
-
The constants you specify in case labels must have the same data type as the result of the expression in the switch statement.
Example
switch (Stringvar1+Stringvar2) //Both variables are string type. case 1: // Invalid case "caption-1": // Valid . . endswitch;
-
If no case statement is specified in a switch case statement, the statement is interpreted as a syntax error.
-
You can specify a maximum of 255 break statements in one switch case statement.
-
You do not need to specify any statements for all case labels except the last case label.
Example
switch(i) { case 1: case 2: a=b+c; // If you do not specify this statement, the script // analyzer will issue a syntax error. }
(3) Example of coding
s1="abcdefghijk"; switch (!AIT_IsEmpty(s1 )) case true: // Executed if s1 is empty. s2 = AIT_StrUpper(s1); AIT_MessageBox("s2",s2); if ( ( length = AIT_StrLength(s2)) > 10) break; endif; break; default: // Executed if the expression evaluates to false. break; // endswitch;