Fall-Through Behavior of "switch" Statements

This section provides a tutorial example that show the 'fall-through' behavior of 'switch' statements.

From the previous tutorial, we learned that the execution of a "switch" statements starts from the label in the contained statement block that matches the value of a given test expression. The execution continues with all remaining sub-statements in the block, including "case" or "default" labels.

This behavior is called "fall-through" execution. If you do not want the execution falling through labels, you need to use "break" statements to jump out of the block.

Here is nice example from the Java language specification that demonstrate the "fall-through" behavior:

/* TooMany.java
 * From "The Java Language Specification"
 */
class TooMany {
  static void howMany(int k) {
    switch (k) {
      case 1: System.out.print("one ");
      case 2: System.out.print("too ");
      case 3: System.out.println("many");
    }
  }
  public static void main(String[] args) {
    howMany(3);
    howMany(2);
    howMany(1);
  }
}

The output confirms the fall-through behavior:

herong> java TooMany.java

many
too many
one too many

Here is the version that stops the fall-through behavior with "break" statements:

/* TwoMany.java
 * From "The Java Language Specification"
 */
class TwoMany {
  static void howMany(int k) {
    switch (k) {
      case 1: System.out.println("one");
        break;  // exit the switch
      case 2: System.out.println("two");
        break;  // exit the switch
      case 3: System.out.println("many");
        break;  // not needed, but good style
    }
  }
  public static void main(String[] args) {
    howMany(1);
    howMany(2);
    howMany(3);
  }
}

Here is the output:

herong> java TwoMany.java

one
two
many

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

Control Flow Statements

 What Is Control Flow Statement

 "if-then" Statements

 "if-then-else" Statements

 Nested "if-then-else" Statements

 "switch" Statements

Fall-Through Behavior of "switch" Statements

 Basic "for" Statements

 Enhanced "for" Statements

 "while" Statements

 "do" Statements

 "break" Statements

 Labeled "break" Statements

 "continue" Statements

 Labeled "continue" Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB