switch
statement has the following syntax:
statement-switch:
switch (sw-expr)
{
case const-case-expr1:
statement-block *
[case const-case-expr2:
statement-block *
…
]
[default:
statement-block +
]
}
switch
statement's block must not be followed by a ‘;’ character!
switch
statement is evaluated according to the following procedure:
const-case-exprN
expressions are evaluated at compile time. If they fail to compute to a constant value, compilation error occurs. If you need to use non-const expressions, consider using the case_union.sw-expr
is evaluated at run time.const-case-exprN
value one by one until a match is found. If the match is not found, statement-block after the “default:” label is evaluated, if present.struct A
{
BYTE type;
switch (type)
{
case 0:
int value;
break;
case 1:
double value;
break;
case 2:
string value;
break;
default:
$assert("Invalid file");
} // note: no ';' allowed here!
};