Java break statement

Introduction

Java break statement has three uses

  • it can terminate a statement sequence in a switch statement.
  • it can exit a loop
  • it can break to a labeled line

break to Exit a Loop

By using break, we can terminate a loop.

Here is a simple example:

// Using break to exit a loop. 
public class Main {
  public static void main(String args[]) {
    for(int i=0; i<100; i++) {
      if(i == 10) {
         break; // terminate loop if i is 10
      }      // w  w w.j a  va2s .  c  o  m
      System.out.println("i: " + i);
    }
    System.out.println("Loop complete.");
  }
}

Although the for loop is designed to run from 0 to 99, the break statement to terminate it when i equals 10.

break to exit while loop

The break statement can exit any of Java's loops.

For example, here the break statement exits a while loop.

// Using break to exit a while loop. 
public class Main {
  public static void main(String args[]) {
    int i = 0;//  w w w.jav  a2  s. co  m
    
    while(i < 100) {
      if(i == 10) {
         break; // terminate loop if i is 10
      }      
      System.out.println("i: " + i);
      i++;
    }
    System.out.println("Loop complete.");
  }
}

break nested loop

When used with nested loops, the break statement will only break out of the innermost loop.

For example:

// Using break with nested loops.
public class Main {
  public static void main(String args[]) {
    for(int i=0; i<3; i++) {
      System.out.print("Pass " + i + ": ");
      for(int j=0; j<100; j++) {
        if(j == 10) {
           break; // terminate loop if j is 10
        }//  www. j  av  a 2  s  .co m
        System.out.print(j + " ");
      }
      System.out.println();
    }
    System.out.println("Loops complete.");
  }
}

break to a labeled line

Java break statement can break out of one or more blocks of code to a labeled line of code.

The general form of the labeled break statement:

break label; 

label identifies a block of code.


public class Main {
  public static void main(String args[]) {
    boolean t = true;

    first: {//  ww w  . jav  a  2 s  .  c o  m
      second: {
        third: {
          System.out.println("Before the break.");
          if(t) {
             break second; // break out of second block
          }          
          System.out.println("This won't execute");
        }
        System.out.println("This won't execute");
      }
      System.out.println("This is after second block.");
    }
  }
}

For example, following program, the outer loop executes only once:

// Using break to exit from nested loops
public class Main {
  public static void main(String args[]) {
    outer: for(int i=0; i<3; i++) {
      System.out.print("Pass " + i + ": ");
      for(int j=0; j<100; j++) {
        if(j == 10){
            break outer; // exit both loops
        }/*from w ww  . j  av  a 2s  . co m*/
        System.out.print(j + " ");
      }
      System.out.println("This will not print");
    }
    System.out.println("Loops complete.");
  }
}



PreviousNext

Related