Java Tutorial

Basic Concepts

Flow Control Statements

Object Oriented Concepts

Coming Tutorials

>Home>Java Tutorial>Jumping Statements Break, Continue, Return in Java

Java Tutorial

Basic Concepts

Flow Control Statements

Object Oriented Concepts

Coming Tutorials



The Java jumping statements are the control statements which transfer the program execution control to a specific statement. Java has three types of jumping statements break, continue and return. Labelled break, labelled continue, labelled loops.

Java Break, Continue, Return Statements, Labelled Loops Examples

The jumping statements are the control statements which transfer the program execution control to a specific statements.

Java has three types of jumping statements they are break, continue, and return. These statements transfer execution control to another part of the program.


Java Break Statement

We can use break statement in the following cases.

Inside the switch case to come out of the switch block.

Within the loops to break the loop execution based on some condition.

Inside labelled blocks to break that block execution based on some condition.

The break cannot be used outside the loops and switch statement.


Example: Invalid, break without switch or loop


class Test
{
public static void main(String[] args)
{
int i=5;
if(i==5)
{
break; // error: break outside switch or loop
}
}
}

Example: Valid, break inside a loop


class Test
{
public static void main(String[] args)
{
for(int j=0; j<10; j++)
{
if(j==5)
{
break;
}
System.out.println(j);
}
System.out.println("outside of for loop");
}
}

Output:-


0
1
2
3
4
outside of for loop

Java Continue Jumping Statement

This statement is used only within looping statements.

When the continue statement is encountered, then it skip the current iteration and the next iteration starts.

The remaining statements in the loop are skipped. The execution starts from the top of loop again.

We can use continue statement to skip current iteration and continue the next iteration inside loops.


Example: To print odd numbers.


class Test
{
public static void main(String[] args)
{
for(int j=1; j<=100; j++)
{
if(j%2==0)
{
continue;
}
System.out.println(j);
}
}
}

Output:-


1
3
5
.
.
99

Note: The continue statement cannot be used outside the loop.


Java Labelled Break and Continue

In the case of nested loops to break and continue a particular loop we should go for labelled break and continue statements. The Java labelled loops allows transferring to a particular line or statement.


Example: Labelled Break Statement


class Test
{
public static void main(String[] args)
{
out:
for(int i=1; i<=100; i++)
{
  System.out.println("outer");
  for(int j=1; j<=100; j++)
  {
    System.out.println("nested");
    if(j==2)
    {
    // break; this will exit from inner for loop only
    break out; // this will exit from both for loops
    }
  }
}
}
}

Output:-


outer
nested
nested

Example: Labelled Continue Statement


class Test
{
public static void main(String[] args)
{
out:
for(int i=1; i<=100; i++)
{
  System.out.println("outer");
  for(int j=1; j<=100; j++)
  {
    System.out.println("nested");
    if(j==2)
    {
    // continue; this will skip second(j==2) iteration of inner for loop only
    continue out; // this will skip current iteration of both for loops
    }
  }
}
}
}

Output:- The outer for loop will iterate 100 times but the inner for loop will iterate twice each time.


outer
nested
nested
outer
nested
nested
.
.
.
outer
nested
nested

Java Return Jumping Statement

The return statement is mainly used in methods in order to terminate a method in between and return back to the caller method. It is an optional statement. That is, even if a method doesn't include a return statement, control returns back to the caller method after execution of the method. Return statement may or may not return parameters to the caller method.


Example: Use of return statement


class Test
{
public static void main(String[] args)
{
Test t = new Test();
int sum = t.addition(10,20);  //addition() method return integer value
System.out.println("Sum = "+sum);
t.show("Devavrat");  //show() method does not return any value 
}
int addition(int a,int b)
{
return a+b;
}
void show(String name)
{
System.out.println("Welcome "+name);
return; // not returning anything, it is optional
}
}

Output:-


Sum = 30
Welcome Devavrat

Share the article to help your friends