[course03] 04 switch case
Base switch case
A switch statement allows you to test the value of an expression and, depending on that value, to jump directly to some location within the switch statement. Only expressions of certain types can be used. The value of the expression can be one of the primitive integer types int, short, or byte. It can be the primitive char type. It can be String. Or it can be an enum type. In particular, note that the expression cannot be a double or float value.
switch (expression) {
case constant-1:
statements-1
break;
case constant-2:
statements-2
break;
.
. // (more cases)
.
case constant-N:
statements-N
break;
default: // optional default case
statements-(N+1)
} // end of switch statement
//same as
if (expression == constant-1) { // but use .equals for String!!
statements-1
}
else if (expression == constant-2) {
statements-2
}
.
.
.
else if (expression == constant-N) {
statements-N
}
else {
statements-(N+1)
}example : menus
Enum in switch statements
Last updated
Was this helpful?