break और continue
In this page:
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
#!/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
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
for num in 1 2 3 4 5; do
if (( num % 2 == 0 )); then
continue
fi
echo "Odd number: $num"
done
Login to try C/C++/Java/PHP code in the editor
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
#!/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
Login to try C/C++/Java/PHP code in the editor
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
#!/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
Login to try C/C++/Java/PHP code in the editor
- यह confuse करना कि
continueक्या skip करता है; यह सीधे loop की condition/next item re-check करने पर jump करता है, सिर्फ *current iteration के body का बाकी हिस्सा* skip करते हुए, पूरा loop नहीं। - यह भूल जाना कि
breakऔरcontinueदोनों एक optional numeric argument accept करते हैं जब loops nested हों तो एक outer loop को affect करने के लिए, जैसेbreak 2, और default रूप से यह मान लेना कि वे सिर्फ innermost loop को ही affect करते हैं। - किसी loop के बाहर
break/continueउपयोग करना, जो एक shell warning/error produce करता है क्योंकि उनके act करने के लिए कोई loop context नहीं है।
breaknearest enclosing loop से तुरंत exit करता है, बाकी बचे सभी iterations को पूरी तरह skip करते हुए।continuecurrent 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 nestedifconditions से ज़्यादा clear बनाता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: