Switching to make decisions
We have seen the vast and virtually limitless possibilities of combining the Java operators with if and else statements. But sometimes a decision in Java can be better made in other ways.
When we are deciding based on a clear list of possibilities that don't involve complex combinations, switch is usually the way to go.
We start a switch decision like this:
switch(argument){
}
In the previous example, argument could be an expression or a variable. Within the curly braces, {}, we can make decisions based on the argument with case and break elements:
case x: // code for case x break; case y: // code for case y break;
You can see in the previous example that each case states a possible result and each break denotes the end of that case, as well as the point at which no further case statements should be evaluated.
The first break encountered breaks out of the...