Break and Continue Statements in C
While working with loops, sometimes we need more control over how the loop behaves.
C provides two special statements for this purpose:
breakcontinue
These statements help us control the flow inside loops in a very precise way.
The break Statement
The break statement is used to
immediately terminate a loop.
When break is executed, the loop stops,
and control moves to the statement after the loop.
Example: Stop Loop When Number Is Found
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
printf("%d ", i);
}
return 0;
}
Output:
1 2 3 4
Explanation:
- The loop starts from 1
- When
ibecomes 5,breakexecutes - The loop stops completely
The continue Statement
The continue statement is used to
skip the current iteration
and move to the next iteration of the loop.
Unlike break, the loop does not stop.
Example: Skip One Value
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
printf("%d ", i);
}
return 0;
}
Output:
1 2 4 5
Explanation:
- When
iis 3,continueis executed - Printing is skipped only for that iteration
- The loop continues normally
break vs continue
breakcompletely exits the loopcontinueskips only the current iterationbreakstops loop executioncontinuemoves to the next cycle
Real-World Thinking
Think of a loop as a classroom:
- break: Teacher ends the class immediately
- continue: Teacher skips one student and continues
This analogy helps remember the difference easily.
Mini Practice
- Print numbers from 1 to 10 but stop when number is 7
- Print numbers from 1 to 10 but skip number 5
- Print only odd numbers using continue
Quick Quiz
Q1. What does the break statement do?
It immediately exits the loop.
Q2. What does continue do?
It skips the current iteration and continues the loop.
Q3. Does break stop the entire program?
No, it stops only the loop.
Q4. Can continue be used outside a loop?
No, it can be used only inside loops.
Q5. Which statement is used to skip printing a value?
continue