Answer:
0.2
Rules for switch Statements
Here is what a switch statement looks like:
switch ( integerExpression )
{
case label1 :
statementList1
break;
case label2 :
statementList2
break;
case label3 :
statementList3
break;
. . . other cases like the above
default:
defaultStatementList
}
Here is how it works:
- Only one case is selected per execution of the
switchstatement. - The value of
integerExpressiondetermines which case is selected. integerExpressionmust evaluate to anintegertype (includingchar).- Each
labelmust be an integer literal (like 0, 23, or 'A'), but not an expression or variable. - There can be any number of statements in a
statementList. - The
statementListis usually followed withbreak; - Each time the
switchstatement is executed, the following happens:- The
integerExpressionis evaluated. - The
labels after eachcaseare inspected one by one, starting with the first. - The first label that matches has its
statementListexecute. - The statements execute until the
breakstatement is encountered. - Now the entire
switchstatement is complete.
- The
- If no case label matches the value of
integerExpression, then thedefaultcase is picked, and its statements execute.
QUESTION 6:
Would you believe that there are even more rules for the break-statement?