In Java language there are 3 types of jump statements:
- Break Statement.
- Continue Statement.
- Return Statement.
Break Statement: Break statement is used to terminate the loops like for, while, do-while from the subsequent execution, it also terminates switch statement.
syntax
break;
Example:
// Program to demostrate break statement.
public class Learner{
public static void main(static[]args)
{
int num=5; // initializing the variable
while(num>0)
{
if(num==3)
break;
System.out.println(num);
num--;
}
}
Output:
5 4
Continue Statement: Continue statement is used to continue the next iteration of a loop, so the remaining statements are skipped within the loop.
Syntax:
continue;
Example:
// Program to demostrate continue statement.
public class Learner{
public static void main(String[]args)
{
int num=1; // initializing the variable
for(i = 0; i<= 10; i++)
{
if(i==5 || i==6)
{
System.out.println("Skiping",i);
continue;
}
System.out.println(i);
}
}
Output:
0 1 2 3 4
Skiping 5
Skiping 5
7 8 9
Return Statement: Return statement is used to end or terminate a function immediately with or without value and continues the flow of program execution from where it was called.
The function declared with void type does not return any value.
Syntax:
return;
or
return value;
Example:
// Program to demostrate return statement.
public class Learner{
public static void main(String[]args)
{
int sum = sum();
System.out.println("Sum of digits from 1 to 10 = ",sum);
}
int sum()
{
int sum = 0;
int i;
for( i = 0; i <= 10 ; i++) {
sum += i;
}
return sum;
}
Output:
Sum of digits from 1 to 10 = 55