Java - Block statement with Label

Introduction

A labeled statement can be used with any type of a block statement.

The following code shows how to use a labeled break Statements:

Demo

public class Main {
  public static void main(String[] args) {
    blockLabel: {/* www  .j  ava  2  s.c o m*/
      int i = 10;
      if (i == 5) {
        break blockLabel; // Exits the block
      }

      if (i == 10) {
        System.out.println("i is not five");
      }
    }

  }
}

Result

Related Topic