# For ... Loops

In programming, a loop is a structure that lets you tell the interpreter that you want to repeat an action, or a series of actions, multiple times.

As an example, let's say that we want to write out the numbers from zero to 9.

for (let i = 0; i < 10; i = i + 1) {
  console.log(i);
}
1
2
3

This is an example of a for loop. Between the curly braces we can write as much or as little code as we want. After the for between the parenthese we put three things, separated with semi-colons.

  1. Define a counter variable that you will use to keep track of how many times you looped.
  2. Write a test condition (just like inside an if statement) so the loop knows when to stop looping.
  3. Write an expression that defines how to alter the counter variable each time you reach the end of the loop.

For...loops are just the first of many kinds of loops that you will learn. However, they all need to know the answers to those three questions to be able to run. It will just be how they get those values that changes.

Back to Week 3 main page

Last Updated: 6/13/2020, 11:30:19 PM