← Back to R Course | Chapter 4: Control Flow | Lesson 6 of 6

break और next

break पूरी तरह एक loop खत्म करता है, जबकि next सिर्फ current turn skip करता है और अगले के साथ जारी रहता है। यह एक game छोड़ने बनाम एक round पास करने जैसा है।

In this page:

  1. break और next
Syntax
r
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

markup
for (i in 1:10) {
  if (i %% 2 == 0) {
    next  # skip even numbers
  }
  if (i > 7) {
    break  # stop once past 7
  }
  cat(i, "\n")
}
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. एक loop के बाहर next इस्तेमाल करना, जो एक error देता है।
  2. यह उम्मीद करना कि break सारे nested loops छोड़ देगा, जब यह सिर्फ innermost वाले से exit करता है।
  3. next को ऐसे code के बाद रखना जो नहीं चलना चाहिए, इसलिए इससे पहले के statements फिर भी execute होते हैं।
चैप्टर सारांश
  • if, else, और ifelse() alternatives के बीच चुनते हैं।
  • for, while, और repeat loops code repeat करते हैं।
  • break और next loop flow control करते हैं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

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.