Java break statement

In this chapter you will learn:

  1. When to use break statement
  2. Syntax for break statement
  3. Example - How to exit a for loop for a condition
  4. Example - break ut of a while loop
  5. How to use break statement to exit an infinite loop
  6. How to break just one layer of the nested loop
  7. How to break from a switch statement
  8. How to break where you want

Description

When a break statement is encountered inside a loop, the loop is terminated and program control resumes at the next statement following the loop.

Syntax

break;

or

break labelName;

Example

Here is a simple example:

 
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
      System.out.println("i: " + i);
    }//w  ww  .  j a va 2 s . co m
    System.out.println("Loop complete.");
  }
}

This program generates the following output:

Example 2

The break statement can be used with while loop as well. For example, here is the preceding program coded by use of a while loop.

 
public class Main {
  public static void main(String args[]) {
    int i = 0;//ww  w. ja  v  a 2 s  . com
    while (i < 100) {
      if (i == 10)
        break; // terminate loop if i is 10
      System.out.println("i: " + i);
      i++;
    }
    System.out.println("Loop complete.");
  }
}

The output:

Example 3

The break statement is useful to exit an infinite loop. In the following while loop the true value is hard coded in, therefore the while loop is an infinite loop. Then it uses an if statement combined with the break statement to exit the whole loop when i is 10.

 
public class Main {
  public static void main(String args[]) {
    int i = 0;//from   ww  w.  j  a  va2 s.  c om
    while (true) {
      if (i == 10){
        break; // terminate loop if i is 10
      }
      System.out.println("i: " + i);
      i++;
    }
    System.out.println("Loop complete.");
  }
}

The output:

Example 4

When used inside a set of nested loops, the break statement will only break out of the inner-most loop. For example:

 
public class Main {
  public static void main(String args[]) {
    for (int i = 0; i < 5; i++) {
      System.out.print("Pass " + i + ": ");
      for (int j = 0; j < 100; j++) {
        if (j == 10)
          break; // terminate loop if j is 10
        System.out.print(j + " ");
      }/*from  ww w . java 2s. c  o m*/
      System.out.println();
    }
    System.out.println("Loops complete.");
  }
}

This program generates the following output:

Example 5

The break that terminates a switch statement affects only that switch statement and not any enclosing loops.

 
public class Main {
  public static void main(String args[]) {
    for (int i = 0; i < 6; i++)
      switch (i) {
        case 1://from   w  w w.j a  v a 2  s  .c o  m
          System.out.println("i is one.");
          for (int j = 0; j < 5; j++) {
            System.out.println("j is " + j);
          }
          break;
        case 2:
          System.out.println("i is two.");
          break;

        default:
          System.out.println("i is greater than 3.");
      }
  }
}

The output:

From the result we can see that the break statement only exit the switch statement.

Example 6

We can specify a label for break statement and let the code logic exit to that point. The following code uses the label to make break statement exit two layers of the nested for loop.

 
public class Main {
  public static void main(String args[]) {
    outer: for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        if (j + 1 < i) {
          System.out.println();/*from w ww  .j a v a2 s.com*/
          continue outer;
        }
        System.out.print(" " + (i * j));
      }
    }
    System.out.println();
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. What is Java continue statement
  2. Syntax for Java continue statement
  3. Example - Java continue statement
  4. How to use continue and label together
Home »
  Java Tutorial »
    Java Langauge »
      Java Statement
Java if Statement
Java if else Statement
Java if else ladder statement
Java nested if statement
Java switch Statement
Java for loop
Java for each loop
Java while Loop
Java do while loop
Java break statement
Java continue statement
Java Comments
Java documentation comment(Javadoc)