Fall through in Java
In this article, you will be acknowledged about the fall through condition in Java along with an example program.
Fall through
It is a situation where there isn't a break statement in any of the cases. Although a break statement is not required in a switch statement. Implementing a break statement is optional, but it could be makes a great sense. Unless a break statement is found, the control flow in this case will be disturbed. It happens quite rarely.
The break statement indicates that the case is over. A program will continue to run until a break statement is discovered or the completion of the transition is reached if a break statement is not included.
If a switch statement evaluates every statement that follows the first test case, we can conclude that it ended in failure. Notably, there is no break statement after each case.
Let us understand this condition with the syntax and an example program.
Syntax
switch(expression)
{
case 1:
//program statements
case 2:
//program statements
case 3:
//program statements
case n:
//program statements
default:
// program statements
}
Example(1):
File name: Fallthrou1.java
// Java program that depicts the flow and implementation of fall through condition
import java.util.*;
import java.io.*;
class Fallthrou1{
public static void main(String[] args) {
int lte = 1;
switch ( lte ){
case 1:{
System.out.println("I am an intern");
}
//Because there is no break statement, a fall through situation will occur.
case 2:{
System.out.println("I am a student");
}
case 3:{
System.out.println("I am a java aspirant");
}
default :{
System.out.println("I am a human being");
}
}
}
}
Output
I am intern
I am a student
I am a java aspirant
I am a human being
Example(2);
File name: Fallthrou2.java
// Java program that would represent the flow of fall through condition in java
import java.util.*;
import java.io.*;
public class Fallthrou2
{
public static void main(String arg[])
{
String sky="She ";
switch(sky)
{
case "she":
System.out.println("She is a good woman.");
case "them":
System.out.println("She is a wise woman.");
case "me":
System.out.println("She is a good friend of mine.");
case "ours":
System.out.println("She helped me through hard times.");
default:
System.out.println("We are best friends.");
}
}
}
Output
She is a good woman.
She is a wise woman.
She is a good friend of mine.
She helped me through hard times.
We are best friends.
Real-time applications
The fall through condition in java can be implemented to solve a few real- time situations like
- If a character is a vowel or a consonant.
- To know the number of days in a specific month
File name: VorC.java
// Java Program to verify if an alphabet is a vowel or a consonant
import java.util.*;
import java.io.*;
class VorC{
public static void main(String[] args) {
char alpha='k';
switch ( alpha ){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println("It is a Vowel");
break;
default :{
System.out.println(" It is a Consonant");
}
}
}}
Output
It is a Consonant
File name: Daysinmon.java
// Java program to know the number of days in a specific month
import java.util.Scanner;
public class Daysinmon {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int mon = 0;
int d;
System.out.println("Enter the number of month in a year : ");
mon = s.nextInt();
switch (mon) {
case 4:
case 6:
case 9:
case 11:
d = 30;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
d = 31;
break;
case 2:
d = 28;
break;
default:
d = 0;
break;
}
if (d != 0)
{
System.out.println("The total number of days in a given month is :"+d);
}
else
{
System.out.println("Please enter a valid input");
}
}
}
Output
Enter the number of month in a year :
8
The total number of days in a given month is :31
Advantages
We are well aware that the switch statement only functions for a single data point or statement, and that there are frequently multiple outputs with the same value. In these situations, the fall through condition is significant and helps the system run more quickly by avoiding comparing.
Disadvantages
We neglect to specify explicit break statement inside the switch statement, which causes all cases, even those that don't match the searched value to be executed. The program suffer greatly as a result of this scenario. Therefore, to resolve this issue, we must include the break keyword inside the switch statement for each case. The fall through circumstance has this problem.