Break, Continue, Goto in C
Break, Continue, Goto in C
Introduction
In C programming, sometimes we need to control the flow of loops and statements.
This is done using jump statements like:
-
break -
continue -
goto
👉 These statements help us change the normal execution of a program.
What are Jump Statements?
Jump statements are used to:
- Skip some part of code
- Exit loops early
- Jump to another part of the program
1. break Statement
The break statement is used to exit a loop immediately.
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;
}
Output
1
2
Explanation
👉 Loop stops when i == 3
2. continue Statement
The continue statement is used to skip the current iteration and move to the next one.
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;
}
Output
1
2
4
5
Explanation
👉 Number 3 is skipped
3. goto Statement
The goto statement is used to jump to a labeled statement.
Syntax
goto label;
label:
// code
Example
#include <stdio.h>
int main() {
int i = 1;
start:
if (i <= 3) {
printf("%d\n", i);
i++;
goto start;
}
return 0;
}
Output
1
2
3
⚠️ Important Note
👉 Avoid excessive use of goto
👉 It can make code hard to understand
Comparison Table
| Statement | Purpose | Effect |
|---|---|---|
| break | Exit loop | Stops loop immediately |
| continue | Skip iteration | Skips current loop step |
| goto | Jump control | Moves to labeled statement |
Common Mistakes
-
❌ Using
breakoutside loop -
❌ Misusing
continue -
❌ Overusing
goto -
❌ Infinite loops with
goto
Pro Tips
-
✔ Use
breakto exit loops early -
✔ Use
continueto skip unwanted cases -
✔ Avoid
gotounless necessary - ✔ Write clean and readable code
Conclusion
break, continue, and goto are powerful control statements in C. They help manage program flow efficiently.
Use them wisely to write better programs.
👉 This article is part of Dharani Tech Edu Hub — where learning programming is made simple and practical.
Comments
Post a Comment