The break statement is optional. If you don’t write the break statement at the end of your case statement, execution of the program will continue into the next case and then to the case after that and so on until a break statement is encountered or the end of the switch statement is reached. In programming terms, we refer to this as program control falling through the cases of switch statement. To understand this fall through thing, let’s go back to our last example of switch statement and remove all the break statements from cases and see what happens.
/**
* This program shows the usage of switch statement.
*
* @author KnowledgeBoat
* @version 1.0
*/
public class KboatSwitchExample
{
public void demoSwitch() {
int number = 1;
switch(number) {
case 0:
System.out.println("Value of number is zero");
case 1:
System.out.println("Value of number is one");
case 2:
System.out.println("Value of number is two");
default:
System.out.println("Value of number is greater than two");
}
System.out.println("End of switch");
}
}
What is this output we got this time? Let’s try to understand it. Value of number is 1, so case 1 is matched and the println following it gets executed. We see Value of number is one
printed in the output.
As we have removed the break statement from this case, program control falls through to case 2, its println gets executed and we see the output Value of number is two
in the console window.
As there are no break statements in our switch, the fall through of program control continues to the default case and we see the output Value of number is greater than two
in the console.
So that’s fall through in switch statement for you. Let’s write a break statement in case 2 and run the program.
Case 1 gets executed, then program control falls through to case 2. println of case 2 gets executed but since case 2 now has a break statement, it transfers the program control to the end of switch statement and the println of the default case is not printed.
By now you may wonder, why switch statement behaves this way and is there any use of this fall through behaviour? Turns out, this fall through behaviour of switch is very useful in programming. Let me prove it to you with an example.
Our example program takes the month of the year as input and prints the season corresponding to that month as the output. It decides the season based on this table:
Months | Season |
---|---|
Feb, Mar | Spring |
Apr, May, Jun | Summer |
Jul, Aug, Sep | Monsoon |
Oct, Nov | Autumn |
Dec, Jan | Winter |
import java.util.Scanner;
public class KboatSeasons
{
public void printSeason() {
Scanner in = new Scanner(System.in);
System.out.print("Enter the month number: ");
int month = in.nextInt();
switch(month) {
case 2:
case 3:
System.out.println("Its Spring season.");
break;
case 4:
case 5:
case 6:
System.out.println("Its Summer season.");
break;
case 7:
case 8:
case 9:
System.out.println("Its Monsoon season.");
break;
case 10:
case 11:
System.out.println("Its Autumn season.");
break;
case 12:
case 1:
System.out.println("Its Winter season.");
break;
default:
System.out.println("Invalid Month");
break;
}
}
}
Notice that multiple months map to the same season. In such, situations we can use fall through behaviour of the switch statement. In the program,we have clubbed together the cases of months mapping to the same season and provided a single break statement.
Let’s say I give the month number as 4. It will match case 4. Since there is no break the program control will fall through case 5 and 6 and print Its Summer season.
to the console. The break after the println statement will transfer the program control outside of the switch. Using the fall through behaviour of switch statement, I can make this program concise by not repeating the same println statement in the case of each month that maps to the same season.
Below is the output of the program with a few different months as input.