Master C++ Loop Control: When to Use break vs continue
This article explains C++ loop control statements, detailing the syntax, use cases, and differences of break and continue, and provides multiple code examples demonstrating how to exit loops early or skip iterations for tasks such as searching and data filtering.
In C++ programming, loop control statements such as for, while, and do‑while are essential for repeating tasks. Sometimes you need to exit a loop early or skip the current iteration, which is done with break and continue statements.
1. break statement: early termination of loop
The break statement immediately exits the current loop, skipping any remaining statements in the loop body and continuing execution after the loop.
Syntax
break;Applicable scenarios
Terminate the loop when a certain condition is met (e.g., target value found).
Prevent fall‑through in a switch‑case structure.
Example 1: Using break in a for loop
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit when i=5
}
cout << i << " ";
}
return 0;
}Output: 1 2 3 4
Explanation: When i equals 5, break stops the loop, so 5 and later numbers are not printed.
Example 2: Using break in a while loop
#include <iostream>
using namespace std;
int main() {
int num = 1;
while (num <= 10) {
if (num == 7) {
break; // Exit when num=7
}
cout << num << " ";
num++;
}
return 0;
}Output: 1 2 3 4 5 6
2. continue statement: skip current iteration
The continue statement skips the remaining part of the current loop iteration and proceeds directly to the next iteration.
Syntax
continue;Applicable scenarios
Skip the current iteration when certain conditions are met (e.g., filter data).
Avoid unnecessary calculations inside a loop.
Example 1: Using continue in a for loop
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
cout << i << " ";
}
return 0;
}Output: 1 3 5 7 9
Explanation: When i is even, continue bypasses the cout statement, so only odd numbers are printed.
Example 2: Using continue in a while loop
#include <iostream>
using namespace std;
int main() {
int num = 0;
while (num < 10) {
num++;
if (num == 5) {
continue; // Skip num=5
}
cout << num << " ";
}
return 0;
}Output: 1 2 3 4 6 7 8 9 10
Note: Ensure the loop variable is updated; otherwise, an infinite loop may occur.
3. Difference between break and continue
Effect: break terminates the entire loop; continue only skips the current iteration.
Use case: break for early exit, continue for filtering data.
Loop behavior: break ends the loop, continue keeps the loop running.
4. Practical application scenarios
(1) Using break when searching data
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
int target = 30;
for (int i = 0; i < 5; i++) {
if (arr[i] == target) {
cout << "Found target " << target << ", index: " << i << endl;
break; // Exit after finding
}
}
return 0;
}(2) Using continue to filter data
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 3 || i == 7) {
continue; // Skip 3 and 7
}
cout << i << " ";
}
return 0;
}Output: 1 2 4 5 6 8 9 10
Conclusion
break: Forces exit of the whole loop, suitable for early termination. continue: Skips the current iteration, suitable for filtering data.
Key difference: break stops the loop, continue only skips the current step.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
