Loading Posts...

Java Break Statement Example | Break in Java Tutorial

Java Break Statement Example | Break in Java Tutorial

Java Break Statement Example | Break in Java Tutorial is today’s topic. When a break statement is seen inside the loop, the loop is immediately terminated, and the program control resumes at the next statement, which is following the loop. It can be used to terminate the case in the switch statement. Until now, we have already seen the while and do-while loop in Java.

Java Break Statement

When a break statement is seen inside any loop, a loop is immediately terminated, and that program control resumes at the next statement which is following the loop.

The Java break is used to break the loop or switch statement. It breaks the current flow of a program at the specified condition. In the case of the inner loop, it breaks the only inner loop.

#Syntax

The syntax of a break is the single statement inside any loop is following.

break;

See the following example with for loop.

public class Conditionals {

  public static void main(String[] args) {

    int arr[] = { 1, 2, 3, 4, 5 };
    for (int i = 0; i < arr.length - 1; i++) {
      if (arr[i] == 5) {
        // breaking the loop
        break;
      }
      System.out.println(i);
    }
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
0
1
2
3
➜  java

#Java Break Statement with Inner Loop

It breaks the inner loop only if you use the break statement inside an inner loop.

See the following code example.

public class Conditionals {

  public static void main(String[] args) {

    for (int i = 2; i <= 5; i++) {
      // inner loop
      for (int j = 2; j <= 5; j++) {
        if (i == 3 && j ==3) {
          // using break statement inside the inner loop
          break;
        }
        System.out.println(i + " " + j);
      }
    }
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
2 2
2 3
2 4
2 5
3 2
4 2
4 3
4 4
4 5
5 2
5 3
5 4
5 5
➜  java

#Java Break Statement in while loop

The while statement evaluates an expression, which must return the boolean value. 

See the following code.

public class Conditionals {

  public static void main(String[] args) {

    int i = 5;  
    while (i <= 11) {
      System.out.println(i);
      if (i == 9) {
        break;
      }
      i++;
    }
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
5
6
7
8
9
➜  java

#Java Break Statement in do-while loop

Java do-while loop is used to execute a block of statements continuously until the given condition is false.

See the following code example.

public class Conditionals {

  public static void main(String[] args) {

    int m = 0;
    do {
      System.out.println(m);
      if (m == 6)
        break;
      m++;
    } while (m < 10);
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
0
1
2
3
4
5
6
➜  java

#Java break label

The labeled break statement is used to terminate the outer loop, and the loop should be labeled for it to work.

See the following code example.

public class Conditionals {
  public static void main(String[] args) {
    int[][] arr = { { 1, 2 }, { 3, 4 }, { 9, 10 }, { 11, 12 } };
    boolean found = false;
    int row = 0;
    int col = 0;
    // find index of first int greater than 10
    searchint:

    for (row = 0; row < arr.length; row++) {
      for (col = 0; col < arr[row].length; col++) {
        if (arr[row][col] > 11) {
          found = true;
          // using break label to terminate outer statements
          break searchint;
        }
      }
    }
    if (found)
      System.out.println("First int 11 is found at index: [" + row + "," + col + "]");
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
First int 11 is found at index: [3,1]
➜  java

In the above code, we are searching the character at a particular row and column in the array.

So, We use break reserve keyword for breaking out of the loop in Java program.

Finally, Java Break Statement Example | Break in Java Tutorial is over.

The post Java Break Statement Example | Break in Java Tutorial appeared first on AppDividend.

Get Free Email Updates!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailChimp ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.

user

The author didnt add any Information to his profile yet

Loading Posts...