Tutorial

Java break statement, label

Published on August 3, 2022
Default avatar

By Pankaj

Java break statement, label

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Java break statement is used to terminate the loop in between it’s processing. We use break reserve keyword for breaking out of the loop in java program.

Java break

There are two forms of break statement - unlabeled and labeled. Mostly break statement is used to terminate a loop based on some condition, for example break the processing if exit command is reached. Unlabeled break statement is used to terminate the loop containing it and can be used with switch, for, while and do-while loops.

break in java example

Here is an example showing java break statement usage in for loop, while loop and do-while loop.

package com.journaldev.util;

package com.journaldev.util;

public class JavaBreak {

	public static void main(String[] args) {
		String[] arr = { "A", "E", "I", "O", "U" };

		// find O in the array using for loop
		for (int len = 0; len < arr.length; len++) {
			if (arr[len].equals("O")) {
				System.out.println("Array contains 'O' at index: " + len);
				// break the loop as we found what we are looking for
				break;
			}
		}

		// use of break in while loop
		int len = 0;
		while (len < arr.length) {
			if (arr[len].equals("E")) {
				System.out.println("Array contains 'E' at index: " + len);
				// break the while loop as we found what we are looking for
				break;
			}
			len++;
		}

		len = 0;
		// use of break in do-while loop
		do {
			if (arr[len].equals("U")) {
				System.out.println("Array contains 'U' at index: " + len);
				// break the while loop as we found what we are looking for
				break;
			}
			len++;
		} while (len < arr.length);
	}

}

java break statement, break in java Note that if we remove break statement, there won’t be any difference in the output of the program. For small iterations like in this example, there is not much of a performance benefit. But if the iterator size is huge, then it can save a lot of processing time.

Java break label

Labeled break statement is used to terminate the outer loop, the loop should be labeled for it to work. Here is an example showing java break label statement usage.

package com.journaldev.util;

public class JavaBreakLabel {

	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] > 10) {
					found = true;
					// using break label to terminate outer statements
					break searchint;
				}
			}
		}
		if (found)
			System.out.println("First int greater than 10 is found at index: [" + row + "," + col + "]");
	}

}

java break label We can also use break statement to get out of switch-case statement, you can learn about all these in below video. https://www.youtube.com/watch?v=K148NXHD-UM Reference: Oracle Documentation

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
April 9, 2019

Pankaj! more like Spankaj. Bravo!!

- Rob Stein

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    November 17, 2018

    Thanks for sharing… Very helpful

    - Rizka andari

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      March 6, 2016

      on of the best java learning site , for beginner and professional. Thank u Pankaj.

      - Ashok Reddy

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        December 23, 2015

        Hi, I don’t get what searchint: does / is. Does it create some kind of loop? I didn’t know this syntax. Thanks

        - Emanuele

          Try DigitalOcean for free

          Click below to sign up and get $200 of credit to try our products over 60 days!

          Sign up

          Join the Tech Talk
          Success! Thank you! Please check your email for further details.

          Please complete your information!

          Get our biweekly newsletter

          Sign up for Infrastructure as a Newsletter.

          Hollie's Hub for Good

          Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

          Become a contributor

          Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

          Welcome to the developer cloud

          DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

          Learn more
          DigitalOcean Cloud Control Panel