Quiz Entry - updated: 2026.07.10
What do break and continue do in loops?
break jumps out of the loop completely; continue skips the rest of the current iteration and jumps to the loop's next test/update.
// break - exit loop immediately
while (i < N) {
c += a[i];
// Done, exit the loop
if (c > N) break;
i++;
}
// continue - skip rest of body, go to next iteration
for (i = 0; i < N; i++) {
// Skip odd numbers
if (i % 2) continue;
// Only even indices
c += a[i];
}
With nested loops:
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
// Only exits inner loop!
if (condition) break;
}
// Continues here after inner break
}
Tip: To break out of nested loops, use a flag variable, goto, or refactor into a function with return.