Locked lesson.
About this lesson
Looping through things using For loops.
Exercise files
Download this lesson’s related exercise files.
For Loops.docx58.9 KB For Loops - Solution.docx
59 KB
Quick reference
For Loops
For loops allow us to loop through conditions.
When to use
Use a For loop any time you want to loop through a condition and take some action.
Instructions
The basic struction of a For loop is this:
for (statement1; statement2; statement3) {
//do something
}
Statement 1 is the initial counter variable.
Statement 2 is the condition we'll be testing against.
Statement 3 does something to the counter (increases it or decreases it).
example:
for (i = 1; i < 10; i++){
document.write(i + " is less than 10...<br/>");
}
Hints & tips
- For loops are a great way to loop through conditions
- 00:04 We've talked about If statements and
- 00:06 switch now it's time to move into the loops, and loops are great.
- 00:08 They're another one of those fundamental programming concepts that all programming
- 00:12 languages have, and you'll use loops all the time for a bunch of different things.
- 00:16 So, what exactly is a loop?
- 00:18 Well basically a loop is just what it sounds like,
- 00:20 it allows us to loop through things and do them over and over programmatically.
- 00:24 Now there're several different types of loops and
- 00:27 they all do things slightly differently and you'll learn as you get more
- 00:30 experience which type of loop to use in different circumstances.
- 00:33 In this video we're gonna look at For loops.
- 00:36 And For loops loop through a block of code a number of times and
- 00:40 they basically have this general format.
- 00:44 I can look at this and its for that's our For loop, and then we have statement one,
- 00:48 statement two, and statement three, separated by semicolons, and
- 00:52 I'll talk about these in just a minute.
- 00:54 And then inside of here, here's the code that we're gonna loop through.
- 00:59 So what are these weird statement things?
- 01:01 What's going on here?
- 01:02 So statement one is executed before the loop starts right away, okay.
- 01:07 Statement 2, let's us defined a condition, sort of like an if statement,
- 01:10 remember there's a condition.
- 01:12 And statement 3, gets done after we have looped through one time.
- 01:16 So, every time we looped through, after we loop we'll do statement 3.
- 01:19 So, statement 1, you start, statement 2 is your condition and
- 01:23 statement 3 is whatever you wanna do after you have looped.
- 01:26 So it sounds a little bit confusing but let's just go ahead and build one and
- 01:29 we'll see that it's not really that bad at all.
- 01:31 So I'm just gonna delete this and let's go for, call it a For loop,
- 01:37 and let's create a variable i = 1; that's our statement one.
- 01:42 And for our condition, let's say i < 10.
- 01:46 So any time i is less than 10, we wanna keep looping.
- 01:50 And statement three at the end.
- 01:52 What we wanna do is, let's increment i one time.
- 01:55 Remember our incremental?
- 01:57 So then, we just open and close our brackets and
- 02:01 now anything here in the middle.
- 02:03 This is what's we're gonna loop through.
- 02:06 So let's go document.wright, and let's say what?
- 02:11 Let's just print out the number to the screen.
- 02:14 And let's give it a line break so that it's not all on one line.
- 02:20 Okay so, we start out i as 1.
- 02:22 We check to see if it's less than 10.
- 02:24 And then we increment and we write it out to the screen.
- 02:26 So let's go ahead and click reload.
- 02:28 And we can see 1, 2, 3, 4, 5, 6, 7, 8, 9.
- 02:30 Why did it stop at 9?
- 02:32 Because when we get to 10, after 9, it increments with our statement three.
- 02:36 And it becomes 10 and then it starts over and it says is i < 10.
- 02:40 Is 10 < 10?
- 02:44 No, 10 is not less than 10.
- 02:45 So in that case the whole thing stops.
- 02:48 So this keeps going as long as this is true.
- 02:51 So we've got this less than, we can use our conditional statements greater than,
- 02:56 greater than equal to, equal whatever we want, we can use there, so
- 03:00 that's pretty much it.
- 03:01 And a For loop is kind of the more complicated loop because we have all these
- 03:04 different moving parts, there's all this stuff going on,
- 03:07 it seems a little bit complicated.
- 03:08 And like I said you'll use loops all the time for all kinds of different things.
- 03:12 They're just a fundamental programming building block one of those things.
- 03:16 So that's For loop.
- 03:17 In the next video we'll look at While loop.
Lesson notes are only available for subscribers.