Java - Add break statement to switch statement

Introduction

When a break statement is executed inside a switch statement, the control is transferred outside the switch statement. For example,

Demo

public class Main {
  public static void main(String[] args) {
    int i = 10;/*w  w  w . j a v a  2s . c  o  m*/
    switch (i) {
    case 10:
      System.out.println("Ten");
      break; // Transfers control outside the switch statement
    case 20:
      System.out.println("Twenty");
      break; // Transfers control outside the switch statement
    default:
      System.out.println("No-match");
      break; // Transfers control outside the switch statement. It is not necessary.
    }

  }
}

Result

Related Topic