🟡 Control Flow  ·  Lesson 19

break and continue

break और continue

The break Statement — exit the loop entirely

for (let i = 1; i <= 10; i++) {
    if (i === 5) {
        break;              // stop the loop completely
    }
    console.log(i);
}
// Output: 1, 2, 3, 4     (5 onwards never printed)
break stops a loop immediately and jumps to the code after it. The moment JavaScript hits break, the loop is finished — no more iterations, regardless of what the loop condition says. You met break already in the switch statement, where it ends a case; in loops it ends the whole loop. Its main purpose is efficiency and logic: once you've found what you were looking for, why keep searching? Almost always, break sits inside an if — "if this condition is met, we're done."

The continue Statement — skip this one pass

for (let i = 1; i <= 5; i++) {
    if (i === 3) {
        continue;           // skip ONLY this iteration
    }
    console.log(i);
}
// Output: 1, 2, 4, 5      (3 was skipped, loop continued)

// Print only even numbers:
for (let i = 1; i <= 10; i++) {
    if (i % 2 !== 0) continue;    // skip odd numbers
    console.log(i);               // 2, 4, 6, 8, 10
}
continue skips the REST of the current iteration and jumps to the next one — unlike break, the loop keeps running. Think of it as "not this one, next please." It's ideal for filtering: skip invalid entries, skip odd numbers, skip empty rows, and process everything else. The pattern if (shouldSkip) continue; at the top of a loop body is very common and reads cleanly — it lets you handle exceptions early instead of wrapping the whole body in a big if.

break vs continue — the crucial difference

breakcontinue
EffectEXITS the loopSKIPS one iteration
Loop keeps running?No — it's overYes — next pass starts
Use forFound it / stop searchingFilter out unwanted items
The mental model is simple: break means "I'm done with this loop entirely," while continue means "I'm done with this particular item — move on to the next." Mixing them up is a common beginner error, and the symptom is obvious once you know: using break where you meant continue makes your loop stop after the first skip. Both statements affect only the loop they're directly inside — which matters a lot with nested loops.

In Nested Loops — only the inner loop is affected

for (let i = 1; i <= 3; i++) {
    for (let j = 1; j <= 3; j++) {
        if (j === 2) break;      // breaks the INNER loop only
        console.log(i, j);
    }
}
// Output: 1 1, 2 1, 3 1   ← the outer loop keeps going!
A break inside a nested loop only exits the loop it's directly in — the INNER one. The outer loop carries on to its next pass. This surprises people who expect break to escape everything. In the example, break ends each inner loop early, but the outer loop still runs all three times. If you truly need to exit both, you can use a "labelled break" (giving the outer loop a name like outer: and writing break outer;), though this is rare. Usually it's cleaner to restructure the code or use a flag variable.

Practical Uses — searching and filtering

// break: stop as soon as you find what you want
let students = ["Aman", "Priya", "Ravi"];
for (let i = 0; i < students.length; i++) {
    if (students[i] === "Priya") {
        console.log("Found at index " + i);
        break;                     // no point checking the rest
    }
}

// continue: skip the ones you don't want
let marks = [85, -1, 92, -1, 78];
for (let m of marks) {
    if (m < 0) continue;           // skip invalid entries
    console.log("Valid mark:", m);
}
These two statements shine in real tasks. break powers searching: loop through a list, and the instant you find your target, stop — checking the remaining items would waste time. continue powers filtering: walk the whole list, but skip over entries that don't qualify (invalid data, empty values, items that don't match a filter). Together with for and if, they cover a huge portion of the everyday logic you'll write. Now you have the complete toolkit for control flow.

Exam Corner

Q: What does break do in a loop? Exits the loop entirely and continues after it.

Q: What does continue do? Skips the rest of the current iteration and moves to the next one.

Q: In a nested loop, which loop does break exit? Only the innermost loop it's directly inside.

Q: Which would you use to stop searching once found? break.

Q: Which would you use to skip invalid items but keep looping? continue.

break Statement — loop पूरी तरह छोड़ना

for (let i = 1; i <= 10; i++) {
    if (i === 5) {
        break;              // loop bilkul rok do
    }
    console.log(i);
}
// Output: 1, 2, 3, 4     (5 se aage kabhi print nahi)
break loop को तुरंत रोककर उसके बाद के code पर कूद जाता है. जिस पल JavaScript break पर पहुंचता है, loop खत्म — कोई और iterations नहीं, चाहे loop condition कुछ भी कहे. आप break को switch statement में मिल चुके, जहां यह case खत्म करता है; loops में यह पूरा loop खत्म करता है. इसका मुख्य मकसद efficiency और logic है: जो ढूंढ रहे थे मिल गया, तो खोजते क्यों रहें? लगभग हमेशा, break किसी if के अंदर बैठता है — "अगर यह condition पूरी हो, हमारा काम हो गया."

continue Statement — यह एक pass छोड़ो

for (let i = 1; i <= 5; i++) {
    if (i === 3) {
        continue;           // SIRF yeh iteration chhodo
    }
    console.log(i);
}
// Output: 1, 2, 4, 5      (3 chhoda, loop chalta raha)

// Sirf even numbers print karo:
for (let i = 1; i <= 10; i++) {
    if (i % 2 !== 0) continue;    // odd numbers chhodo
    console.log(i);               // 2, 4, 6, 8, 10
}
continue वर्तमान iteration का BAKI हिस्सा छोड़कर अगले पर कूदता हैbreak के उलट, loop चलता रहता है. इसे "यह नहीं, अगला please" समझिए. यह filtering के लिए आदर्श है: अमान्य entries छोड़िए, odd numbers छोड़िए, खाली rows छोड़िए, और बाकी सब process कीजिए. Loop body के ऊपर if (shouldSkip) continue; pattern बहुत common है और साफ पढ़ता है — यह आपको अपवाद जल्दी संभालने देता है बजाय पूरी body को बड़े if में लपेटने के.

break vs continue — अहम अंतर

breakcontinue
असरLoop से BAHARएक iteration CHHODTA
Loop चलता रहता?नहीं — खत्महां — अगला pass शुरू
किसके लिएमिल गया / खोज बंदअनचाहे items filter
मानसिक मॉडल सरल है: break मतलब "मेरा इस loop से काम पूरा," जबकि continue मतलब "इस खास item से काम पूरा — अगले पर चलो." इन्हें उलझाना common beginner गलती है, और लक्षण जानने पर साफ है: जहां continue चाहिए वहां break use करना आपका loop पहले skip पर ही रोक देता है. दोनों statements सिर्फ उस loop को प्रभावित करते हैं जिसके सीधे अंदर हैं — जो nested loops के साथ बहुत मायने रखता है.

Nested Loops में — सिर्फ अंदरूनी loop प्रभावित

for (let i = 1; i <= 3; i++) {
    for (let j = 1; j <= 3; j++) {
        if (j === 2) break;      // sirf ANDAR ka loop tootta hai
        console.log(i, j);
    }
}
// Output: 1 1, 2 1, 3 1   ← bahar ka loop chalta rehta hai!
Nested loop के अंदर break सिर्फ उसी loop से बाहर निकलता है जिसके वह सीधे अंदर है — ANDAR वाले से. बाहरी loop अपने अगले pass पर चलता रहता है. यह उन्हें चौंकाता है जो break से सब कुछ छोड़ने की उम्मीद करते हैं. Example में, break हर अंदरूनी loop जल्दी खत्म करता है, पर बाहरी loop फिर भी तीनों बार चलता है. अगर सच में दोनों से निकलना हो, "labelled break" use कर सकते हैं (बाहरी loop को outer: जैसा नाम देकर break outer; लिखना), हालांकि यह दुर्लभ है. आमतौर पर code restructure करना या flag variable use करना साफ है.

व्यावहारिक इस्तेमाल — खोजना और filter करना

// break: jo chahiye milte hi ruk jao
let students = ["Aman", "Priya", "Ravi"];
for (let i = 0; i < students.length; i++) {
    if (students[i] === "Priya") {
        console.log("Found at index " + i);
        break;                     // baaki jaanchne ka koi matlab nahi
    }
}

// continue: jo nahi chahiye unhe chhodo
let marks = [85, -1, 92, -1, 78];
for (let m of marks) {
    if (m < 0) continue;           // amaanya entries chhodo
    console.log("Valid mark:", m);
}
ये दो statements असली कामों में चमकते हैं. break खोज चलाता है: list पर loop कीजिए, और जिस पल target मिले, रुक जाइए — बचे items जांचना समय बर्बाद करता. continue filtering चलाता है: पूरी list पर चलिए, पर योग्य न होने वाली entries छोड़िए (अमान्य data, खाली values, filter से न मिलते items). for और if के साथ, वे आपके रोज़ लिखे logic का बड़ा हिस्सा cover करते हैं. अब आपके पास control flow की पूरी toolkit है.

Exam Corner

Q: Loop में break क्या करता है? Loop पूरी तरह छोड़कर उसके बाद जारी रहता है.

Q: continue क्या करता है? वर्तमान iteration का बाकी हिस्सा छोड़कर अगले पर जाता है.

Q: Nested loop में break किस loop से निकलता है? सिर्फ उस अंदरूनी loop से जिसके वह सीधे अंदर है.

Q: मिलते ही खोज रोकने को कौन-सा use करेंगे? break.

Q: अमान्य items छोड़कर loop जारी रखने को कौन-सा? continue.
← Back to JavaScript Tutorial
🔗

Share this topic with a friend

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

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

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

💻 Live Code Editor

इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
👁 Live Preview
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का console.log देखने के लिए F12 दबाइए.