This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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.
Code Samples
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
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up