DEV Community

Cover image for switch Statements
Paul Ngugi
Paul Ngugi

Posted on

switch Statements

A switch statement executes statements based on the value of a variable or an expression.
Based on the previous Case Study on Computing Taxes found here

if (status == 0) {
// Compute tax for single filers
}
else if (status == 1) {
// Compute tax for married filing jointly or qualifying widow(er)
}
else if (status == 2) {
// Compute tax for married filing separately
}
else if (status == 3) {
// Compute tax for head of household
}
else {
 System.out.println("Error: invalid status");
 System.exit(1);
}
Enter fullscreen mode Exit fullscreen mode

The if statement makes selections based on a single true or false condition. There are four cases for computing taxes, which depend on the value of status. To fully account for all the cases, nested if statements were used. Overuse of nested if statements makes a program difficult to read. Java provides a switch statement to simplify coding for multiple conditions. You can write the following switch statement to replace the nested if statement.

switch (status) {
case 0: compute tax for single filers;
break;
case 1: compute tax for married jointly or qualifying widow(er);
break;
case 2: compute tax for married filing separately;
break;
case 3: compute tax for head of household;
break;
default: System.out.println("Error: invalid status");
 System.exit(1);
}
Enter fullscreen mode Exit fullscreen mode

The flowchart of the preceding switch statement:

Image description

This statement checks to see whether the status matches the value 0, 1, 2, or 3, in that order. If matched, the corresponding tax is computed; if not matched, a message is displayed. Here is the full syntax for the switch statement:

switch (switch-expression) {
case value1: statement(s)1;
break;
case value2: statement(s)2;
break;
 ...
case valueN: statement(s)N;
break;
default: statement(s)-for-default;
}
Enter fullscreen mode Exit fullscreen mode

The switch statement observes the following rules:

  • The switch-expression must yield a value of char, byte, short, int, or String type and must always be enclosed in parentheses.
  • The value1, . . ., and valueN must have the same data type as the value of the switch-expression. Note that value1, . . ., and valueN are constant expressions, meaning that they cannot contain variables, such as 1 + x.
  • When the value in a case statement matches the value of the switch-expression, the statements starting from this case are executed until either a break statement or the end of the switch statement is reached.
  • The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression.
  • The keyword break is optional. The break statement immediately ends the switch statement.

Do not forget to use a break statement when one is needed. Once a case is matched, the statements starting from the matched case are executed until a break statement or the end of the switch statement is reached. This is referred to as fall-through behavior. For example, the following code displays Weekdays for day of 1 to 5 and Weekends for day 0 and 6.

switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
Enter fullscreen mode Exit fullscreen mode

To avoid programming errors and improve code maintainability, it is a good idea to put a comment in a case clause if break is purposely omitted.

Here's a program to find out the Chinese Zodiac sign for a given year. The Chinese Zodiac is based on a twelve-year cycle, with each year represented by an animal— monkey, rooster, dog, pig, rat, ox, tiger, rabbit, dragon, snake, horse, or sheep —in this cycle. Note that year % 12 determines the Zodiac sign. 1900 is the year of the rat because 1900 % 12 is 4.

Image description

Image description

Top comments (0)