Core Java

Java – Break and Continue

When dealing with loops in Java, break and continue keywords are the ones we often come across.  We’ll see what they mean and how can they be used. We’ll be learning the usage of Java break and continue statements in both labelled as well as unlabelled context.

Let’s start by first understanding what is a labelled statement?

Labelled Statements :

Java allows us to label a statement within our code with the help of below syntax :

Sample usage :

The purpose of labelling our statements comes into picture only in the context of using break or continue statements.

Rules for Labelling Statements:
  1. A label name could be any valid identifier.
  2. You can never assign a label to a declaration statement.
  3. A single statement can have multiple labels.
  4. The scope of the label identifier is the statement for which it is defined. So, it is legal to redeclare another statement with the same label name outside the scope of the first statement.
  5. Any attempt  to reuse the same label identifier within the scope of the statement it refers will fail to compile.

break statement :

break statement helps move out of the current context by transferring the control to the statement immediately after it.

Unlabelled break statement : The unlabelled break statement can be used in loops : for, enhanced- for, while, do-while and switch statements.

If we run the above code, it will produce the below output.

It clearly is evident that as soon as i became 2 , the control came out of the loop and executed the next statement.

Labelled break statement : A labelled break statement can be used to move out of context of any labelled statement.

The above code compiles fine and produces below output.

continue statement :

continue keyword in Java is used to immediately transfer control to the next iteration of the current loop.Therefore, it can be used only within looping statements – for, enhanced for, while and do-while.

Unlabelled continue statement : Immediately transfers the control to the next iteration of the current loop in context.

In the above code, when continue is called for i==2, the control moves immediately to the next iteration with i=3, thereby printing the below output.

Labelled continue statement : Moves the control to the next iteration of the labelled loop. Definitely, this continue statement must be present within the scope of that labelled loop.

Such a feature comes handy when dealing with nested loops.

The continue statement in the above example will pass the control to the next iteration of loop L1 and will produce the below output.

Conclusion :

In this post, we looked at Java break and continue statements. They are the important keywords which comes handy in controlling the flow of execution.

break statement moves the control out of the its current scope. Labelled break statement can be used to move out of context of any labelled statement. The unlabelled break statement can be used only within loops and switch statements.

continue statement transfer the control to the next iteration of the loop in context. A labelled continue can be used only within its scope ( i.e. the scope of a labelled loop )  and helps move it to its next iteration.

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *