← Back to Bash Course | Chapter 4: Loops | Lesson 10 of 14

break और continue

break और continue एक loop के अंदर fine-grained control देते हैं: break पूरे loop को तुरंत रोक देता है, जबकि continue सिर्फ next iteration तक आगे skip करता है।
Syntax
bash
for variable in list; do
  [ condition ] && break
  [ condition ] && continue
done

break to Exit Early

break nearest enclosing loop को तुरंत terminate करता है, और execution loop के done के बाद वाले पहले statement से continue होता है।

यह उपयोगी है उसी moment रुकने के लिए जब आपको जो ढूंढ रहे थे वह मिल जाए, बाकी बचे items को process किए बिना।

उदाहरण: break to Exit Early

bash
#!/bin/bash
for num in 1 2 3 4 5; do
    if [ "$num" -eq 3 ]; then
        echo "Found 3, stopping the loop"
        break
    fi
    echo "Checking $num"
done

continue to Skip an Iteration

continue current iteration के body का बाकी हिस्सा skip करता है और सीधे next iteration पर jump करता है (condition फिर से check करते हुए या next list item पर बढ़ते हुए)। यह उन items को filter out करने के लिए handy है जिन्हें आप आगे process नहीं करना चाहते।

उदाहरण: continue to Skip an Iteration

bash
#!/bin/bash
for num in 1 2 3 4 5; do
    if (( num % 2 == 0 )); then
        continue
    fi
    echo "Odd number: $num"
done

break N and continue N for Nested Loops

जब loops nested हों, break N या continue N innermost से counting outward Nth loop पर act करता है (तो break 2 एक साथ दो levels of loops exit करता है)। बिना किसी number के, दोनों by default सिर्फ innermost loop को affect करते हैं।

उदाहरण: break N and continue N for Nested Loops

bash
#!/bin/bash
for outer in 1 2 3; do
    for inner in a b c; do
        if [ "$inner" = b ]; then
            continue 2
        fi
        echo "outer=$outer inner=$inner"
    done
done

Combining break and continue

break और continue एक ही loop में साथ उपयोग किए जा सकते हैं अलग situations handle करने के लिए: continue से unwanted items skip करते हुए जबकि एक बार overall goal reach होने पर पूरी तरह रुकने के लिए भी ready रहते हुए।

यह loop body को एक समय में एक condition पर focused रखता है बजाय एक giant nested if के।

उदाहरण: Combining break and continue

bash
#!/bin/bash
total=0
for num in 1 2 3 4 5 6 7 8; do
    if (( num % 2 != 0 )); then
        continue
    fi
    total=$((total + num))
    if [ "$total" -ge 10 ]; then
        echo "Reached total of $total, stopping"
        break
    fi
done
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह confuse करना कि continue क्या skip करता है; यह सीधे loop की condition/next item re-check करने पर jump करता है, सिर्फ *current iteration के body का बाकी हिस्सा* skip करते हुए, पूरा loop नहीं।
  2. यह भूल जाना कि break और continue दोनों एक optional numeric argument accept करते हैं जब loops nested हों तो एक outer loop को affect करने के लिए, जैसे break 2, और default रूप से यह मान लेना कि वे सिर्फ innermost loop को ही affect करते हैं।
  3. किसी loop के बाहर break/continue उपयोग करना, जो एक shell warning/error produce करता है क्योंकि उनके act करने के लिए कोई loop context नहीं है।
चैप्टर सारांश
  • break nearest enclosing loop से तुरंत exit करता है, बाकी बचे सभी iterations को पूरी तरह skip करते हुए।
  • continue current iteration के body का बाकी हिस्सा skip करता है और next iteration के condition check पर jump करता है।
  • दोनों एक optional numeric argument accept करते हैं, break N / continue N, loops nested होने पर Nth enclosing loop को affect करने के लिए (default 1 है, innermost)।
  • break/continue को अच्छी तरह उपयोग करना अक्सर loop logic को loop body के अंदर deeply nested if conditions से ज़्यादा clear बनाता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.