Locked lesson.
About this lesson
Looping until a condition is met using until loops.
Exercise files
Download this lesson’s related exercise files.
Until Loops.docx59 KB Until Loops - Solution.docx
59.1 KB
Quick reference
Until Loops
Until loops are another loop for your toolbelt.
When to use
There may be specific times when you need an until loop, but it's more just a personal preference.
Instructions
An until loop is similar to a while loop, just from a different angle.
num = 0
until num == 10 do
puts num += 1
end
Hints & tips
- Until loops are similar to while loops
- Until loops execute until your condition is true
- 00:04 Okay, in this video, I want to talk about until loops.
- 00:06 The last video, we talked about the while loop,
- 00:08 my terminal finally uncrashed itself.
- 00:10 So until loop is very much like a while loop, but
- 00:12 the logic is a little bit different, and you'll see it looks the same.
- 00:16 It's just until some condition do, and then do something, right?
- 00:21 So it looks very much like a while loop.
- 00:24 But you'll see in just a second when we actually build one of these,
- 00:29 the logic behind it is a little bit different, and sort of useful.
- 00:33 So let's just go ahead and make one of these.
- 00:35 And just like before, I can create, let's create a counter.
- 00:38 I'm just going to call it num, like we called the number in the last video,
- 00:41 doesn't matter, name it whatever you want.
- 00:43 So, until num = 10 do, and let's do the same thing.
- 00:47 Let's put our num, and
- 00:49 then let's now create our increment assignment operator here.
- 00:54 So if we save this and come down here and run it, we get this same 1, 2, 3, 4,
- 00:58 5, 6, 7, 8, 9, 10, and then it stops.
- 01:01 It keeps working until this condition is met.
- 01:04 Let's create a number = 0.
- 01:06 Let's create a while statement, just to look and
- 01:10 sort of see the difference, while number is less than 10, do something.
- 01:18 number += 1.
- 01:24 Here we go, so you can see they look very much alike.
- 01:29 Same sort of formats, same sort of look and feel to them.
- 01:33 But while this is less than 10, do this,
- 01:35 versus do this until this condition gets met.
- 01:39 In both instances, we're trying to meet some sort of condition.
- 01:44 It's just sort of in your brain which way you want to count down to sort of
- 01:48 categorize these things.
- 01:50 And there are specific reasons why you might want to do it
- 01:53 with an until loop versus a while loop, but they're very, very similar.
- 01:56 So really, it's just a personal preference a lot of the time, so
- 01:59 we can play around with this with a little bit of the time here.
- 02:03 Let's say we can go your number is,and then we can do our string interpolation,
- 02:10 use the hashtag, put that in there, save it.
- 02:14 Now if we run this, you see your numbers 1, your numbers 2,
- 02:19 your numbers 3, so all kinds of stuff.
- 02:22 And I should mention, it really doesn't matter what kind of loop you're doing,
- 02:25 we've just got one line of code here.
- 02:27 You can put an entire block of code here.
- 02:30 You can put whatever you want, as many lines as you want.
- 02:32 You can put other loops inside of your loops.
- 02:35 Let's copy this loop here, and let's change this to 5,
- 02:39 just to make it a little less wieldy.
- 02:41 And now inside of this code block, we can paste in our other loop.
- 02:45 I'm just going to tab this over and same thing here.
- 02:47 Let's change this to 3, and instead of puts-ing out a number,
- 02:51 let's puts out I like cheese, because I do.
- 02:54 And remember, we have to increase increment our counter, so let's save this.
- 02:58 Now what's going to happen here is this is our main loop, this top one, and
- 03:02 it's going to run five times.
- 03:04 And every time it runs, it's going to print out I like cheese three times,
- 03:08 because that's what this loop will do.
- 03:10 So it'll go through one time, I Like Cheese!, I Like Cheese!, I Like Cheese!,
- 03:14 loop back around.
- 03:15 Go through it the second time, I Like Cheese!, I Like Cheese!, I Like Cheese!,
- 03:18 I Like Cheese!.
- 03:18 Loop back around the third time, the fourth time, the fifth time,
- 03:21 until this main loop completes.
- 03:23 So let's save this and run it.
- 03:25 If we've done it correctly, we have your number is 1, I like Cheese!,
- 03:27 I like Cheese!, I like Cheese!.
- 03:29 Your number is 2, I like Cheese!, I like Cheese!, I like Cheese!, number is 3,
- 03:32 4 and 5, and on and on.
- 03:33 So just sort of an example to show you that you can put other loops
- 03:36 inside of this code block, but you can put anything in there.
- 03:39 Other conditional statements, any kind of code that you want,
- 03:42 as much as you want, or as little as you want inside of your loop.
- 03:45 So those are until loops.
- 03:47 In the next video, we're going to look at the for and the each loop,
- 03:50 which are both a little bit different.
- 03:52 They have a little bit different format than these last two loops.
Lesson notes are only available for subscribers.