The Until Loop3:08 with Jason Seifer
The until loop will continue to run until a certain condition is met. It's the opposite of the while loop in that while loops run as long as the condition is true, while the until loop will run as long as the condition is false and it exits when the condition becomes true.
Example simple until loop:
answer = "" until answer == "no" do print "Do you want this loop to continue? (y/n) " answer = gets.chomp end
Example until loop used with a number based conditional exit:
def print_hello(number_of_times) i = 0 while i < number_of_times puts "hello" i += 1 end end answer = 0 until answer >= 5 print "How many times do you want to print 'hello'? Enter a number greater than 5 to exit) " answer = gets.chomp.to_i print_hello(answer) end
You need to sign up for Treehouse in order to download course files.Sign up