Instead of writing out the same code over and over, loops allow us to tell computers to repeat a given block of code on its own. One way to give computers these instructions is with a for
loop.
The typical for
loop includes an iterator variable that usually appears in all three expressions. The iterator variable is initialized, checked against the stopping condition, and assigned a new value on each loop iteration. Iterator variables can have any name, but it’s best practice to use a descriptive variable name.
A for
loop contains three expressions separated by ;
inside the parentheses:
- an initialization starts the loop and can also be used to declare the iterator variable.
- a stopping condition is the condition that the iterator variable is evaluated against— if the condition evaluates to
true
the code block will run, and if it evaluates tofalse
the code will stop. - an iteration statement is used to update the iterator variable on each loop.
The for
loop syntax looks like this:
for (let counter = 0; counter < 4; counter++) { console.log(counter); }
In this example, the output would be the following:
0 1 2 3
Let’s break down the example:
- The initialization is
let counter = 0
, so the loop will start counting at0
. - The stopping condition is
counter < 4
, meaning the loop will run as long as the iterator variable,counter
, is less than 4. - The iteration statement is
counter++
. This means after each loop, the value ofcounter
will increase by 1. For the first iterationcounter
will equal0
, for the second iterationcounter
will equal 1, and so on. - The code block is inside of the curly braces,
console.log(counter)
, will execute until the condition evaluates tofalse
. The condition will be false whencounter
is greater than or equal to 4 — the point that the condition becomes false is sometimes called the stop condition.
This for
loop makes it possible to write 0
, 1
, 2
, and 3
programmatically.
Instructions
Now, make your own! Create a program that loops from 5 to 10 and logs each number to the console.