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
There's another type of loop that we can use in Ruby called the Until Loop. 0:00 The Until Loop will continue to run until a certain condition is met. 0:05 It's the opposite of the While Loop 0:10 in that the While Loop runs as long as the condition is true, 0:13 while the Until Loop will run as long as the condition is false. 0:17 And it exits when the condition becomes true. 0:21 Let's take a look at how that works now, using work spaces. 0:25 Using your Ruby work space, let's go ahead and create a new file to create an until 0:30 loop, and we'll call this until.rb. 0:35 Now, we'll do something similar to what we've previously been doing. 0:41 We'll create an answer variable, which is an empty string. 0:44 And this time we'll say, until the answer is no. 0:49 [SOUND] We can print do you want this loop to continue? 0:55 [SOUND]. 1:02 And then just like before, we'll set the answer to standard input. 1:06 Now, let's go ahead and run this and see what happens. 1:14 [SOUND]. 1:16 Do I want this loop to continue? 1:19 Yes. 1:21 Do I want this loop to continue? 1:22 No. 1:23 Oh. 1:25 I entered N but I'm checking for no. 1:26 I'm gonna go ahead and type that out. 1:31 And it exits. 1:34 Now, the reason that we have to have answers set appear 1:37 is because answer will be null. 1:40 If we go into the loop without it. 1:43 If i take that out of there and 1:45 then run this again, we get undefined local variable or method answer. 1:46 So, that's why we need to have answer up front in these loops, so. 1:52 Again, this loop is going to run until the condition. 1:57 Is correct. 2:02 And Until loops and While loops are very similar. 2:05 If we open up this While loop with a number, and I'm going to copy this, 2:08 and then paste it into a new file called until_number.rb. 2:17 We can change the end here and say until 2:24 the answer is greater than or equal to 5 we'll run the loop again. 2:28 We just kind of need to invert what we're looking for. 2:33 So, I'm gonna click back into the console, 2:37 clear the screen and type ruby until_number.rb. 2:42 And we'll print this three times. 2:48 And, now let's also print it six. 2:52 Okay, and then it prints it six times and exits. 2:55 And you'll also notice that in an Until loop and 2:58 a While loop, you can leave off the do if we want to. 3:01 This is purely a matter of preference. 3:05
You need to sign up for Treehouse in order to download course files.Sign up