Break and Continue Statements in C++
Break Statement in C++
The break statement is used to immediately terminate the loop when a specific condition becomes true.
When the break statement executes, the control comes out of the loop and moves to the next statement after the loop.
Syntax of Break Statement:
break;
How Break Statement Works?
Algo of break statement
- The loop starts execution normally.
- The condition inside the loop is checked.
- If the break statement is encountered, the loop stops immediately.
- The control moves to the statement written after the loop.
Note: The break statement is mostly used when we want to stop the loop early based on a condition.
Program 1: Stop Loop at Number 5
#include <iostream>
using namespace std;
int main() {
int i = 1;
while(i <= 10)
{
if(i == 5)
{
break;
}
cout << i << " ";
i++;
}
return 0;
}
Output:
1 2 3 4
Explanation:
- The loop starts from i = 1.
- The loop continues while i <= 10.
- Numbers are printed one by one.
- When i becomes 5, the condition (i == 5) becomes true.
- The break statement executes immediately.
- The loop stops and control comes outside the loop.
- Therefore, only numbers from 1 to 4 are printed.
Program 2: Search a Number
#include <iostream>
using namespace std;
int main() {
int i = 1;
while(i <= 10)
{
if(i == 7)
{
cout << "Number Found";
break;
}
else
{
cout << i << endl;
}
i++;
}
return 0;
}
Output:
1
2
3
4
5
6
Number Found
Explanation:
- The loop starts from i = 1.
- The loop checks numbers one by one.
- If the value of i is not equal to 7, the number is printed using the else block.
- So, numbers 1 to 6 are displayed on the screen.
- When i becomes 7, the condition (i == 7) becomes true.
- The message "Number Found" is displayed.
- After that, the break statement immediately stops the loop.
Summary:
- Break statement is used to terminate the loop immediately.
- It is mostly used with conditions inside loops.
- After break executes, control comes outside the loop.
Continue Statement in C++
The continue statement is used to skip the current iteration of a loop and move to the next iteration.
When the continue statement executes, the remaining statements inside the loop are skipped for that iteration.
Syntax of Continue Statement:
continue;
How Continue Statement Works?
Algo of continue statement
- The loop starts execution normally.
- The condition inside the loop is checked.
- If the continue statement executes, the remaining code of that iteration is skipped.
- The loop directly moves to the next iteration.
Note: Continue does not stop the loop. It only skips the current iteration.
Program 1: Skip Number 5
#include <iostream>
using namespace std;
int main() {
for(int i = 1; i <= 10; i++)
{
if(i == 5)
{
continue;
}
cout << i << " ";
}
return 0;
}
Output:
1 2 3 4 6 7 8 9 10
Explanation:
- The for loop starts from i = 1.
- The loop runs while i <= 10.
- In every iteration, the value of i increases by 1 using i++.
- When i becomes 5, the condition (i == 5) becomes true.
- The continue statement executes.
- The current iteration is skipped immediately.
- Therefore, number 5 is not printed.
- The loop continues normally for the remaining numbers.
Program 2: Skip Vowels
#include <iostream>
using namespace std;
int main() {
for(char ch = 'A'; ch <= 'E'; ch++)
{
if(ch == 'A' || ch == 'E')
{
continue;
}
cout << ch << " ";
}
return 0;
}
Output:
B C D
Explanation:
- The for loop starts from character 'A'.
- The loop runs while ch <= 'E'.
- In every iteration, the character value increases automatically using ch++.
- When the character is 'A' or 'E', the condition becomes true.
- The continue statement executes.
- That iteration is skipped immediately.
- Therefore, only B C D are printed.
Summary:
- Continue statement skips the current iteration.
- It does not terminate the loop.
- Control directly moves to the next iteration.