🟡 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.

Frequently Asked Questions

What is the difference between break and continue in C++?
break immediately exits the entire loop, so no more passes run. continue skips only the rest of the current pass and jumps to the next iteration. In short, break stops the loop, while continue skips one round.
What does the break statement do in C++?
break ends the loop right away and moves control to the first statement after the loop. It is useful when you have found what you were looking for, or hit a condition that means there is no point continuing. It is also used to end a case in a switch.
What does the continue statement do in C++?
continue skips the remaining statements in the current iteration and goes straight to the next one. It is handy when you want to ignore certain values but keep looping, such as skipping negative numbers while processing a list.
Does break exit all loops in a nested loop?
No. A single break only exits the loop it is directly inside — the innermost one. To leave several nested loops at once you need another approach, such as a flag variable or restructuring into a function with an early return, since C++ has no labelled break.
Can break and continue be used in any loop?
Yes, both work in for, while and do-while loops. break is additionally used inside switch statements to end a case. In nested loops they affect only the innermost loop that contains them.

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 करें।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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