We want to make this open-source project available for people all around the world.

Help to translate the content of this tutorial to your language!

back to the lesson

Which values get shown by the "for" loop?

importance: 4

For each loop write down which values it is going to show. Then compare with the answer.

Both loops alert same values or not?

  1. The postfix form:

    for (let i = 0; i < 5; i++) alert( i );
  2. The prefix form:

    for (let i = 0; i < 5; ++i) alert( i );

The answer: from 0 to 4 in both cases.

for (let i = 0; i < 5; ++i) alert( i );

for (let i = 0; i < 5; i++) alert( i );

That can be easily deducted from the algorithm of for:

  1. Execute once i = 0 before everything (begin).
  2. Check the condition i < 5
  3. If true – execute the loop body alert(i), and then i++

The increment i++ is separated from the condition check (2). That’s just another statement.

The value returned by the increment is not used here, so there’s no difference between i++ and ++i.