#Lesson 1 - While Loops
Students will be able to create while 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.
- Typically, loops go over an array. This is useful when you want to do something with every element in an array.
- Usually, loops have an exit condition. That is, some criteria must be met in order to end the running of the loop.
- A loop that does not terminate is called an infinite loop. As programmers, we seek to avoid these because they freeze our program.
- Do Now
- Exit Ticket based off assessments from current lesson.
- Students will show progress toward reaching the objective based on their performance on the Exit Ticket.
- Iterate
- Initialization
- Increment / Decrement
- Halt
- Exit condition
- Loops
- While Loops
- Arrays
- Attendance
- Return reviewed Do Now and Exit Ticket from previous class.
- Do Now
Today, we will learn about while loops. This is important because loops are a tool that programmers can use to run the same operation over and over again. They are also useful for iterating through an array (which we will learn next) and processing each element in it.
When we take the bus and pay with change, the machine keeps counting every coin we deposit until we run out. The machine also calculates the total amount of money after each coin has been counted to check if we have deposited enough to pay the fare. Once we have, the machine stops counting and we hear a beep so that we know we can stop putting money in. Someone give me another example of a loop in real life.
####Create a while loop.
var i=0;
while(i < 2)
{
console.log(i);
i = i+1;
}
console.log("Loop terminated");
Let's break this down line-by-line:
- In line 1, we're creating a variable called
iand setting it equal to 0. Because we're setting the value ofifor the first time, we can say we're initializingito 0. - In line 3, we are checking if
iis less than 2. This is our exit condition; ifiis ever not less than 2, the loop's body will not run. An exit condition means that some criteria must be met in order to end the running of the loop. - However, if the condition is true, line 5 prints out
iand line 6 increases, or increments, the value ofiby 1. If increments mean to increase, a decrement means _________? - Line 9 only runs once the loop has terminated.
####Output
Let's look at the output:
0
1
Loop terminated.
Let's also 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. 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. 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.
Based off this, we can say the loop's body ran two times or went through two iterations. Notice that the value of the last printed i is one less than the number in the condition.
####Infinite while loop
What happens if we do not include the i=i+1 line? Something like:
var i=0;
while(i < 2)
{
console.log(i);
}
console.log("Loop terminated");
After the first iteration of the loop, i will not change. In fact, i will never change! Thus, the i<2 condition will always be true. This means the loop will run forever. Your program will freeze, or halt, and the Loop terminated statement will never run.
This is bad and we want to avoid this mistake by making sure we are changing our index so that the exit condition is eventually true.
Now we're going to work with loops together. We can prototype this in JS Bin.
- How do I write a
whileloop that prints "Justin Bieber" 10 times? - How do I write a
whileloop that iterates 5 times and prints out the sequence 0 to 4? - How do I write a
whileloop that prints "Can't get enough of the Biebs" forever?
Create a GitHub repository called "loops" and clone it to a new Cloud9 workspace with the same name.
- Create two files,
while.htmlandscript.js - Add the jQuery CDN and have a single empty
<div>in the HTML file. - In the
script.jsfile create a while loop that counts from 1 to 100. - Apend the number to the blank HTML
<div>so that the number can show up on the HTML page.
Give the Exit Ticket.
Today, you learned about while loops. What does a while loop do? Why is this useful? [Possible student answer: This is important because loops can be used to repeat an operation. They can also be used to iterate through an array which we learn about soon.] Next session, we will learn about for loops.
There is no homework for this session.
- Review the Do Now & Exit Ticket.
- Prepare for next lesson / hand off to next volunteer in rotation.
