break and continue Statements in C
break and continue Statements in C
Introduction
break and continue Statements in C are control statements used to alter the normal flow of loops. These statements help programmers control loop execution more efficiently by stopping loops or skipping specific iterations.
In this C Programming Course in Jaipur, students learn how break and continue statements work, their syntax, differences, and real-world applications in C programming.
What is break Statement in C?
The break statement is used to immediately terminate a loop or switch statement.
When the break statement executes:
- The loop stops immediately
- Control moves outside the loop
Syntax:
break;
Flow of break Statement
Loop Executes → break Encountered → Loop Terminates
The remaining iterations are skipped.
Example of break Statement
#include<stdio.h>
int main() {
int i;
for(i = 1; i <= 10; i++) {
if(i == 5) {
break;
}
printf("%d\n", i);
}
return 0;
}
Output:
1
2
3
4
The loop stops when i becomes 5.
Understanding the Example
Condition
if(i == 5)
Checks whether the value is 5.
break Statement
break;
Terminates the loop immediately.
Applications of break Statement
The break statement is used in:
- Menu-driven programs
- Search operations
- Login systems
- Game development
- Input validation
Students learning C Programming Course in Jaipur use break statements in many practical programs.
What is continue Statement in C?
The continue statement skips the current iteration and moves to the next iteration of the loop.
Syntax:
continue;
Unlike break, the loop does not terminate completely.
Flow of continue Statement
Loop Executes → continue Encountered → Current Iteration Skipped → Next Iteration Starts
Example of continue Statement
#include<stdio.h>
int main() {
int i;
for(i = 1; i <= 5; i++) {
if(i == 3) {
continue;
}
printf("%d\n", i);
}
return 0;
}
Output:
1
2
4
5
The value 3 is skipped.
Understanding continue Statement
Condition
if(i == 3)
Checks whether the value is 3.
continue Statement
continue;
Skips the remaining code for that iteration.
Difference Between break and continue
| break | continue |
|---|---|
| Terminates loop completely | Skips current iteration |
| Control exits loop | Control moves to next iteration |
| Used to stop execution | Used to skip specific conditions |
Both are important loop control statements.
Example Using while Loop with break
#include<stdio.h>
int main() {
int i = 1;
while(i <= 10) {
if(i == 6) {
break;
}
printf("%d\n", i);
i++;
}
return 0;
}
Output:
1
2
3
4
5
Example Using while Loop with continue
#include<stdio.h>
int main() {
int i = 0;
while(i < 5) {
i++;
if(i == 3) {
continue;
}
printf("%d\n", i);
}
return 0;
}
Output:
1
2
4
5
Real-World Example
Search operation:
for(i = 0; i < size; i++) {
if(array[i] == key) {
printf("Found");
break;
}
}
The loop stops once the value is found.
Nested Loops with break
Example:
#include<stdio.h>
int main() {
int i, j;
for(i = 1; i <= 3; i++) {
for(j = 1; j <= 3; j++) {
if(j == 2) {
break;
}
printf("%d %d\n", i, j);
}
}
return 0;
}
The break statement only terminates the inner loop.
Common Errors
Infinite Loop with continue
Incorrect:
while(i <= 5) {
if(i == 3) {
continue;
}
i++;
}
This creates an infinite loop because i++ is skipped.
Correct:
i++;
should execute before continue.
Misusing break Statement
Using break unnecessarily may terminate loops too early.
Best Practices
- Use
breakonly when necessary - Avoid excessive use of
continue - Write clear conditions
- Maintain proper indentation
- Test loop behavior carefully
Readable code improves debugging and maintenance.
Importance of break and continue Statements
These statements help:
- Control loop execution
- Improve efficiency
- Simplify program logic
- Handle special conditions
Loop control statements are essential for practical programming.
Summary
break and continue Statements in C are loop control statements used to modify loop execution. The break statement terminates the loop completely, while the continue statement skips the current iteration and continues execution.
This lesson explained syntax, flow, examples, nested loops, applications, differences, common errors, and best practices for break and continue statements in C programming.
FAQs
What is break Statement in C?
The break statement terminates a loop or switch statement immediately.
What is continue Statement in C?
The continue statement skips the current iteration and moves to the next iteration.
What is the difference between break and continue?
break stops the loop completely, while continue skips only the current iteration.
Can break be used in switch statement?
Yes, break is commonly used in switch statements.
Why are break and continue important?
They help control loop execution efficiently.
