Search Placementyogi.com
 

break , continue and return in Java

There are 3 jump statements in Java

  1.  break
  2.  continue
  3.  return

break statement

The break statement is used to exit from the current block. It can be used at three places:

  1. To exit from the switch block.
  2. To exit a loop
  3. It can be used to replace the goto statement.

 

1. Using break to exit from the switch block

The switch case is completely based on break statements. 

int i=1;
switch(i)
{
	case 1: System.out.println("i=1");
			break;
	case 2: System.out.println("i=2");
			break;
	default: System.out.println("default case");
			break
}

If the case matches, the statements at executed and then break takes the control out of the switch.

If we do not use the break statement, the next case will also get executed.

2. Using break to exit a loop

The break statement when encountered inside a loop , takes the control out of the for loop and the next statement after the loop is executed.

Exiting for loop using break:

for (int i=0;i<10;i++)
{
	if(i==4)
		break;
	System.out.println("i="+i);
}
System.out.println("Outside the loop");

3. Exiting while loop using break:

int i=0;
while (i<10)
{
	if (i==4)
		break;
	System.out.println("i="+i);
	i++;
}

Output:

i=0

i=1

i=2

i=3

Outside the loop

4. Using break inside a nested loop:

When break is used inside a nested loop, the control only comes out of the inner loop.

Example:

for(int i=0;i<5;i++)
{
	for(int j=0;j<5;j++)
	{
		if (j==2)
			break;
		System.out.println("i="+i+", j="+j);
	}
}

Output:

i=0, j=0

i=0, j=1

i=1, j=0

i=1, j=1

i=2, j=0

i=2, j=1

i=3, j=0

i=3, j=1

i=4, j=0

i=4, j=1

Here, in the inner loop, when the value of j equals 2, the inner loop breaks. It does not affect the outer loop.

Try to trace the above program and understand the control flow.

5. Using break to implement goto

goto is considered unstructured and hence java does not have a goto statement.

But sometimes goto statements are needed, for example if you are inside a nested loop and want to come out of all the loops, we need goto.

To implement this we use goto with labels! (Yup! you read it right! we have labels in Java!)

 

Syntax of break with labels:

break label;

label is the name of the block of code which you want to break i.e. the control is transferred out of the named block. 

Lets demonstrate this by an example:

public class ForLoop
{
	public static void main(String[] args)
	{
		outer: for(int i=0;i<5;i++)
		{
			inner: for(int j=0;j<5;j++)
			{
				if (j==2)
				break outer;
				System.out.println("i="+i+", j="+j);
			}
			System.out.println("Outside the inner loop");
		}
		System.out.println("Outside the outer loop");
	}
}

Here, we have named the outer for loop block as "outer" and the inner for loop as "inner".

When the break is encountered inside the inner loop, the control comes out of the "outer" labeled block.

 

Important:

break label can only transfer the control out of an enclosing block.

if the label refers to another block , it throws a compile time error (undefined label).

public class ForLoop
{
	public static void main(String[] args)
	{
		first:
			for(int i=0;i<10;i++)
				System.out.println("first block");
		outer: for(int i=0;i<5;i++)
		{
			inner: for(int j=0;j<5;j++)
			{
				if (j==2)
				break first;
				System.out.println("i="+i+", j="+j);
			}
			System.out.println("Outside the inner loop");
		}
		System.out.println("Outside the outer loop");
	}
}

Output:

C:\java_practice\src\control\ForLoop.java:13: error: undefined label: first

                                break first;

                                ^

This error comes because the "first" labeled block does not enclose the break statement.

Continue statement in Java

continue is used when we have to skip the remaining portion of the code in the current iteration but continue running the loop from the next iteration.

continue shifts the control to the beginning of the block i.e. in this case the "iteration" part of the for statement.

Example:

for(int i=0;i<5;i++)
{
	if (i==2)
		continue;
	System.out.println("i="+i);
}

Output:

0

1

3

4

 

2 is not printed because as soon as i becomes true, continue is executed and the remaining portion of the code is skipped and i++ is executed and the next iteration starts executing.

1. continue inside a nested loop

When continue is used inside a nested loop, only the inner loop's current iteration is skipped and the outer loop is not affected at all.

public class ForLoop
{
	public static void main(String[] args)
	{
	for(int i=0;i<2;i++)
	{
		for(int j=0;j<5;j++)
		{
			if (j==2)
				continue;
			System.out.println("i="+i+", j="+j);
		}
	}
	}
}

Output:

i=0, j=0

i=0, j=1

i=0, j=3

i=0, j=4

i=1, j=0

i=1, j=1

i=1, j=3

i=1, j=4

Notice: j=2 is not printed in any of the iteration.

 

2. Using continue to implement goto

If we use "continue" with a label , we can make it skip the iteration of the outer loop too. 

Consider the following program:

public class ForLoop
{
	public static void main(String[] args)
	{
		outer: for(int i=0;i<3;i++)
		{
			inner: for(int j=0;j<5;j++)
			{
				if (j==2)
					continue outer;
				System.out.println("i="+i+", j="+j);
			}
			System.out.println("Outside the inner loop");
		}
		System.out.println("Outside the outer loop");
	}
}

Output:

i=0, j=0

i=0, j=1

i=1, j=0

i=1, j=1

i=2, j=0

i=2, j=1

Outside the outer loop

Notice: The current iteration in the outer for loop is skepped here i.e.

after i=0,j=1 j becomes 2 so,

i=0, j=3 is not printed, instead the next iteration where i becomes 1 is executed , Hence

i=1, j=0 is printed.

 

3. Using continue with while loop

As we know, continue skips all the remaining statements inside the current iteration and goes to the beginning of the block.

Consider the following program:

public class WhileLoop
{
	public static void main(String[] args)
	{
		int i=10;
		while(i<20) //line6
		{			
			if(i==11)
				continue;
			System.out.println("i="+i);
			i++;
		}
		System.out.println("Last statement inside main");
	}
}

Output: 

It prints i=10 and then goes into an infinite loop.

Lets look into this closely, 

i is 10 initially, then the control enters the loop.

i==11 is false, so i=10 is printed and i is incremented to 11.

Now, i is 11 and so continue is executed, it skips everything and goes to the  while statement at line6. 

Notice that i++ was not executed because of the skip, Hence the value of i is still 11.

if(i==11) again evaluates to true and continue is executed.

This goes on and so the program runs into an infinite loop! Damn!

return statement in Java

The return statement is used return the control to the caller of the method by skipping any of the remaining statements.

public class ReturnProg
{
	public static void main(String[] args)
	{
		int i=10;
		System.out.println("Inside main");
		if(i==10) 
			return;
		System.out.println("Last statement inside main");
	}
}

Output:

Inside main

 

In the above program, the control returns to the JVM from main as soon as the return statement is encountered.

The return statements are  useful when we are using methods. You will get to know more about it when we discuss methods in the later chapters.

 

Be careful with return statements:

If you write the above return statement without the condition, java throws an unreachable code error .

This happens because Java is intelligent enough to find out that if there is a return statement without any condition,the return statement will return the call to the caller methods always and so any statement after "return" will never get executed.

public class ForLoop
{
	public static void main(String[] args)
	{
		int i=10;
		System.out.println("Inside main");
		return;
		System.out.println("Last statement inside main");
	}
}

C:\java_practice\src\control\ForLoop.java:9: error: unreachable statement

                System.out.println("Last statement inside main");

 

 

 

 

 

Banner to LogicGuns.com

Hide Page Information

Break and Continue in Java