# Thread: New to C programming- Need help understand this logical expressions

1. ## New to C programming- Need help understand this logical expressions

Code:
```int main()
{
int  i = 1, j = 1, k = 1;

printf("%d ", ++i || ++j && ++k);
printf("%d %d %d ", i, j, k);

return 0;
}

For the first printf statement the output is 1.
The second statement's output is 2 , 1, 1.

I thought the results would have been:
First statement 1.
Second statement 2, 2, 2.

Can someone please explain how this works?```

2. Hint: the || and && operators have a "short circuit" behaviour such that if evaluating the left operand is sufficient to determine the value of the whole expression, the right operand is not evaluated.

3. Is the left operand (++i || ++j)? If so, doesn't the value of j change because it is incremented before it is evaluated?

4. What is the relative precedence of || and &&?

EDIT:
Actually, grouping does not matter so much for the value of j, though it matters for the value of k. Still, it is good to know how those sub-expressions are grouped.

5. It evaluates the left operand first, then the right operand. RIGHT?

6. Yes, if the left operand is not sufficient to determine the value of the expression. That is, in the case of ||, if the left operand evaluates to a false value (i.e., zero), the right operand is evaluated, otherwise the right operand is not evaluated.

7. Okay, i think i am getting it. Because ++1 has a value greater than zero, there is no need for the expression to increment the value of j or k?

8. Yes.

9. Okay, thank you very much. This expression was killing me, but I get it!!!!

10. O_o

Perhaps?

Code:
```if(true && evaluated);
if(false && not_evaluated);
if(true || not_evaluated);
if(false || evaluated);```
Soma

11. Thanks Soma... this too makes sense and it's an easy reference.

12. For the record even though you made a thread here, it's basically the same issue. Don't make a thread for every Boolean expression you come across.

The NOT operator makes the result of the operand opposite. So if the result would be true, NOT true is false. NOT false is true. If you NOT a NOT expression, you have to flip the result again. You can do this many times.

It works with a single operand, so generally the closest variable or literal is the operand. For anything else, use parenthesis.

13. 1st: Okay I'll remember that... new to system. But if you post a new question to an old thread, how will it get noticed?

2nd: I'm still not sure I get this expression: Since !i is true, then with the double negative it flips the true to false? Then it evaluates !j which is true?

14. Well to start with i is 2. In C, any non-zero value evaluates to true.

So if we evaluate again - !i is false, then NOT again makes it true. Then !j is false.

The boolean expressions are converted to integers when they are added - so true is made 1 instead of "any number excluding zero"

15. I get it now! I understood this !i as, since 2 is not = 0 it is true and the second NOT made it false. I was missing how C evaluates it. Thanks for the clarification

Now if I have another question about expressions, just add to this thread and it will get noticed?