🟢 Beginner · Lesson 15
break, continue और goto
break Statement
break statement nearest enclosing loop (for, while, do-while) या switch को immediately exit करता है। Execution loop/switch के बाद वाले statement से continue होती है।
C Language
for (int i = 1; i <= 10; i++) { if (i == 6) break; // i=6 पर loop exit printf("%d ", i); }
1 2 3 4 5
Search में break
C Language
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; // मिल गया, आगे ढूंढने की ज़रूरत नहीं } }
8 index 2 par mila
💡 break केवल INNERMOST loop exit करता है
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 करता है।
C Language – Odd Numbers Print
printf("Odd numbers: "); for (int i = 1; i <= 10; i++) { if (i % 2 == 0) continue; // Even numbers skip printf("%d ", i); }
Odd numbers: 1 3 5 7 9
continue – Prime Numbers
C Language
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); }
Prime numbers: 2 3 5 7 11 13 17 19
goto Statement
goto execution को function के किसी labeled statement पर jump करता है। Modern C में इसे generally avoid किया जाता है।
⚠️ goto से बचें!
goto code को "spaghetti code" बना देता है — hard to read, debug और maintain। इसकी जगह loops, break और continue use करें।
Programs
C Language – Password Attempts with break
#include <stdio.h> #include <string.h> int main() { char correct[] = "codingEasily"; 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 करें
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C में break और continue में क्या अंतर है?
break तुरंत पूरे loop से बाहर निकल जाता है, तो आगे कोई pass नहीं चलता। continue केवल मौजूदा pass का बाकी हिस्सा छोड़कर अगली iteration पर कूद जाता है। संक्षेप में, break loop रोकता है, जबकि continue एक round छोड़ता है।loop में break statement क्या करता है?
break loop को तुरंत समाप्त करता है और control को loop के बाद की पहली line पर ले जाता है। यह तब उपयोगी है जब आपको जो चाहिए मिल गया हो या ऐसी condition आए जिसका मतलब आगे चलने का कोई फ़ायदा नहीं।C में continue statement क्या करता है?
continue मौजूदा iteration के बचे statements छोड़कर सीधे अगली पर जाता है। यह तब काम आता है जब आप कुछ values अनदेखा करना चाहें पर loop चालू रखें, जैसे सूची process करते समय negative numbers छोड़ना।क्या break और continue किसी भी loop में इस्तेमाल हो सकते हैं?
हाँ, दोनों
for, while और do-while loops में काम करते हैं। break switch statements के अंदर case समाप्त करने को भी इस्तेमाल होता है। Nested loops में, ये केवल उन्हें रखने वाले सबसे भीतरी loop को प्रभावित करते हैं।क्या nested loop में break सभी loops से बाहर निकालता है?
नहीं। एक
break केवल उसी loop से बाहर निकलता है जिसके सीधे अंदर है — सबसे भीतरी। कई nested loops एक साथ छोड़ने को आपको दूसरा तरीका चाहिए, जैसे flag variable या code पुनर्गठन, क्योंकि C में labelled break नहीं है।💻 लाइव कोड एडिटर
इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.