Loops

Follow along with code examples here!

Table of Contents:

Slides

Intro to Iteration

Iteration is the repetition of a process, getting closer to some result each time.

You are assigned the task of flipping a coin 100 times and documenting the result of each. To do this you will:

  1. Gather your materials (pen, paper, a coin)

  2. Until you've flipped 100 times you will:

    1. flip a coin

    2. record the result by writing: "flip number [flip number] was [heads|tails]"

  3. Make sure to increase the flip number after each flip.

Doing this by hand certainly would take a while. We can write code to likely do it faster:

But even so, we have to manually update each number. What a pain! If only there was some way to do this more efficiently.

The for loop

for loops are best used to repeat a process a known number of times. The syntax looks like this:

Armed with this syntax, we can easily loop 100 times!

Try using the debugger and you will see the order of operations

  1. Initialize (let i = 0)

  2. While the condition is true (while i < size returns true)

  3. Execute the code block

  4. Update the counter (i++)

  5. Go to 2

Even in the most basic programming languages, the concept of the GOTO statement has existed. Programmers used to have to label a specific line with a name like "StartLoop" and then write a statement to Go To StartLoop if they wanted to repeat a portion of code. In high-level programming languages like JavaScript, that is abstracted away for us by the for loop.

For Loop Challenge:

Write a program that does the following:

  1. Asks the user to enter a number.

  2. Flips a coin that many times, keeping count of how many times heads was flipped.

  3. Prints back the final number of heads to the user like this:

Here is some starter code:

Check out the solution!

While Loops and Infinite Loops

An infinite loop is one in which the condition is ALWAYS true. This will cause a program to run forever, either depleting resources or just causing the computer to stall while it waits for the program to end.

Infinite loops are most often created using while loop which are best used to repeat a process an unknown number of times.

To ensure that a loop does not go on infinitely, we use these two statements:

  • break; prematurely breaks out of a loop

  • continue; prematurely goes to the iteration step of the loop

Q: How is break different from return? A break statement will exit the current loop and continue executing code that follows the loop. A return statement inside of a loop will exit the current loop AND the current function or program being executed.

While Loop Challenge

Write a program that does the following:

  1. Generates a random number from 1-10.

  2. Asks the user to guess the number.

  3. If the user is correct, print a message congratulating them and end the program.

  4. If they are incorrect, ask them again.

Bonus Features

  1. Keep track of their guesses and when they guess correctly, tell them how many guesses it took for them to get it right.

  2. Limit their guesses to 5 guesses. If they guess 5 times incorrectly, they lose!

Check out the solution!

Nested Loops

A nested loop is a loop written inside the body of another loop. For each iteration of the outer loop, the inner loop will complete ALL of its iterations.

Last updated