The Break Statement
Break leaves a loop, even if the condition for its end is not fulfilled. It can be used to end an
infinite loop,
or to force it to end before its natural end.
For example, let's stop the countdown before its natural end:
/ break loop example
#include <iostream.h>
using namespace std;
int main ()
{
for (int n=10; n>0; n--)
{
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";
break;
}
}
}
The continue statement
The continue statement causes the program to skip the rest of the loop in the current iteration,
as if the end of the statement block had been reached, causing it to jump to the start of the following
iteration.
For example, let's skip number 5 in our countdown:
continue loop example
#include <iostream>
using namespace std;
int main ()
{
for (int n=10; n>0; n--)
if (n==5)
{
continue;
cout << n << ", ";
}
cout << "liftoff!\n";
}
Difference
Both “break” and “continue” are the ‘jump’ statements, that transfer control of the program to another
part of the program. C++ supports four jump statements namely ‘return’, ‘goto’, ‘break’ and ‘continue’.
The main difference between break and continue is that break
is used for immediate termination of loop whereas, continue terminate current iteration and resumes
the control to the loop.
break
break is used to break or terminate a loop whenever we want.
Just type break; after the statement after which you want to break the loop. As simple as that!
Consider an example.
#include <iostream>
int main(){
using namespace std;
int n;
for(n = 1; n <= 5; n++){
cout << "*" << endl;
if(n == 2){
break;
}
}
return 0;
}
In the first iteration of the loop in the above example, '*' gets printed and the condition ( n == 2 )
is checked. Since the value of n is 1, therefore the condition becomes false and n++ increases the
value of 'n' to 2. Again '*' gets printed and since this time the condition of if satisfies, the break statement terminates the loop.
#include <iostream>
int main()
{
int x;
for( ; ; ){
cout << "Enter 0 to stop" << endl;
cin >> x;
if (x == 0){
break;
}
}
return 0;
}
This is an infinite loop. To terminate this we are using break.
If a user enters 0 then the condition of if will get satisfied and the break statement will terminate the loop.
continue
continue statement works similar to break statement. The only difference is that break statement terminates the loop whereas continue statement passes control to the conditional test i.e., where the condition is checked.
In short, it passes control to the nearest conditional test in do...while loop, or the condition of while in while loop,
or the condition of for in for statements skipping the rest of the statements in the loop.
#include <iostream>
int main(){
using namespace std;
int n = 1;
while(n < 10){
if (n == 5){
n = n + 1;
continue;
}
cout << "n = " << n << endl;
n++;
}
return 0;
In the above example, notice that n = 5 is not printed because as the value of 'n' becomes 5,
the if condition becomes true and the statements in the body of the if statements get executed. Thus n = n+1 increases the value of 'n' to 6 and continue statement passes the control to the test condition and rest of the statements are not executed ( cout << "n = " << n << endl; n++; - these two statements are not executed when n is 5 ). Then again the iteration starts and the numbers get printed from 6 onwards
}