Locked lesson.
About this lesson
How to use For Loops.
Exercise files
Download this lesson’s related exercise files.
For Loops.docx58.8 KB For Loops - Solution.docx
59.1 KB
Quick reference
For Loops
For loops are another type of loop.
When to use
For loops can be used for most looping things.
Instructions
A FOR loop has this general format:
for (initial counter; conditional; increment) {
// do something;
}
You don't need to increment your counter inside the loop.
Here's an example:
for ($x = 0; $x < 10; $x++) {
echo "Your Number is $x <br/>";
}
Hints & tips
- For loops use less code than while loops
- You don't have to increment for loops inside the counter
- 00:04 In the last video we talked about the while loop,
- 00:07 in this video I wanna talk about the for
- 00:09 loop, very similar to the while loop just slightly different formatting.
- 00:12 So let's take a look at that the for loop has this general look,
- 00:19 it's for initial counter;conditional;and increment.
- 00:25 And then we have this same do something.
- 00:30 So in the while loop, remember we set our counter outside of the loop.
- 00:36 And then we incremented the counter inside the loop.
- 00:40 Like that.
- 00:41 Well with the for loop we don't have to do any of that.
- 00:43 It takes care of it all right here inside this section.
- 00:47 So.
- 00:49 Let's go for x = 0.
- 00:52 And then our condition is let's say,
- 00:57 as long as x < 10, and then to increment
- 01:02 it we go x++ oops we need a variable x++.
- 01:08 And then whatever we wanna do in the loop,
- 01:12 we type it right here so let's go echo "Your number is x".
- 01:17 And that's it, in the past for our while loop we would've went x++ but
- 01:22 like I said we don't have to do that here, we've laid that out already right up here.
- 01:27 So basically it takes everything that a while loop does and
- 01:30 smooches it up in this one line right here, and takes care of it for you.
- 01:34 And I forgot to put a line break so let's do that, just so
- 01:36 we can read this a little bit better.
- 01:38 Number 0, 1 2 3 4 5 6 7 8 9, so very, very similar to a while loop,
- 01:43 in fact let's look at a while loop and just sorta compare them.
- 01:48 While x < 10 and I'm just gonna copy this,
- 01:53 and of course we need to actually let's change this to y so
- 02:00 things don't get get confusing.
- 02:08 And of course remember we have to
- 02:11 initialize our variable outside of the loop.
- 02:14 So if we save this and I'm gonna put a couple of
- 02:21 line breaks here, just to separate these two.
- 02:25 We have the exact same output for our for loop and our while loop.
- 02:31 Let's get rid of this.
- 02:32 So you can see the difference.
- 02:34 I said earlier its a good idea to write elegant code,
- 02:37 to write as little amount of code as possible.
- 02:39 And under those circumstances, the for
- 02:41 loop seems like the one to use because it's less code, it's more compact,
- 02:46 it's all in one place, it makes a lot of sense, whereas this is more lines of code.
- 02:51 And it's just one of those times you go, who cares?
- 02:54 You know what I mean?
- 02:54 I like while loops so I use while loops, and I think the reason is,
- 02:58 is because I often forget the order that these three things go in.
- 03:02 So I'll try and increment first and assign it later.
- 03:05 It's just one more thing for me to have to memorize, and
- 03:08 I don't like to memorize things, so
- 03:10 I just tend to use the while loop because it's just like an if statement.
- 03:12 It looks just like an if statement.
- 03:14 And it is pretty easy to use.
- 03:15 But on the down side,
- 03:17 sometimes I forget to put my increment in it brings out and goes on for ever.
- 03:21 So, it really is just a personal preference which one of these loops
- 03:25 you wanna use, whatever you feel like doing, like I said there are some
- 03:29 very minor circumstances where you might be forced to use a for loop.
- 03:33 But offhand I can't really can't think of any of those.
- 03:36 They're pretty much interchangeable.
- 03:37 So those are loops.
- 03:39 In the next video, we're gonna sort of take a step back and
- 03:41 play around a little bit with all of this stuff that we've just learned.
- 03:44 And built something called "Fizz Buzz", it's a little game that you use for
- 03:47 interviews and that's a lot of fun.
- 03:49 And that will be the next video.
Lesson notes are only available for subscribers.