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

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-16-2012
shellwoo3
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?```
• 07-16-2012
laserlight
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.
• 07-16-2012
shellwoo3
Is the left operand (++i || ++j)? If so, doesn't the value of j change because it is incremented before it is evaluated?
• 07-16-2012
laserlight
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.
• 07-16-2012
shellwoo3
It evaluates the left operand first, then the right operand. RIGHT?
• 07-16-2012
laserlight
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.
• 07-16-2012
shellwoo3
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?
• 07-16-2012
laserlight
Yes.
• 07-16-2012
shellwoo3
Okay, thank you very much. This expression was killing me, but I get it!!!!
• 07-16-2012
phantomotap
O_o

Perhaps?

Code:

```if(true && evaluated); if(false && not_evaluated); if(true || not_evaluated); if(false || evaluated);```
Soma
• 07-16-2012
shellwoo3
Thanks Soma... this too makes sense and it's an easy reference. :o
• 07-17-2012
whiteflags
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.
• 07-17-2012
shellwoo3
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?
• 07-17-2012
whiteflags
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"
• 07-17-2012
shellwoo3
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?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last