Both “break” and “continue” are the ‘jump’ statements, that transfer control of the program to another part of the program. The main difference between break and continue is that break is used for immediate termination of loop. On the other hand, ‘continue’ terminate the current iteration and resumes the control to the next iteration of the loop.
The break statement is primarily used as the exit statement, which helps in escaping from the current block or loop. Conversely, the continue statement helps in jumping from the current loop iteration to the next loop.
C++ supports four jump statements, namely ‘return’, ‘goto’, ‘break’ and ‘continue’. Java supports three jump statements ‘break’ ‘continue’ and ‘return’. Let’s study the difference between break and continue in the context of C++ and Java.
Content: Break Vs Continue
Comparison Chart
Basis for Comparison | break | continue |
---|---|---|
Task | It terminates the execution of remaining iteration of the loop. | It terminates only the current iteration of the loop. |
Control after break/continue | 'break' resumes the control of the program to the end of loop enclosing that 'break'. | 'continue' resumes the control of the program to the next iteration of that loop enclosing 'continue'. |
Causes | It causes early termination of loop. | It causes early execution of the next iteration. |
Continuation | 'break' stops the continuation of loop. | 'continue' do not stops the continuation of loop, it only stops the current iteration. |
Other uses | 'break' can be used with 'switch', 'label'. | 'continue' can not be executed with 'switch' and 'labels'. |
Definition of Break
In C++ break has only two uses, i.e. first it is used to “terminate the execution of a case in switch statement”. Second, to “terminate the loop and resume the control to the next statement following the loop”.
But in Java break has three uses first, it terminates the case in the switch, second to terminate the loop enclosing break and third, as mentioned earlier that Java does not provide a ‘goto’ statement but, the extended form break can be used in place of ‘goto’.
Break statement in C++
Let us see each use of ‘break’ statement in detail. First, use to terminate the execution of case in a switch. The ‘break’ in switch only affects that switch which encloses it. It does not affect the loop enclosing the switch.
//In context to C++. //using break in switch in context of C++. switch( integer expression) { case 1: ..... ..... break; case 2: ..... ..... break; case 3: ..... ..... break; default: ....... ....... }
Break Statement in Java
The second use, to forcibly terminate the loop and resume at next statement of the loop. As soon as, the break statement is encountered inside the loop, that loop is immediately terminated without executing the remaining code in the body of that loop, and the program control reaches to next statement written after that loop.
//using break to exit the loop in context to Java class main_class{ public static void main(string args[]){ for(int i=0; i<100;i++) { system.out.println( " i: " +i ); if(i==10)break; // as the condition inside 'if' satistfies control comes out of 'for' loop. } system.out.println( " Loop breaks here" ); } }
‘break’ only let the program exit the loop which encloses it. If the break is used with a nested loop, it breaks out only the innermost loop and does not affect the outer loop.
Label in Java
The third use, using the break as the form of goto. Java uses ‘break’ as a form of goto without its problem. As goto provides an unstructured branching which is hard to understand and also goto prohibits compiler optimization.
Java uses an expanded form of break which is similar as goto and helps to exit more than one block at a time and resume the control of the program to the end of labeled block, mentioned with the break statement.
break label;
Here,” label” is the name of a specific block of code or an identifier in Java, and this labelled block must enclose the break statement. The labelled break statement is used to exit from the set of nested loop. As the labelled break statement is encountered, the control exits the labelled block, which is mentioned with the break statement.
Let’s see it practically.
//using break as form of goto in context to Java class main_class{ public static void main(string args[]){ boolean t=true; first{ second{ third{ system.out.println( " this third block" ); if (t) break first; } system.out.println( " this second block" ); } system.out.println( " this first block" ); } system.out.println( " this main block" ); } } //output : this is third block this is main block
In this code, as the control reaches the third block, its print statement gets executed. Thereafter, the control resumes to the ‘if’ statement, and as it is satisfied, the break statement with the label is executed. Label attached with a break is first, so control resume to the statement written after the end of the block first, i.e. in the main block which is a print statement, and that gets executed.
Definition of Continue
As break terminates the remaining iteration of the loop and lets the control exits the loop. Here, continue works somewhat as a break. The continue statement stops the execution of remaining code in the loop for that current iteration and resumes the control to the next iteration of that loop.
The continue statement skips the code for the current iteration and passes the control to the next iteration of the loop.
Continue Statement in C++
Let’s understand the use of ‘continue’ statement in C++ with an example.
// using continue in context to C++ summation=0; cin>>number; while (cin) { if (num<0) cout<<"Negative number found in the data"<<endl; cin>>num; continue; } summation = summation + number; cin>>number; }
The effect of continue statement is better with ‘for’ loop. Whereas, in case of while and do..while the continue statement probably executes update statement but it is not assured.
Continue Statement in Java
Let’s understand the use of ‘continue’ statement in Java with an example.
// using continue in context to Java. //it prints the even number till 100. class main_class{ public static void main(string args[]){ for( int i=0 ; i<=100 ; i++ ){ if( i%2 !=0 ) continue; // continue resume the control to the next iteration in for loop system.out.println( " i : " +i ); } } }
In the above program, If the ‘if’ statement is satisfied then, continue statement executes, which do not allow the following print statement to execute and resume the control to the next iteration of ‘for’ loop.
If i=1 then the ‘if’ condition is satisfied and continue gets executed, without executing the print statement for the odd value of ‘i’ and, the control resumes to the next iteration of the loop ‘for i=2’. If i=2 then ‘if’ statement is not satisfied. Hence, “continue” does not execute and print statement prints the value of ‘i’ which is even.
Key Differences Between Break and Continue
- Basically, break keyword terminates the rest of remaining iterations of the loop. On the contrary, the continue keyword terminates only the current iteration of the loop.
- Once the break keyword executes, the control of the program exit out of the loop and resumes to the next statement after the loop. In case of continue keyword, the control of the program resumes to the next iteration of the loop.
- As the above step concludes, that after the execution of break control of the program exit out of the loop it is clearly understood that break causes early termination of any loop. On the other hand, continue only terminate the current iteration and resume to next iteration of the loop then we can say that continue causes early execution of the next iteration of the loop.
- The break keyword terminates all the remaining iteration after its execution so we can say that it stops the continuation of the loop. As against, the continue keyword still continues the loop execution.
- The break keyword can be used along with the “switch” statements as well as “label”. Conversely, continue keyword can not be used with “switch” and “label”.
Conclusion
The break and continue statement both are the jump statement that transfers the control to another part of the program. Where break statement let the control exit the loop, the continue statement let the control to the next iteration of that loop.
Hrituparna Sinha says
I think the explanation is best ……….it provides a good writing way and an understanding concept.
Thanks
For providing
Harsh says
very nice concept through step by step explanation
Broccoli_Han says
Great article