लू

लूप में Ruby

23 अभ्यास

लूप के बारे में

while

def self.years_before_desired_balance(current_balance, desired_balance)
  years = 0
  while current_balance < desired_balance
    current_balance = annual_balance_update(current_balance)
    years += 1
  end
  years
end

until

def self.years_before_desired_balance(current_balance, desired_balance)
  years = 0
  until current_balance >= desired_balance
    current_balance = annual_balance_update(current_balance)
    years += 1
  end
  years
end

loop

def self.years_before_desired_balance(current_balance, desired_balance)
  years = 0
  loop do
    current_balance = annual_balance_update(current_balance)
    years += 1
    return years if current_balance >= desired_balance
  end
end

जैसा कि आपने शायद ध्यान दिया होगा, Ruby में बढ़ाने वाला ऑपरेटर (i++) नहीं है, जबकि कुछ अन्य भाषाओं में होता है। इसके बजाय i += 1 जैसी चीज़ें इस्तेमाल की जा सकती हैं, जो i = i + 1 के बराबर होती हैं।

GitHub के ज़रिए संपादित करें यह लिंक नई विंडो या टैब में खुलता है।

लूप सीखिए

अभ्यास लॉक है

लूप पर अभ्यास करने के लिए 10 और अभ्यास अनलॉक कीजिए