2/8/00
COM1100 Winter2000 Yuhong Yin
8
switch statement
n
switch ( expression )
n
{ // start of the compound statement
n
case value_1 :
n
statement 1;
n
break;
n
case value_2 :
n
statement 1;
n
break;
n
…
n
case value_n :
n
statement n;
n
break;
n
default :
n
statement;
n
}
// end of
the compound statement
It provides an alternative to
if-else chain for cases to
compare the value of an
integer expression to a
specific value.
switch statement uses 4 C++
keywords :
•
switch
•
case
•
break
•
default