Java Switch Case Statement: Syntax and Usage Explained
Table of Content:
switch case
The if statement in java, makes selections based on a single true or false condition. But switch case have multiple choice for selection of the statements or we can switch case is a multiway branch statement. The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. It adds an easy way to dispatch execution to different parts of your code based on the value of an expression.
switch (expression) { case value1: // statement sequence break; case value2: // statement sequence break; . . . case valueN: // statement sequence break; default: // default statement sequence }
switch case flow chart
The expression must be of type byte, short, int, or char;
each of the values specified in the case statements must be of a type compatible with the expression.
Examples of Switch case:
public class SwitchExample { public static void main(String[] args) { int number=20; switch(number){ case 10: System.out.println("case 10");break; case 20: System.out.println("case 20");break; case 30: System.out.println("case 30");break; default:System.out.println("Not in 10, 20 or 30"); } } }
case 20 Press any key to continue . . .
Explanation of the above program:
each time through the loop, the statements associated with the case constant that matches i are executed. All others are bypassed. After i is greater than 3, no case statements match, so the default statement is executed.
The switch statement works like this:
The value of the expression is compared with each of the literal values in the case statements. If a match is found, the code sequence following that case statement is executed. If none of the constants matches the value of the expression, then the default statement is executed. However, the default statement is optional. If no case matches and no default is present, then no further action is taken.
Why break
is necessary in switch statement ?
The break statement is used inside the switch to terminate a statement sequence. When
a break statement is encountered, execution branches to the first line of code that follows the
entire switch statement. This has the effect of jumping out of the switch.
The break statement is optional. If you omit the break, execution will continue on into the
next case. It is sometimes desirable to have multiple cases without break statements between
them.
Examples of Switch case:
// A simple example of the switch. class SwitchCaseExample { public static void main(String args[]) { for(int i=0; i<6; i++) switch(i) { case 0: System.out.println("This is case zero."); break; case 1: System.out.println("This is case one."); break; case 2: System.out.println("This is case two."); break; case 3: System.out.println("This is case three."); break; default: System.out.println("This is greater than 3."); } } }
This is case zero. This is case one. This is case two. This is case three. This is greater than 3. This is greater than 3. Press any key to continue . . .
Examples of Switch case: The break statement is optional.
// In a switch, break statements are optional. class SwitchMissingBreak { public static void main(String args[]) { for(int n=0; n<12; n++) switch(n) { case 0: case 1: case 2: case 3: case 4: System.out.println("n is less than 5"); break; case 5: case 6: case 7: case 8: case 9: System.out.println("n is less than 10"); break; default: System.out.println("n is 10 or more"); } } }
n is less than 5 n is less than 5 n is less than 5 n is less than 5 n is less than 5 n is less than 10 n is less than 10 n is less than 10 n is less than 10 n is less than 10 n is 10 or more n is 10 or more Press any key to continue . . .
Realistic usage of switch statement
// An improved version of the season program. class SwitchCaseSession { public static void main(String args[]) { int month = 4; String season; switch (month) { case 12: case 1: case 2: season = "Winter"; break; case 3: case 4: case 5: season = "Spring"; break; case 6: case 7: case 8: season = "Summer"; break; case 9: case 10: case 11: season = "Autumn"; break; default: season = "Bogus Month"; } System.out.println("April is in the " + season + "."); } }
April is in the Spring. Press any key to continue . . .
Nested switch Statements
Realistic usage of switch statement
// An improved version of the season program. // An improved version of the season program. class NestedSwitchCase { public static void main(String args[]) { int count = 1; int target = 1; switch(count) { case 1: switch(target) { // nested switch case 0: System.out.println("target is zero inner switch"); break; case 1: // no conflicts with outer switch System.out.println("target is one inner switch"); break; } break; case 2: System.out.println("case 2 outer switch"); } } }
target is one inner switch Press any key to continue . . .