Java break Statement | Label with Java break for loop example

Java break Statement is Used when you want to immediately terminate a Loop and the next statements will execute. It mainly used for loop, switch statement, for each loop or while Lopp, etc.

It’s introduced in Java since JDK 1.5

Java break Statement, Label with Java break for loop , while and if example

A Detailed about Java break statement in the program has the following two usages −

  • An encountered inside a loop to immediately terminated and the program control resumes at the next statement following the loop.
  • Used to terminate a case in the switch statement

This Tutorial you will learn how to use Break statement with for loop, while Loop etc. And you must know about if condition statement because it will use many examples.

Syntax

Here is simple break statement syntax in java

Flow Diagram of Java break

Here is a flowchart.

Flow Diagram of Java break how it works

Java break for loop Example

Here is the First example of how to work with the break statement in Python. In the example using for Loop and checking a condition with if statement. if i = 4 then break the loop.

Output: 0
1
2
3
Breaking the loop at 4

How to break for loop Works

You see the above example, Here we are showing How actually it’s worked.

How Java break for loop Works statement

Java break label

Using a break statement with the label you can stop any loop in Java whether it is an Outer Loop or Inner Loop. See the Java break label example code below.

Output: Parent 1
Child 1
Child 2
Child 3
Parent 2
Child 1

Java break Switch Control 

Hope you did read Java Switch statement tutorial and example, Where we used a break in Switch control statement.

If not do follow the link and check out this example.

Output: Value 2

Java Break Statement in while loop

Let’s see last one more example of Break statement in While Loop.

Output: 1
2

If you any doubts or suggestions then do comment in below.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Examples Break Statement with loops and control statement are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.