java break

java

In Java, the break statement is used to exit a loop or switch statement early, before the loop or switch statement is completed. It terminates the current loop or switch statement and transfers control to the next statement after the loop or switch.

Here is an example of how to use the break statement in a loop in Java:

public class Main {
  public static void main(String[] args) {
    // Define a loop that counts from 1 to 10
    for (int i = 1; i <= 10; i++) {
      // Print the current count
      System.out.println(i);

      // If the count is 5, exit the loop
      if (i == 5) {
        break;
      }
    }
  }
}

This code defines a loop that counts from 1 to 10 and prints the current count to the console. If the count is 5, the break statement is executed and the loop is terminated.

The break statement can also be used to exit a labeled loop, by specifying the label of the loop before the break statement.

For more information on the break statement and how to use it in Java, you can refer to the Java documentation: