Java Reference
In-Depth Information
expression
value1
Y
equals
Do
thing
value1
?
switch(expression)
{
case value1:
// Do value1 thing
N
case value2:
// Do value2 thing
...
Y
expression
equals
Do
value2
thing
default:
// Do default action
?
value2
}
// Continue the program
N
Do default action
Continue the
program
Now when a case label value is equal to the switch expression, the code for that case is executed, and
we fall through to execute all the other cases that follow, including that for the default case if that
follows. This is not usually what you want, so make sure you don't forget the break statements.
You can arrange to execute the same statements for several different case labels, as in the following
switch statement:
char yesNo = 'N';
// more program logic…
switch(yesNo) {
case 'n':
case 'N':
System.out.println("No selected");
break;
case 'y':
case 'Y':
System.out.println("Yes selected");
break;
}
Search WWH ::




Custom Search