break, continue & goto
break, continue और goto
break Statement
The break statement immediately exits the nearest enclosing loop (for, while, do-while) or switch. Execution continues from the statement after the loop/switch.
#include <stdio.h> int main() { printf("Counting: "); for (int i = 1; i <= 10; i++) { if (i == 6) break; // Stop when i reaches 6 printf("%d ", i); } printf("\nLoop ended\n"); return 0; }
break – Search and Exit
#include <stdio.h> int main() { int arr[] = {10, 25, 8, 40, 15}; int n = 5, key = 8, found = 0; for (int i = 0; i < n; i++) { if (arr[i] == key) { printf("%d found at index %d\n", key, i); found = 1; break; // No need to search further } } if (!found) printf("Not found\n"); return 0; }
break in Nested Loops
break exits only the loop it is directly inside. In nested loops, it won't exit the outer loop. You need additional logic (flag variable or goto) to break out of multiple loops.
for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (j == 2) break; // Exits INNER loop only printf("(%d,%d) ", i, j); } printf("\n"); }
continue Statement
The continue statement skips the rest of the current iteration and moves to the next iteration of the loop. Unlike break, it doesn't exit the loop — it just skips one cycle.
#include <stdio.h> int main() { printf("Odd numbers 1-10: "); for (int i = 1; i <= 10; i++) { if (i % 2 == 0) continue; // Skip even numbers printf("%d ", i); } printf("\n"); return 0; }
continue – Skip Divisible by 3
#include <stdio.h> int main() { printf("Skip multiples of 3:\n"); for (int i = 1; i <= 15; i++) { if (i % 3 == 0) { printf("(skip %d) ", i); continue; } printf("%d ", i); } printf("\n"); return 0; }
goto Statement
The goto statement jumps execution to a labeled statement elsewhere in the function. While it exists in C, it is generally avoided as it makes code hard to read and maintain (leads to "spaghetti code").
#include <stdio.h> int main() { int i = 1; start: // Label if (i > 5) goto end; // Jump to 'end' label printf("%d ", i); i++; goto start; // Jump back to 'start' end: printf("\nDone!\n"); return 0; }
The only acceptable use of goto in modern C is for error handling and cleanup in embedded/systems code — jumping to cleanup code when an error occurs in a deeply nested section. For all normal programming, use loops and break/continue instead.
Complete Programs
Prime Numbers with continue
#include <stdio.h> int main() { int n; printf("Enter N: "); scanf("%d", &n); printf("Primes up to %d: ", n); for (int num = 2; num <= n; num++) { int isPrime = 1; for (int j = 2; j * j <= num; j++) { if (num % j == 0) { isPrime = 0; break; } } if (!isPrime) continue; // Skip non-primes printf("%d ", num); } printf("\n"); return 0; }
Summary
| Statement | Effect | Used with |
|---|---|---|
break | Exits the loop/switch immediately | for, while, do-while, switch |
continue | Skips current iteration, goes to next | for, while, do-while |
goto label | Jumps to labeled statement | Function scope (avoid in practice) |
break— exits the innermost loop only (not outer loops)continue— skips rest of current iteration, loop continuesgoto— generally avoided; use only for error handling in embedded code
break Statement
break statement nearest enclosing loop (for, while, do-while) या switch को immediately exit करता है। Execution loop/switch के बाद वाले statement से continue होती है।
for (int i = 1; i <= 10; i++) { if (i == 6) break; // i=6 पर loop exit printf("%d ", i); }
Search में break
int arr[] = {10, 25, 8, 40, 15}; int key = 8; for(int i=0; i<5; i++) { if(arr[i] == key) { printf("%d index %d par mila\n", key, i); break; // मिल गया, आगे ढूंढने की ज़रूरत नहीं } }
Nested loops में break सिर्फ उस loop को exit करता है जिसके अंदर है। Outer loop को exit करने के लिए flag variable या अतिरिक्त logic चाहिए।
continue Statement
continue statement current iteration का बाकी हिस्सा skip करता है और loop की अगली iteration पर जाता है। break के विपरीत, यह loop exit नहीं करता — बस एक cycle skip करता है।
printf("Odd numbers: "); for (int i = 1; i <= 10; i++) { if (i % 2 == 0) continue; // Even numbers skip printf("%d ", i); }
continue – Prime Numbers
int n = 20; printf("Prime numbers: "); for(int num=2; num<=n; num++) { int isPrime = 1; for(int j=2; j*j<=num; j++) if(num%j==0) { isPrime=0; break; } if(!isPrime) continue; // Non-prime skip printf("%d ", num); }
goto Statement
goto execution को function के किसी labeled statement पर jump करता है। Modern C में इसे generally avoid किया जाता है।
goto code को "spaghetti code" बना देता है — hard to read, debug और maintain। इसकी जगह loops, break और continue use करें।
Programs
#include <stdio.h> #include <string.h> int main() { char correct[] = "codeKaFunda"; char input[50]; int attempts = 0; while (attempts < 3) { printf("Password daalen: "); scanf("%s", input); attempts++; if (strcmp(input, correct) == 0) { printf("Sahii! Swagat hai!\n"); break; // Sahi password mila, loop exit } printf("Galat! %d/3 attempts\n", attempts); } if (attempts == 3) printf("Account lock!\n"); return 0; }
सारांश
| Statement | Effect | किसके साथ |
|---|---|---|
break | Loop/switch को immediately exit करता है | for, while, do-while, switch |
continue | Current iteration skip, अगली iteration पर | for, while, do-while |
goto label | Labeled statement पर jump करता है | Function scope (avoid करें) |
break— innermost loop को ही exit करता है, outer को नहींcontinue— current iteration के बाकी code को skip करता है, loop चलती रहती हैgoto— generally avoid करें; loops और break/continue use करें