🟡 Control Flow  ·  Lesson 20

break and continue Statements

Controlling a loop

Sometimes you want to stop a loop early, or skip just one round without stopping. C++ gives you two keywords for exactly this: break and continue.

The break statement

break exits the loop immediately — control jumps to whatever comes after it:

C++
for (int i = 1; i <= 10; i++) {
    if (i == 4) break;    // stop when i is 4
    cout << i << " ";
}
Output:
1 2 3

The loop was set to run to 10, but break ended it at 4.

The continue statement

continue skips the rest of the current pass and jumps to the next iteration:

C++
for (int i = 1; i <= 6; i++) {
    if (i % 2 == 0) continue;   // skip even numbers
    cout << i << " ";
}
Output:
1 3 5

When i is even, continue skips the cout and moves on — so only odd numbers print.

break and continue together

C++
for (int i = 1; i <= 10; i++) {
    if (i == 8) break;          // stop entirely at 8
    if (i % 3 == 0) continue;   // skip multiples of 3
    cout << i << " ";
}
Output:
1 2 4 5 7

In nested loops

Inside nested loops, both affect only the innermost loop that contains them:

C++
for (int i = 1; i <= 2; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j == 2) break;      // breaks only the inner loop
        cout << i << "," << j << "  ";
    }
}
Output:
1,1 2,1

The break ends the inner loop each time, but the outer loop keeps going. To exit both at once, use a flag or move the loops into a function and return.

Common mistakes

  • Expecting break to leave all nested loops — it exits only one.
  • Placing continue before the counter update in a while loop, causing an infinite loop.
  • Using continue where a simple if around the code would be clearer.
  • Forgetting that break in a switch is separate from loop break.
🏋️ Practice

Loop from 1 to 50 and print numbers, but break when you reach the first multiple of 13, and continue past any multiple of 5. Predict the output before running it.

Summary

  • break exits the loop entirely.
  • continue skips the rest of the current pass and moves on.
  • Both work in for, while and do-while.
  • In nested loops they affect only the innermost loop.
  • Watch continue in while loops — update the counter first.

Loop नियंत्रित करना

कभी आप loop को जल्दी रोकना चाहते हैं, या बिना रुके बस एक round छोड़ना चाहते हैं। C++ आपको ठीक इसके लिए दो keywords देता है: break और continue

break statement

break loop से तुरंत बाहर निकल जाता है — control उसके बाद जो हो उस पर कूद जाता है:

C++
for (int i = 1; i <= 10; i++) {
    if (i == 4) break;    // i 4 hone par ruko
    cout << i << " ";
}
Output:
1 2 3

Loop 10 तक चलने को सेट था, पर break ने इसे 4 पर खत्म कर दिया।

continue statement

continue मौजूदा pass का बाकी हिस्सा छोड़कर अगली iteration पर कूद जाता है:

C++
for (int i = 1; i <= 6; i++) {
    if (i % 2 == 0) continue;   // even numbers skip karo
    cout << i << " ";
}
Output:
1 3 5

जब i even हो, continue cout छोड़कर आगे बढ़ जाता है — तो केवल odd numbers print होते हैं।

break और continue साथ

C++
for (int i = 1; i <= 10; i++) {
    if (i == 8) break;          // 8 par poori tarah ruko
    if (i % 3 == 0) continue;   // 3 ke multiples skip karo
    cout << i << " ";
}
Output:
1 2 4 5 7

Nested loops में

Nested loops के अंदर, दोनों केवल उन्हें रखने वाले सबसे भीतरी loop को प्रभावित करते हैं:

C++
for (int i = 1; i <= 2; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j == 2) break;      // sirf inner loop break karta hai
        cout << i << "," << j << "  ";
    }
}
Output:
1,1 2,1

break हर बार inner loop खत्म करता है, पर outer loop चलता रहता है। दोनों एक साथ छोड़ने को flag इस्तेमाल करें या loops को function में डालकर return करें।

आम गलतियाँ

  • break से सभी nested loops छोड़ने की उम्मीद करना — यह केवल एक से निकलता है।
  • while loop में counter update से पहले continue रखना, infinite loop पैदा करना।
  • continue वहाँ इस्तेमाल करना जहाँ code के चारों ओर सादा if ज़्यादा साफ़ होता।
  • यह भूलना कि switch में break loop break से अलग है।
🏋️ अभ्यास

1 से 50 तक loop करें और numbers print करें, पर 13 के पहले multiple पर पहुँचने पर break करें, और 5 के किसी भी multiple को continue से छोड़ें। चलाने से पहले output का अनुमान लगाएँ।

सारांश

  • break loop से पूरी तरह बाहर निकलता है।
  • continue मौजूदा pass का बाकी छोड़कर आगे बढ़ता है।
  • दोनों for, while और do-while में काम करते हैं।
  • Nested loops में ये केवल सबसे भीतरी loop को प्रभावित करते हैं।
  • while loops में continue का ध्यान रखें — पहले counter update करें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में break और continue में क्या अंतर है?
break तुरंत पूरे loop से बाहर निकल जाता है, तो आगे कोई pass नहीं चलता। continue केवल मौजूदा pass का बाकी हिस्सा छोड़कर अगली iteration पर कूद जाता है। संक्षेप में, break loop रोकता है, जबकि continue एक round छोड़ता है।
C++ में break statement क्या करता है?
break loop को तुरंत समाप्त करता है और control को loop के बाद की पहली statement पर ले जाता है। यह तब उपयोगी है जब आपको जो चाहिए मिल गया हो, या ऐसी condition आए जिसका मतलब आगे चलने का कोई फ़ायदा नहीं। यह switch में case समाप्त करने को भी इस्तेमाल होता है।
C++ में continue statement क्या करता है?
continue मौजूदा iteration के बचे statements छोड़कर सीधे अगली पर जाता है। यह तब काम आता है जब आप कुछ values अनदेखा करना चाहें पर loop चालू रखें, जैसे सूची process करते समय negative numbers छोड़ना।
क्या nested loop में break सभी loops से बाहर निकालता है?
नहीं। एक break केवल उसी loop से बाहर निकलता है जिसके सीधे अंदर है — सबसे भीतरी। कई nested loops एक साथ छोड़ने को आपको दूसरा तरीका चाहिए, जैसे flag variable या function में पुनर्गठन करके जल्दी return, क्योंकि C++ में labelled break नहीं है।
क्या break और continue किसी भी loop में इस्तेमाल हो सकते हैं?
हाँ, दोनों for, while और do-while loops में काम करते हैं। break इसके अलावा switch statements के अंदर case समाप्त करने को इस्तेमाल होता है। Nested loops में ये केवल उन्हें रखने वाले सबसे भीतरी loop को प्रभावित करते हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।