×

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.


Related Topics

Bouncy Number in Java

We will define bouncy numbers in this section and write Java programmes to determine whether a specific number is bouncy. Java coding exams and academic assignments usually inquire about the...

3 minutes read.

Java Integer equals() method

The equals() method of Integer class compares the given object to the specified object. Syntax public boolean equals(Object obj) Parameters The parameter ‘obj’ represents the object to be compared with. Overrides The equals() method overrides equals...

1 minute read.

Polygonal Number in Java

What does a Java Polygonal Number Mean? In mathematics, a polygonal number refers to a number that is expressed through dots or pebbles arranged in a regular polygonal pattern. Alphas are...

4 minutes read.

Collection Programs in Java

Collection Programs in Java Collection in Java provides a way to manipulate or store a group of objects. Each object in a collection is called element. Collection programs in Java mainly...

4 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

Java HashMap

Java HashMap extends AbstractMap and implements Map interface. It is the collection of multiple entries where an entry consists of key and value pair. The HashMap can contain only one...

7 minutes read.

Packages Program in java

Let us know what package is in the Java programming language, and later we will learn about types of packages and how to define a package. How to import Java...

4 minutes read.

Frugal number in Java

A frugal number is a positive integer with base b that has more digits than the number of prime factors it can be factored into (including exponents greater than 1)....

3 minutes read.

String vs StringBuilder

String vs StringBuilder In this section, we will discuss the comparison between Java String and StringBuilder class. String In Java, a string is treated as an object that represents a sequence of characters....

4 minutes read.

Java InetAddress class

InetAddress class The InetAddress class refers to the IP address, both IPv4 and IPv6.An instance of an InetAddress consists of an IP address and possibly its corresponding hostname. It provides a method to get the...

9 minutes read.

Features of Java

The objective of the Java programming language is to provide portability, security, and simplicity. In spite of this, Java has a number of features that makes it a popular language...

5 minutes read.

Nth Term of Geometric Progression in Java

There are 3 numbers provided. The geometric progression's initial term is the first number. The second number is the geometric progression's common ratio, and the third number represents the nth...

3 minutes read.

Java Integer doubleValue() method

The doubleValue() method of Integer class returns a double value for this Integer after a widening primitive conversion. Syntax public double doubleValue() Parameters NA Specified by This method is specified by doubleValue in class Number Return Value This...

1 minute read.

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

Java Project Ideas

When it comes to constructing projects, Java is regarded as one of the best languages and is also one of the most paid. Java excels in any application, whether it...

10 minutes read.

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two...

3 minutes read.

Java String startsWith() method

Java String startsWith() method checks whether current String starts with given prefix or not . Syntax: public boolean startsWith(String prefix) public boolean startsWith(String prefix, int offset) Parameters: prefix : It is sequence of character Returns: It returns...

2 minutes read.

Java Technologies List

Introducing Java technology is not necessary. Everyone across the globe is still in awe of Java's incredible capabilities for developing mobile apps and websites. Of course, you can be persuaded...

8 minutes read.

Level order Traversal of a Binary Tree in Java

In Java, a level order traversal of a tree structure is also referred to as the breadth-first traversal of the binary tree. Regarding the subsequent binary tree: Level order traversal is as...

5 minutes read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.