Java Programming
About Lesson

Jump statements are one of the control statements in Java that are used to shift or jump the focus of program control from one place to another. They are three in number.
1. Break
2. Continue
3. Return

The goto statement which is known as jump statement in C and C++ is discontinued in java.

Break Statement:
Break statement is used to come out of a loop or switch block based on a specific condition. Break cannot be used outside any loop or switch block. To replace goto statement java is giving break with two forms: labeled and unlabeled.
Unlabelled break is used to interrupt execution of the current block and come out from it. But it can interrupt its native block only(i.e. where break is existed). In order to extend the effect of break statement we can use labelled break.

Example of unlabeled break:
{
Loop(cond){
Code inside loop;

If(cond)
{
….
Break;
}
}
Code outside loop;
….
}
The break in the above code would cause the come out its container loop block only.

Labelled Break:
In case of nested loop, break will interrupt the execution of inner loop only. There are two solutions to break outer loop:
1. Another break inside outer loop
2. Labelled break

Example of labelled break:
{
Label:
Outer Loop(cond){
Code inside outer loop;
Inner loop(cond){
Code inside inner loop;

If(cond)
{
….
Break Label;
}
}
}
Code outside loop;
….
}

//Example java program to show how labelled works
public class LabelledBreak {

public static void main(String[] args) {
int i=1;
outside:{
while(i<10)
while(true)
{
System.out.println(i+”\n”);
i++;
if(i>5)
break outside;
System.out.println(“code in inner loop\n”);
}
System.out.println(“code in outer loop”);
}
}

}

 

The Continue Statement
Continue statement is generally used inside loops only. It causes to skip the remaining statements in the loop and continues with the next iteration.
Continue also can be used labelled or unlabeled.

Example of unlabeled continue:
{
Loop(cond){
Code inside loop

If(cond)
Continue;
Code inside loop;
}
}

Example of labeled continue:
{
Label:
Outer Loop(cond){
Code in outer loop

Inner loop(cond){
Code in inner loop

If(cond)
Continue label;
Code in inner loop;
}
Code in outer loop;
}
}

//Example java program for Labelled Continue

public class LabelledContinue {

public static void main(String[] args) {
outer:
for(int i=1;i<=3;i++)
{
System.out.println();
for(int j=1;j<10;j++)
{
if(j%5==0)
continue outer;
System.out.print(j);
}

}
}

}

Try to check the output of the above program by removing label.

You cannot copy content of this page