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

while और until

while तब तक repeat होता है जब तक एक condition true हो, और until तब तक repeat होता है जब तक यह false हो। यह soup hot होने तक stir करने बनाम इसके अभी भी cold होने तक stir करने जैसा है।

In this page:

  1. while और until
Syntax
ruby
while condition
  # body
end

until condition
  # body
end

while और until

while condition इसका block तब तक repeat करता है जब तक condition truthy हो, हर iteration से पहले check होते हुए। until condition इसका inverse है, तब तक repeat करते हुए जब तक condition falsy हो।

दोनों एक single statement के लिए एक trailing modifier form भी support करते हैं, जैसे i += 1 while i < 10।

Note: stopping point की तरह phrased conditions के लिए until अक्सर while !condition से ज़्यादा naturally पढ़ा जाता है।

उदाहरण: while and until

markup
count = 1

while count <= 3
  puts "While count: #{count}"
  count += 1
end

count = 1
until count > 3
  puts "Until count: #{count}"
  count += 1
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. counter update करना भूल जाना, इसलिए while count <= 3 हमेशा के लिए चलता है।
  2. until को while से confuse करना, जब until condition false होने तक चलता है।
  3. जब each या times simpler हो और हमेशा के लिए loop नहीं कर सकता तब while इस्तेमाल करना।
🔒

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.