double discount;
// Usually code would be read in
char code = 'B' ;
switch ( code )
{
case 'A':
discount = 0.0;
break;
case 'B':
discount = 0.1;
break;
case 'C':
discount = 0.2;
break;
default:
discount = 0.3;
}
System.out.println( "discount is: "
+ discount );
|
- The integerExpression is evaluated.
- In this example, the expression is just a variable, code,
which evaluates to the character 'B'.
- The case labels are inspected starting with the first.
- The first one that matches is case 'B' :
- The corresponding statementList starts executing.
- In this example, there is just one statment.
- The statement assigns 0.1 to discount
.
- The break statement is encountered.
- The statement after the switch statment is executed.
- In this example, the println statement
|