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?
Java allows us to label a statement within our code with the help of below syntax :
labelName : statement
L1 : for(int i = 0; i<10; i++){ System.out.println(i); }
The purpose of labelling our statements comes into picture only in the context of using break or continue statements.
helloLabel : String str ="hello"; //compilation fails!
joe : cool : smart : System.out.println("I am Joe!");
L1 : for(int i=0; i<10; i++){ System.out.println(i); } L1 : if(i >9){ //same label name 'L1' allowed System.out.println(i); }
L1 : for(int i=0; i<10; i++){ L1 : if(i==9){ // fails to compile - L1 can't be used here System.out.println("I am 9!"); } }
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.
int i; for(i=0; i<10; i++){ if(i==2) break; System.out.println(i); } System.out.println("Yey!I came out of loop with i :"+i);
If we run the above code, it will produce the below output.
0 1 Yey!I came out of loop with i :2
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.
int i=2; myLabel : if(i==2){ System.out.println("In an labeled if statement"); break myLabel; //labeled break allowed in if statement } System.out.println("Yey!I came out of labeled if");
The above code compiles fine and produces below output.
In an labeled if statement Yey!I came out of labeled if
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.
for(int i=0;i<10;i++){ if(i==2) continue; System.out.print(i+" "); } System.out.println("\nI am finally out of loop!");
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.
0 1 3 4 5 6 7 8 9 I am finally out of loop!
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.
L1 : for(int i=0;i<3;i++){ L2 :for(int j=0;j<3;j++){ if(i==1) continue L1; System.out.println(i+":"+j); } }
The continue
statement in the above example will pass the control to the next iteration of loop L1
and will produce the below output.
0:0 0:1 0:2 2:0 2:1 2:2
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.