How to stop a loop in Java?

To stop a loop in Java, use the break statement.

Here's how you do it:

// Prints 1, 2, then stops the loop
for (int i = 1; i < 1000; i++) {
  if (i == 3) {
    break;
  }

  System.out.print(i + ", ");
}