#Lesson 2 - For Loops
Students will be able to create for loops, take repeatable tasks and convert them to loops, and iterate through an array using loops.
- Loops let you run the same operation over and over again.
- A
forloop completes the same actions as awhileloop but does so using less code, so it is preferred.
- Do Now
- Exit Ticket
- Students will show progress toward reaching the objective based on their performance on the Exit Ticket.
- Iterate
forloopwhileloopbreakstatementreturncontinuestatement
- Attendance
- Return reviewed Do Now and Exit Ticket from previous class.
- Do-now
Today, we will learn about for loops. for loops are similar to while loops in that they will complete the same action. However, for loops are completed with less code.
NOTE: For this reason, you should use for loops moving forward on all projects.
####Create a for loop.
for (var i=0; i<2; i=i+1)
{
console.log(i);
}
console.log("Loop terminated");
This for loop performs the same exact operations as the while loop in the last lesson. The only difference is the syntax; for loops are more compact and thus require less line of codes. Because we're programmers and we like to do things with the least amount of effort as possible, we prefer to use for loops when we can.
Let's run through this code line-by-line too. [Repeat above exercise.]
#####Repeat operations
Suppose I want to print the sequence 0 to 100, I could do that using:
console.log("0");
console.log("1");
console.log("2");
console.log("3");
console.log("4");
console.log("5");
console.log("6");
console.log("7");
console.log("8");
...
OK, this is taking too much time. We programmers like to do things with the least amount of effort as possible. So let's do this in a loop instead:
for (var i=0; i<101; i=i+1)
{
console.log(i);
}
This prints:
0
1
2
3
...
100
In four lines, we accomplished with a loop something that would have taken 101 lines without a loop!
####Exiting the loop using break and return.
What if we wanted to exit the loop before the exit condition was met? We could do this by doing the following:
for (var i=0; i<2; i=i+1)
{
if (i == 1)
break; // OR return
else
console.log(i);
}
console.log("Loop terminated");
This will cause the loop to run only one iteration.
#####Output
0
Loop terminated.
Let's break this down line-by-line:
iis set to 0.- Is
iless than 2? Yes, because 0 is less than 2. Therefore, the condition is true and proceed to execute the body of the loop. - Is
iequal to 1? No, so run theelsestatement. igets printed as 0.igets incremented. It is now equal to 1.- Is
iless than 2? Yes, because 1 is less than 2. Therefore, the condition is true and proceed to execute the body of the loop. - Is
iequal to 1? Yes, so run thebreakstatement. This exits the loop. Loop terminatedgets printed.
####Skipping to the next iteration using continue
What if we wanted to skip to the next iteration of the loop without running the remaining code in our body? We could do this by doing the following:
for (var i=0; i<2; i=i+1)
{
if (i == 0)
continue;
console.log(i);
}
console.log("Loop terminated");
#####Explanation
This will cause the loop to skip printing i on the first iteration.
#####Output
1
Loop terminated.
Let's break this down line-by-line:
iis set to 0.- Is
iless than 2? Yes, because 0 is less than 2. Therefore, the condition is true and proceed to execute the body of the loop. - Is
iequal to 0? Yes, so skip running the rest of the body and proceed to the next iteration of the loop. - Then,
igets incremented. It is now equal to 1. - Is
iless than 2? Yes, because 1 is less than 2. Therefore, the condition is true and proceed to execute the body of the loop. - Is
iequal to 1? No, so skip theifstatement and go to the next line in the body. igets printed as 1.igets incremented. It is now equal to 2.- Is
iless than 2? No, because 2 is not less than 2. Therefore, the condition is false so we must terminate the loop and run the code after it. Loop terminatedgets printed.
Now we're going to work with loops together.
- How do I write a
forloop that prints "Taylor Swift" 10 times? - How do I write a
forloop that iterates 5 times and prints out the sequence 0 to 4? - How do I create a
forloop that counts down from 10 to 1?
In your "loops" workspace from last lesson complete the following:
- Create a new file called
for.html. - Add the jQuery CDN and have a single image in the html file with this image in it. (The image can also be found at http://imgur.com/GPRGZBB).
- In the
script.jsfile create aforloop that adds another image of a bottle of milk until the page reaches 100 bottles.
Bonus: After the page counts up to 100 bottles have it count back down to 0 bottles.
Give the Exit Ticket.
Today, you learned about `for loops. This is important because loops can be used to repeat an operation. They can also be used to iterate through an array. Next, we will work on a project that connects our knowledge of arrays and loops together.
Homework can be found here.
- Review the Do Now & Exit Ticket.
- Prepare for next lesson / hand off to next volunteer in rotation.
