Loop explination?

while (count<12) {
document.write("special<br/>");
count++;

Could someone explain to me how the count++ works? I’m reading on this and I don’t quite grasp it. Otherwise the loop will go on forever.

The loop keeps happening because the increment keeps adding 1 and until the loop is out of range will the loop stop, and I suppose that is the whole point of loops ? :slight_smile:

Great help so far from you, I appreciate.

You’re right, in that the while loop has to keep on returning to where it starts.

The loop keeps on happening while the condition is true, which in this case is that the variable called count contains a number that is less than 12.


while (count<12) {
    document.write("special<br/>");
    count++;
}

Here is how it is interpreted:

  1. Is the count less than 12? It’s undefined, so yes.
  2. document.write(…);
  3. Increase count by 1
  4. End of while loop, recheck the condition.
  5. Is the count less than 12? It’s 1, so yes.
  6. document.write(…);
  7. Increase count by 1
  8. End of while loop, recheck the condition.
  9. Is the count less than 12? It’s 2, so yes.
  10. document.write(…);
  11. Increase count by 1
  12. End of while loop, recheck the condition.
  13. Is the count less than 12? It’s 3, so yes.

14-42 not shown for brevity.

  1. Increase count by 1
  2. End of while loop, recheck the condition.
  3. Is the count less than 12? It’s 11, so yes.
  4. document.write(…);
  5. Increase count by 1
  6. End of while loop, recheck the condition.
  7. Is the count less than 12? It’s 12, so no.

The while loop now ends and execution occurs after the while loop

You have to increment to me it seems like incrementing is telling the loop to start at the beginning, unless that is what it is doing ? :slight_smile:

That’s due to the condition is (count < 12) which doesn’t automatically increment.

I suspect that it’s a learning assignment where you’re expected to learn how to use a while loop.

The count for the while loop would need to be incremented either from within the loop, or from within the condition itself.

Why do you need to add a increment of 1 to the while command ? Shouldn’t the variable just stop when it reaches just under 12?

Does this updated version of that code help you to understand what’s happening?


var count = 0;
while (count < 12) {
    document.write("special<br/>");
    count = count + 1;
}

The indentation becomes important, as it helps to aid your understanding of the code.

The ++ operator that you’re asking about is a shortcut that is brought over from the c language.

The following three code snippets all demonstrate the same process of adding 1 to a value.
They all have different pros and cons that relate to them, with the second example being my personal favourite one to use.

This snippet uses a normal arithmetic operator to perform an addition.


count = count + 1;

This one uses an assignment operator called +=


count += 1;

And here is the increment operator ++ in use.


count++;