Tutorials  /  JavaScript

Java break statement, label - Tutorial

Ccentron Redaktion · April 2024 ·2 min read ·JavaScript, Tutorial

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.

Java Break in Loops

Go
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);
    }
}

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.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

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.

Go
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 + "]");
    }
}
Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren