1

I have written the following loop:

for (i = 10; i--; i > 5) {
  console.log(i);
}

Which outputs this to the console:

9
8
7
6
5
4
3
2
1
0

Can anyone tell me why it's doing this?

I had a look and it seems like it's just a simple matter of having to increment using the third parameter of the for loop, rather than the second, but I'm fascinated as to the digits logged to the console. Can anyone give me a simple explanation of why it outputs the numbers it does?

3
  • I'm getting output as 10,9,8....3,2,1,0. I'm running this script in chrome browser on windows 7 box. Are you sure with the output you are seeing? Commented Mar 27, 2016 at 14:58
  • My bad! I was trying with initial value of i = 11. Commented Mar 27, 2016 at 15:07
  • Yeah, there was some confusion with a tab opening up where I didn't expect it to when copy and pasting. The question has been amended now. Commented May 15, 2016 at 11:29

3 Answers 3

4

The for loop has this structure:

for (initial condition ; termination condition ; incremental condition)

In your for loop, the termination and incremental condition are swapped (!):

for (i = 10; i--; i > 5)

That is:

initial condition: i = 10
termination condition: i-- !!!
incremental condition: i > 5 ??? Does not do incremental

Then, as long as the termination condition returns true (that is, when i > 0), the loop continues.

And since i, having an initial value of 10, will only become 0 after the i-- is executed 10 times, the result returned was:

9 8 7 6 5 4 3 2 1 0

Totaling 10 loops.

Sign up to request clarification or add additional context in comments.

13 Comments

What is it about i-- which causes it to act differently from what might appear to be its intended purpose? That is: why does i-- function as one might expect i > 0 to in this particular context? In what manner is the lower level processing involved similar?
The placement of i-- is in wrong place. It is supposed to be placed as incremental (or rather, decremental) condition. But, in this context it is placed as termination condition (yes, the text book name is weird: termination condition, not continuation condition.) So what happen in this case is, since your i-- is placed as termination condition, it behaves as expected as termination condition, that is, the loop continues so long as termination condition is true -> i > 0. But the entire loop itself is wrong because i-- is supposed to be placed as incremental condition instead.
But your i-- is not behaving as expected as incremental (or decremental) condition, because it has never been placed as incremental (or, decremental) condition at all. That is the issue with the for loop you show.
As for the reason why your digit starts from 9, that is because the process is as follow: 1) check the condition, 2) decrement the value, 3) enter the loop. That is why in the first step, when the check of value is done, i value is actually 10 (according to point (1)), but since it is decremented because of point (2) before printing, that causes when it is printed (point (3)), it shows 9 instead of 10 - which is the value when it was checked. And that is also the reason why the last printed value was 0 and not 1, because of the step (1), (2), (3) are executed as ordered above.
var me = "Reads above data with increasing fascination."
|
1

The loop did not finish when you expected because Boolean(i--) is false only if i == 0; in short, you misplace compare and action in the loop.

2 Comments

So, what function is i-- performing at the level below the visible level, causing Boolean(i--) to return true when i == 0? Also, why is it not i <= 0? Also, given i > 5 shouldn't be an incremental condition at all, why is the loop incrementing by one step?
i-- produces digits, 9876543210, as i-- is placed on condition place it checking if result of i-- is true, so, long story is short, it happens only when i eq zero. i>5 does not change i, and so, not affecting the loop.
-1

Question is now corrected so ignore: Firstly, you are wrong - it does not produce the output you indicated :). Instead it produces the following:

for (i = 11; i--; i > 5) {console.log(i);}
// 10 9 8 7 6 5 4 3 2 1 0

It starts with 10, not with 9 :).

Secondly, you are not allowed to change the order of statements in the for block. Instead, you must specify them as shown:

for (i = 11; i > 5; i--) { console.log(i); }
// 11 10 9 8 7 6

Third (and most important):

  1. You put a starting value in i; whatever value you want or need.
  2. Then you create the condition which checks whether to continue looping.
  3. The third statement is usually used to increment or decrement i.

1 Comment

What do you suggest was good about the answer? It didn't really address my question, which wasn't 'is this something I am allowed to do?' but rather 'why, when I do this, does it output that particular thing?'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.