Ци

Цикли у 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 вправ, щоб практикувати концепцію Цикли