Loops repeat work, but sometimes you can decide earlier than your loop’s end whether more work is needed. Two simple tools help:
break— stop the loop immediately (exit the nearest loop).continue— skip the rest of this iteration and move on to the next round.
These are especially handy in mathematical code where a condition tells you you’re done (first divisor found, threshold reached) or where some cases are irrelevant (skip evens, skip multiples of 3).
We’ll use only concepts we’ve covered: variables, arithmetic, comparisons, if, classic for, and basic console I/O.
break: exit as soon as you know the answer
Example 1 — Primality test (stop at the first divisor)
#include <iostream>
int main() {
int n;
std::cout << "Enter n (>= 2): ";
std::cin >> n;
bool is_prime = true;
// Try divisors up to sqrt(n). Exit early if one divides n.
for (int d = 2; d * d <= n; d = d + 1) {
if (n % d == 0) {
is_prime = false;
break; // Found a divisor -> no need to test further
}
}
if (is_prime) {
std::cout << n << " is prime\n";
} else {
std::cout << n << " is composite\n";
}
return 0;
}
Why break helps: once a divisor is found, more tests can’t change the answer. Early exit saves time.
Example 2 — Smallest k with \(1 + 2 + \dots + k \ge T\)
We accumulate triangular numbers until we reach a threshold T, then stop.
#include <iostream>
int main() {
int T;
std::cout << "Threshold T: ";
std::cin >> T;
int sum = 0;
int k = 0;
for (int i = 1; ; i = i + 1) { // we'll exit using break
sum = sum + i;
k = i;
if (sum >= T) {
break; // first k where the sum reaches/exceeds T
}
}
std::cout << "Smallest k with 1+...+k >= " << T << " is " << k << '\n';
return 0;
}
Here we used a for with an empty condition on purpose. The loop is controlled by the break once the mathematical condition is met.
continue: skip uninteresting cases, keep looping
Example 3 — Sum of odd squares up to (N)
We only want odd indices. continue skips the even ones.
#include <iostream>
int main() {
int N;
std::cout << "Sum of odd squares up to N. Enter N: ";
std::cin >> N;
int sum = 0;
for (int i = 1; i <= N; i = i + 1) {
if (i % 2 == 0) {
continue; // skip even i
}
sum = sum + i * i; // only odd i reach here
}
std::cout << "Sum = " << sum << '\n';
return 0;
}
Example 4 — Partial harmonic sum skipping multiples of 3
\[
S_N = \sum_{\substack{k=1 \ k \not\equiv 0 \pmod{3}}}^{N} \frac{1}{k}
\]
#include <iostream>
int main() {
int N;
std::cout << "Harmonic sum excluding multiples of 3. Enter N: ";
std::cin >> N;
double sum = 0.0;
for (int k = 1; k <= N; k = k + 1) {
if (k % 3 == 0) {
continue; // skip k divisible by 3
}
sum = sum + 1.0 / k;
}
std::cout << "Sum = " << sum << '\n';
return 0;
}
continue keeps the loop structure simple: the “normal work” lives after the guard.
How control flows (for the classic for loop)
- On
continue: the program jumps to the step, then checks the condition for the next round. - On
break: the program leaves the loop immediately and resumes after the closing brace. - Both only affect the nearest enclosing loop.
When to use which
- Use
breakwhen you’re searching for the first match or stopping by a threshold (first divisor, first time a sum exceeds (T), first power exceeding a limit). - Use
continuewhen many iterations are “noise” and you want to filter them out early (skip evens, skip non-multiples, skip invalid inputs).
Common pitfalls
- Code after a
breakinside the same block won’t run—make sure any needed updates happen beforebreak. - With
continue, remember the step still runs (e.g.,i = i + 1) before the next check. - Avoid stacking many unrelated
continuechecks; prefer a single clear guard if possible.
Key takeaways
breakexits the loop immediately;continueskips the rest of the current iteration.- They simplify math-driven loops: stop when sufficient, skip when irrelevant.
- Keep conditions near the top of the loop so the “main work” stays uncluttered and readable.
Next, we’ll move on to range-based for loops and then to nested loops—both build on this foundation.
