Which keyword is used to exit a loop prematurely in Java?

The story behind the answer

The Java keyword used to exit a loop prematurely is `break`. When Java reaches a `break` statement, it immediately leaves the nearest enclosing loop and continues with the first statement after that loop.

`break` works in `for`, `while`, and `do-while` loops, and it can also terminate a `switch` statement. In nested loops, an unlabeled `break` affects only the innermost loop. Java also supports labeled breaks, such as `break outer;`, which can exit a specifically labeled outer loop.

A common mix-up is `continue`. The `continue` keyword does not end the loop; it skips the rest of the current iteration and starts the next one. `return` is broader: it exits the entire method, possibly ending the loop as a side effect. Java also has `System.exit()`, but that terminates the running application rather than merely leaving one loop.

Source: Wikipedia · fact-checked Aug. 2026

Add question to a list

Choose a list to keep this question in: