Java break statement is used to terminate the loop in between it’s processing. We use break reserve keyword for breaking out of the loop in java program.

Java break

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.

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.

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.

We can also use break statement to get out of switch-case statement, you can learn about all these in below video.

Reference: Oracle Documentation

By admin

Leave a Reply

%d bloggers like this: