# Thread: Why 1 is ignored in this for loop?

1. ## Why 1 is ignored in this for loop?

I'm taking the book "C programming, a modern approach 2nd edtiion" and for the code/exercise below, why exactly the "1" is not being printed?

I get "5 4 3 2" as output, but then why not 1 as well? What am I potentially missing here?

Code:
```#include <stdio.h>

int main (void)
{
int i, j;
for (i = 5, j = i - 1; i > 0, j > 0; --i, j = i - 1)
{
printf("%d ", i);

}
return 0;
}```

2. The loop ends when either i or j reaches 0. Since j starts 1 lower than i, it reaches 0 before i. Since you are only printing the value of i you see 5, 4, 3, 2.

3. Originally Posted by john.c
The loop ends when either i or j reaches 0. Since j starts 1 lower than i, it reaches 0 before i. Since you are only printing the value of i you see 5, 4, 3, 2.
That's it, thank you very much!

- Dry run the code on pen and paper

Your for loop run condition is (i > 0 , j > 0). There is ",", and this case the compiler uses the right-most condition as the loop run condition.

So your loop is the same as:
Code:
`for (i = 5 , j = i - 1; j > 0; --i , j = i - 1)`
So, you basically start of with i = 5, j =4.

1st run: i = 5, j = 4
2nd run: i = 4, j = 3
3rd run: i = 3, j = 2
4th run: i = 2, j = 1

Now, i =1 and j = 0 which violates loop condition and you don't get the 1 printed.

5. > The loop ends when either i or j reaches 0. Since j starts 1 lower than i, it reaches 0 before i. Since you are only printing the value of i you see 5, 4, 3, 2.

Isn't the right-most condition taken as your loop condition? I remember reading that somewhere so please correct me if I'm wrong.

Going by what you said, (i > 0 , j > 0) is the same as (i > 0 && j > 0).

6. @Zeus, you are absolutely correct! I made a mistake there, somehow reading the comma operator as a &&.
But because it's the comma operator, only the second operand (j > 0) is effective.
Thanks for the correction.

7. Haha, you got me bothered for a few moments there lol. I thought I was learning everything wrong the right way