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

repeat का उपयोग

repeat तब तक हमेशा के लिए चलता रहता है जब तक आप इसे रुकने के लिए न कहें, एक merry-go-round की तरह जो तब तक चलता है जब तक कोई brake न खींचे। Brake एक break command है।

In this page:

  1. repeat
Syntax
r
repeat {
  # body
  if (condition) break
}

repeat

repeat इसकी body बिना किसी built-in condition के indefinitely चलाता है, रुकने के लिए पूरी तरह अंदर एक break statement पर depend करते हुए।

यह दूसरी languages में मिलने वाले unconditional infinite loop का R equivalent है (जैसे C-family languages में while (true))। यह सबसे useful तब है जब exit condition शुरुआत में नहीं बल्कि loop body के बीच में check हो।

Note: जब आपको loop body के बीच कहीं exit condition check करना हो, सिर्फ top पर नहीं, repeat सही choice है।

उदाहरण: repeat

markup
count <- 1

repeat {
  cat("Count:", count, "\n")
  count <- count + 1
  if (count > 3) {
    break
  }
}
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. break omit करना, इसलिए repeat हमेशा के लिए चलता है।
  2. counter update को if (... ) break के बाद रखना, इसलिए loop एक iteration बहुत देर से रुकता है।
  3. repeat इस्तेमाल करना जब एक clear condition वाला while पढ़ना आसान होता।
🔒

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.