C break & continue
In this page:
break; // leave the loop immediately
continue; // skip to the next iteration
The break Statement in Loops
break immediately exits the loop it's inside, skipping any remaining iterations entirely -- execution jumps straight to the first statement after the loop, regardless of what the loop's original condition would have done.
Example: The break Statement in Loops
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 1; i <= 10; i++) {
// Check whether i == 5
if (i == 5) {
// Leave the switch/loop
break;
}
// Print formatted text to the screen
printf("%d ", i);
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
The continue Statement in Loops
continue skips only the rest of the current iteration's body, then jumps straight to the loop's update step and condition check to begin the next iteration -- unlike break, the loop itself keeps running.
Example: The continue Statement in Loops
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 1; i <= 5; i++) {
// Check whether i == 3
if (i == 3) {
// Skip the rest of this iteration
continue;
}
// Print formatted text to the screen
printf("%d ", i);
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Using break with while Loops
Inside a while loop, break is commonly used to exit as soon as a specific condition is detected mid-loop, giving you a clean way to stop early instead of needing to redesign the loop's main condition around that case.
Example: Using break with while Loops
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare i and set it to 1
int i = 1;
// Repeat while i <= 10 is true
while (i <= 10) {
// Check whether i == 4
if (i == 4) {
// Leave the switch/loop
break;
}
// Print formatted text to the screen
printf("%d ", i);
i++;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Using continue with while Loops
When using continue inside a while loop, you must make sure the loop's counter or condition variable is updated before the continue statement runs, or you risk creating an infinite loop that keeps skipping the update step.
Example: Using continue with while Loops
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare i and set it to 0
int i = 0;
// Repeat while i < 5 is true
while (i < 5) {
i++;
// Check whether i == 3
if (i == 3) {
// Skip the rest of this iteration
continue;
}
// Print formatted text to the screen
printf("%d ", i);
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested Loop Breaks
break only exits the single loop it's physically written inside -- if that loop is nested inside another one, the outer loop is completely unaffected and keeps running its own iterations normally.
Example: Nested Loop Breaks
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 0; i < 3; i++) {
// Loop: repeat while the condition holds
for (int j = 0; j < 3; j++) {
// Check whether j == 1
if (j == 1) {
// Leave the switch/loop
break;
}
// Print formatted text to the screen
printf("(%d,%d) ", i, j);
}
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
breakand expecting it to exit both loops in a nested loop, when it only exits the innermost. - Putting
continuebefore the increment in awhileloop, causing an infinite loop. - Using
breakoutside a loop or switch, which is a compile error.
if,if-else, else-if ladders, nested conditions, andswitchlet a program choose between different paths.while,do-while, andforloops, including nested loops, repeat code while a condition holds.breakandcontinuegive finer control by leaving a loop or skipping to its next iteration.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: