HP C A.06.05 Reference Manual
Statements
switch
Chapter 6178
switch
Syntax
switch (
exp
)
{
case
const_exp
: [
statement
]...
[case
const_exp
: [
statement
]...]
[default : [
statement
]...]
}
Arguments
exp
The integer expression that the switch statement evaluates and then
compares to the values in all the cases.
const_exp
An integer expression to which
exp
is compared. If
const_exp
matches
exp
,
the accompanying statement is executed.
statement
Zero or more simple statements. (If there is more than one simple
statement, you do not need to enclose the statements in braces.)
Description
The switch statement is a conditional branching statement that selects among several
statements based on constant values.
The expression immediately after the switch keyword must be enclosed in parentheses and
must be an integral expression.
The expressions following the case keywords must be integral constant expressions; that is,
they may not contain variables.
An important feature of the switch statement is that program flow continues from the
selected case label until another control-flow statement is encountered or the end of the
switch statement is reached. That is, the compiler executes any statements following the
selected case label until a break, goto, or return statement appears. The break statement
explicitly exits the switch construct, passing control to the statement following the switch
statement. Since this is usually what you want, you should almost always include a break
statement at the end of the statement list following each case label.