chevron_left
354 points
12 7 5

We oftentimes encounter break and continue statements while working with loops in Java. It seems perplexing especially with continue statement.

In this article let us clearly distinguish between break and continue:

Using break statement:

In Java break statement can be used to terminate a case in a switch statement and to exit a loop. When a break statement is encountered in a loop, it abruptly terminates the loop unexecuting the remaining code and the control resumes to the next statement after the loop.

  • When we use the break statement inside a set of nested loops. It will break out only from the inner loop.
  • More than one break statement may appear inside a loop

Using continue statement:

In a scenario, where you want to skip the execution of set of statements for a specific condition but not the loop. Here's where continue statement is encompassed.

  • In case of while, do while loops, the continue statement causes control to directly transfer to the conditional expression that controls the loop.
  • In case of for loop control first shifts to the iteration and then to the conditional expression. Howsoever for all three loops the in-between code is bypassed.

Let us consider an example:

package simple.examples;

public class BreakContinue {

public static void main(String[] args) {
	
	for(int i=1;i<=10;i++)
	{
		System.out.println(i+ " Before break");
		if(i==5)break;
		System.out.println(i +" After break");
	}
	System.out.println("*************************");
	for(int i=1;i<=10;i++)
	{
		System.out.println(i+ " Before continue");
		if(i==5)continue;
		System.out.println(i+ " After continue");
	}

}

}

In the preceding example, declared a for loop for 10 iterations

  1. first for loop with break statement
    As discussed, for i= 1 to 4 iterations both the prints are executed. As soon as the condition is true and break statement is encountered, the loop terminates immediately.
  2. second for loop with continue statement
    Here, only for the condition i=5, code after the condition is bypassed and continues with the remaining iterations.

The output of this code gives us more clarity

1 Before break
1 After break
2 Before break
2 After break
3 Before break
3 After break
4 Before break
4 After break
5 Before break
****
1 Before continue
1 After continue
2 Before continue
2 After continue
3 Before continue
3 After continue
4 Before continue
4 After continue
5 Before continue
print statement 5 After continue missing, this is bypassed*
6 Before continue
6 After continue
7 Before continue
7 After continue
8 Before continue
8 After continue
9 Before continue
9 After continue
10 Before continue
10 After continue

Hope this helps!!

More Posts

Difference between break and continue in PHP abdullah_yasir - Oct 23, 2020
Continue statement in Java Yogesh Sharma - Aug 22
Cases for Continue statement in Java Yogesh Sharma - Aug 22
Break Statement in Java Yogesh Sharma - Aug 22
How to break out of For Loop java MallikaShaik672 - Oct 23, 2020
terminate a loop in java DivyaN - Aug 16
exit from for loop in java DivyaN - Aug 16
exit a while loop java DivyaN - Aug 15
[Solved] syntaxerror: 'break' outside loop IamAdinath - Jan 24
Difference between break and continue in java Mubashir Islam - Oct 21, 2020