break और next
break पूरी तरह एक loop खत्म करता है, जबकि next सिर्फ current turn skip करता है और अगले के साथ जारी रहता है। यह एक game छोड़ने बनाम एक round पास करने जैसा है।
In this page:
Syntax
for (item in vector_name) {
if (skip_condition) next
if (stop_condition) break
# body
}
break और next
break तुरंत nearest enclosing loop से exit करता है, जबकि next current iteration का बाकी हिस्सा skip करता है और अगले पर move करता है -- दूसरी languages में continue का R equivalent।
दोनों for, while, और repeat loops के अंदर काम करते हैं। ये loop की अपनी condition से आगे loop execution पर finer control देते हैं।
Note:
R
next इस्तेमाल करता है जहाँ कई दूसरी languages continue इस्तेमाल करती हैं -- यह naming difference याद रखें।उदाहरण: break and next
for (i in 1:10) {
if (i %% 2 == 0) {
next # skip even numbers
}
if (i > 7) {
break # stop once past 7
}
cat(i, "\n")
}
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- एक loop के बाहर
nextइस्तेमाल करना, जो एक error देता है। - यह उम्मीद करना कि
breakसारे nested loops छोड़ देगा, जब यह सिर्फ innermost वाले से exit करता है। nextको ऐसे code के बाद रखना जो नहीं चलना चाहिए, इसलिए इससे पहले के statements फिर भी execute होते हैं।
चैप्टर सारांश
if,else, औरifelse()alternatives के बीच चुनते हैं।for,while, औरrepeatloops code repeat करते हैं।breakऔरnextloop flow control करते हैं।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: