Why doesn't the logic i == 0 work?


#1

In both challenges 8 and 16 we need to loop through integer values.

The logic below works
for (i = j; i > -1; i-= 3) // why did the syntax i == 0 not work in 2nd parameter??


#2

To help you answer your own question, run the following loop:

The loop with i > -1:

...
for (i = j; i > -1; i-= 3)
{
   printf ("--> i = %d\n", i);
}
...

And check the output to see if the value of i ever becomes zero.

Now, the loop with i == 0:

...
for (i = j; i == 0; i-= 3)
{
   ...
}
...

If the value of i doesn’t become zero, then the value of the expression i == 0 will be false and the loop will terminate.


#3

Thanks I understand now. This is not working the way I interpreted. Each time the loop executes it is either true or false and will exit on false. brain cramp on my side. not sure why I thought it would continue looping until it equaled zero.