Βρ

Βρόχοι σε 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 ακόμη ασκήσεις για να εξασκηθείς στην έννοια Βρόχοι