Java - Use break statement to stop for-loop


A break statement can stop the for-loop statement.

When a break statement is executed, the control is transferred to the next statement after the for-loop statement.

The following code uses the for-loop statement to print all integers between 1 and 10 using a break statement.

public class Main {
  public static void main(String[] args) {
    for (int num = 1;; num++) { // No condition-expression
      System.out.println(num); // Print the number
      if (num == 10) {
        break; // Break out of loop when i is 10


Related Topic