Locked lesson.
About this lesson
Understanding for and each loops, and when to use them.
Exercise files
Download this lesson’s related exercise files.
For and Each Loops.docx60.1 KB For and Each Loops - Solution.docx
59.1 KB
Quick reference
For and Each Loops
Let's learn the for and each loops.
When to use
As with most loops, using them is largely a personal preference.
Instructions
To create a for loop:
for number in 0..10
puts number
end
To create an each loop:
(0..10).each do |number|
puts number
end
Hints & tips
- Use for loops to loop within a range
- Use each loops to loop over each item
- 00:00 In this video, I want to look at for loop and an each loop.
- 00:07 And these are,
- 00:08 pretty dramatically different than the last two loops that we used.
- 00:11 So a for loop will start there.
- 00:13 A for loop, it runs a specific number of times over a range of numbers.
- 00:17 And the best way for me to explain it, is to just show it to you.
- 00:20 So we go, for and
- 00:22 let's go number, it's just a variable in, and now we set a range.
- 00:27 So from 0, let's say 2, 5, and that dot, dot just means a range from 0 to 5.
- 00:34 So then we have an end, and inside of here, it does whatever we want it to do,
- 00:39 whatever our code block happens to be.
- 00:41 So let's just put some number, so if we save this, you'll notice,
- 00:45 before we declared we went like number = 0.
- 00:50 We don't have to do that here.
- 00:51 And if we save it, four designates this on its own, so use it in this range.
- 00:58 So from 0 to 5 number starts with 0 and it goes to 5,
- 01:02 befor it slaps these numbers right here into the variable.
- 01:07 So we see 0, 1, 2, 3, 4, 5, we could put something weird like,
- 01:13 I love cheese, right?
- 01:15 So if we save this, and run it, it just prints out,
- 01:20 I love cheese, 1, 2, 3, 4, 5, 6 times.
- 01:23 So 0, 1, 2, 3, 4, 5, that's 6 things, so that's a for loop, kind of weird, right?
- 01:29 But there's definitely reasons why you might want to use that, and
- 01:32 we'll see those later on.
- 01:33 So now, I want to talk about the each loop.
- 01:34 The each loop is equally kind of different than the first two loops
- 01:38 that we looked at.
- 01:39 And the format of an each loop is something.each,
- 01:44 and then do, and then we have this variable.
- 01:50 And then here's our code, whatever we want to do, and then end.
- 01:53 So this is the format, so this looks kind of weird, right?
- 01:56 But it's actually very useful.
- 01:57 So let's just go ahead and make one of these things.
- 01:59 Let's create a range like we did before from 0 to 5,
- 02:03 and we can say, .each for each item in this range.
- 02:08 So for 0, do this for 1, do this for 2, do this, all the way up to 5. do.
- 02:14 And then let's create a variable, and let's just call num, and
- 02:17 let's just puts num, and then end.
- 02:21 So remember for a for loop we didn't have to declare what num was it sort of did it for
- 02:26 us here.
- 02:27 We sort of have to declare it and we just do it like that.
- 02:29 So for save this and run it, we get 0, 1, 2, 3, 4, 5.
- 02:33 Likewise, we can do the same, I love cheese, because I really do.
- 02:38 And it just does I love cheese five times.
- 02:41 So this seems a little bit weird but let's really quickly,
- 02:47 Create an array with John, Tim, and Mary.
- 02:53 So now we can use instead of this range of numbers we can say, for
- 02:58 each item in the names array do this.
- 03:02 So what do you think this is going to do?
- 03:05 It's going to say, I Love Cheese three times because we have three
- 03:09 items in our array.
- 03:10 We could also print out our array, we can go puts num.
- 03:15 Now we run this we get John, Tim, and Mary.
- 03:17 So lots of ways to loop through an array but man, this is very simple.
- 03:22 And that's the each loop.
- 03:22 So, for loop ,each loop, while loop,
- 03:25 until loop you have four solid loops to choose from for different reasons.
- 03:29 You could loop through an array with a while loop just as easily.
- 03:31 So I'll leave that to you to sort of work your way through that.
- 03:34 In the next video we're going to look at FizzBuzz.
Lesson notes are only available for subscribers.