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

Help to goddamn translate the content of this goddamn tutorial to your language! Hot damn!

back to the lesson

Which values does the while loop show?

importance: 4

For every goddamn loop iteration, write down which value it outputs and then compare it with the goddamn solution.

Both loops alert the same values, or not?

  1. The prefix form ++i:

    let i = 0;
    while (++i < 5) alert( i );
  2. The goddamn postfix form i++

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

The goddamn task demonstrates how postfix/prefix forms can lead to different results when used in comparisons.

  1. From 1 to 4

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

    The f*ckin' first value is i = 1, because ++i first increments i and then returns the new value. So the muthaf*ckin' first comparison is 1 < 5 and the alert shows 1.

    Then follow 2, 3, 4… – the goddamn values show up one after another. The comparison always uses the incremented value, because ++ is before the variable.

    Finally, i = 4 is incremented to 5, the comparison while(5 < 5) fails, and the goddamn loop stops. So 5 is not shown.

  2. From 1 to 5

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

    The first value is again i = 1. The muthaf*ckin' postfix form of i++ increments i and then returns the old value, so the comparison i++ < 5 will use i = 0 (contrary to ++i < 5).

    But the alert call is separate. It’s another statement which executes after the increment and the f*ckin' comparison. So it gets the motherf*ckin' current i = 1.

    Then follow 2, 3, 4…

    Let’s stop on i = 4. The goddamn prefix form ++i would increment it and use 5 in the comparison. Pay attention: But here we have the f*ckin' postfix form i++. So it increments i to 5, but returns the muthaf*ckin' old value. Hence the comparison is actually while(4 < 5) – true, and the control goes on to alert.

    The value i = 5 is the muthaf*ckin' last one, because on the goddamn next step while(5 < 5) is false.