Introduction
In Java jump statements are mainly used to transfer control to another part of our program depending on the conditions. These statements are very useful from the programmer's view because these statements allow alteration of the flow of execution of the program. These statements can be used to jump directly to other statements, skip a specific statement and so on. In Java we have the following three jump statements:
- break (simple and labeled)
- continue
- return
The break statement
If we want to go out of a loop then we use a break statement. If we use a break statement in a loop then execution will continue with the immediate next statement outside the loop. After a break, all the remaining statements in the loop are skipped. The break statement can be used in a while loop, for loop, do-while loop, and in a switch case.
Syntax :
if(condition)
{
break;
}
Example :
package myclass1;
class Myclass1
{
public static void main(String args[])
{
int n = 1;
while (n<=15)
{
System.out.println("\n" + n);
n++;
if (n==7)
{
break;
}
}
}
}
Output :
The labeled break statement
We can also use the break statement with a label. The break statement is used when we want to transfer the flow of control from an inner loop to an outer loop, but when we want the flow of control to exit the outer loop then we use a labeled break statement. A labeled break statement is somehow similar to the goto statement.
Syntax :
if(condition)
break label_name;
Example :
package myclass1;
class Myclass1
{
public static void main (String args[])
{
boolean a=true;
m:
{
n:
{
o:
{
System.out.println("Before break");
if(a)
break n;
System.out.println("This will not execute");
}
}
System.out.println("After break");
}
}
}
Output :
The continue statement
The continue keyword is used mainly with loops. When we do not want to execute some statements then we use a continue statement to skip those statements to execute. If the continue statement is confronted in the program then it will start the next iteration. It does not terminate the loop, it just skips some part of the loop. The execution again starts from the top of the loop. In some ways it is similar to the break statement.
Syntax :
if(condition)
{
continue;
}
Example :
package myclass1;
class Myclass1
{
public static void main(String args[])
{
int [] id = {999, 888, 777, 666, 555};
for(int x : id )
{
if( x == 777 )
{
continue;
}
System.out.print( x );
System.out.print("\n");
}
}
}
Output :
The return statement
The return statement is the last jump statement. The return statement is used to end the execution of a specific method and then return a value. When we use a return statement in our program then it sends the program control to the method caller. The data type of the returned value should always be equal to the data type of the method's declared return value.
Syntax :
if(condition)
{
return;
}
Example :
package myclass1;
class Myclass1
{
public static void main(String[] args)
{
yahoo(true);
System.out.println("hi");
}
public static void yahoo(boolean a)
{
System.out.println("1");
if (a)
{
return;
}
System.out.println("2");
System.out.println("3");
}
}
Output :
Summary
This article has introduced you to the jump statements in Java. It also covered the syntax and the examples of these jump statements.