← Back to Ruby Course | Chapter 6: Control Flow | Lesson 6 of 7

loop do क्या है

loop do इसका block बार-बार चलाता है बिना किसी end के जब तक आप break न कहें, एक playground merry-go-round की तरह जो तब तक चलता है जब तक कोई इसे रोके।

In this page:

  1. loop do
Syntax
ruby
loop do
  # body
  break if condition
end

loop do

loop do ... end इसका block indefinitely चलाता है, रुकने के लिए पूरी तरह अंदर एक break पर depend करते हुए -- unconditional infinite loop का Ruby equivalent।

यह आमतौर पर तब इस्तेमाल होता है जब exit condition को loop body के बीच में check करना हो। loop automatically StopIteration भी catch करता है, जो इसे enumerators के साथ useful बनाता है।

Note: loop idiomatic choice है जब stopping condition को loop body के बीच में check करना हो, सिर्फ शुरुआत में नहीं।

उदाहरण: loop do

markup
count = 1

loop do
  puts "Count: #{count}"
  count += 1
  break if count > 3
end
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 भूल जाना, इसलिए loop do कभी खत्म नहीं होता।
  2. counter update से पहले break if count > 3 रखना, इसलिए यह कभी नहीं रुकता या एक step देर से रुकता है।
  3. एक known count वाले task के लिए loop इस्तेमाल करना, जब times या each clearer है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.