break and continue Statements in C Programming
break and continue Statements in C Programming
In C programming, break and continue are loop control statements. They are used to change the normal flow of loops like for, while, and do-while.
1. break Statement
The break statement immediately stops the loop and exits from it.
Syntax:
break;
Example:
#include <stdio.h>
int main() {
int i;
for(i = 1; i <= 5; i++) {
if(i == 3)
break;
printf("%d\n", i);
}
return 0;
}
This program prints 1 and 2, then stops when i becomes 3.
2. continue Statement
The continue statement skips the current iteration and moves to the next loop cycle.
Syntax:
continue;
Example:
#include <stdio.h>
int main() {
int i;
for(i = 1; i <= 5; i++) {
if(i == 3)
continue;
printf("%d\n", i);
}
return 0;
}
This program prints 1, 2, 4, and 5. It skips printing 3.
Difference Between break and continue
| break | continue |
|---|---|
| Stops the loop completely | Skips only one iteration |
| Exits the loop | Moves to next iteration |
Conclusion
The break and continue statements give better control over loops. Understanding these concepts helps in writing efficient and logical programs.
Comments
Post a Comment