Java Keywords (Part IX): Switch Statements
This blog will illustrate a more effective way (in certain cases) to change flow of operations by using the keyword switch.
I suggest that if you have not read any of the articles in Java Keyword series, go back read them before proceeding further. Also, go back and read the one about Data Types. All of these articles are from September 2018. That should help you find them quickly. You can also use the "search" option at the top of this page.
Java keyword list
abstract | continue | for | new | switch |
assert | default | goto* | package | synchronized |
boolean | do | if | private | this |
break | double | implements | protected | throw |
byte | else | import | public | throws |
case | enum | instanceof | return | transient |
catch | extends | int | short | try |
char | final | interface | static | void |
class | finally | long | strictfp | volatile |
const* | float | native | super | while |
A different way to write nested If/Else statements
In a previous blog, you learned how to create nested "if/else" statements. To review, let's look at the following example.
String color = "";
...
if (color.equals("green") {
// Keep going
} else if (color.equals("yellow") {
// Proceed carefully. Maybe reduce speed
} else if (color.equals("red") {
// Stop!!!
} else {
// Not sure what to do.
}
A switch statement offers one advantage over nested if/else structures and that is that the variable to be evaluated is evaluated only once. The code below illustrated the previously shown nested if/else but using a switch statement.
String color = "";
...
switch (color) {
case "green": // if (color.equals("green")
// Keep going
break;
case "yellow": //else if (color.equals("yellow")
// Proceed carefully. Maybe reduce speed
break;
case "red": // else if (color.equals("red")
// Stop!!!
break;
default: //else
// Not sure what to do.
break;
}
Converting a nested if/else structure to a switch statement is rather simple as you can see from the previous example. The "if" and "else/if" blocks are converted into a "case" statement and the "else" is converted to the "default" statement. Also, "default" statements are optional just like an "else" clause. However, it is considered bad practice to do so. That said, this conversion could get trickier in certain cases that we will explore later. For now, we need to understand the rules for using "switch" statements.
switch (expression) {
case value1:
statement(s);
break;
case value2:
statement(s);
break;
…
case valueN:
statement(s);
break;
default:
statement(s);
break;
}
Rules for using switch statements
- The switch-expression must yield a value of char, byte, short, or int or String.
- Only a single parameter in the switch-expression is allowed.
- The switch-expression must ALWAYS be enclosed in parentheses.
- All values must be the same data type as the switch-expression and must be literal values (constants).
- The only valid evaluation of the switch expression is an equality.
- The order of cases is irrelevant, but it is considered good practice for the "default" statement to be the last case in the switch body.
- The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement.
The order of the cases is relevant in some cases and "break" is always required. Consider the following example:
char letterGrade = '\u0000'; // \u0000 is the unicode NULL character
int gradePoint = 0;
...
if (gradePoint >= 90) {
gradeLetter = 'A';
} else if (gradePoint >= 80) {
gradeLetter = 'B';
} else if (gradePoint >= 70) {
gradeLetter = 'C';
} else if (gradePoint >= 60) {
gradeLetter = 'D';
} else {
gradeLetter = 'F';
}
char letterGrade = '\u0000'; // \u0000 is the unicode NULL character
int gradePoint = 0;
...
switch (gradePoint) {
case 100: // do nothing and go to next case
case 99: // do nothing and go to next case
case 98: // do nothing and go to next case
case 97: // do nothing and go to next case
case 96: // do nothing and go to next case
case 95: // do nothing and go to next case
case 94: // do nothing and go to next case
case 93: // do nothing and go to next case
case 92: // do nothing and go to next case
case 91: // do nothing and go to next case
case 90:
gradeLetter = 'A'; // grade points 100-90 get an 'A'
break; // finish
case 89:
case 88:
case 87:
case 86:
case 85:
case 84:
case 83:
case 82:
case 81:
case 80:
gradeLetter = 'B';
break;
case 79:
case 78:
case 77:
case 76:
case 75:
case 74:
case 73:
case 72:
case 71:
case 70:
gradeLetter = 'C';
break;
case 69:
case 68:
case 67:
case 66:
case 65:
case 64:
case 63:
case 62:
case 61:
case 60:
gradeLetter = 'D';
break;
default:
gradeLetter = 'F';
break;
}
By taking advantage of the rule that "break" is an optional instruction, we are able to group cases that need to execute the same logic. In this case, for example, all grade point values between 90 and 100 equate to a letter grade of 'A'. So, for instance, if the value of the gradePoint variable is 95, the "switch-expression" will go directly to the case which handles that value and fall through the case that contains the logic to be executed. So, instead of thinking strictly that "the keyword break is optional", it might be better to remember that the keyword "break" is most likely required at the very least in the cases where logic is executed. This is not to say that it has to be this way every single time. This is where creativity dictate that you deviate from the norm. But these cases are very rare.
Lastly, we determined that the order of the statements can be relevant. For this example to work, we cannot intertwine cases that will be evaluated differently. For example, a case with a value between 80 and 89 cannot be placed between cases with values between 90 and 100. However, in the original example with the traffic light colors, the order is indeed irrelevant. The takeaway here to conclude this topic is that it will all depend in the specific structure you need to create. But, following the examples and rules outlined here should get you close (if not straight to) the solution you will need for your specific case.
Comments
Post a Comment