views:

146

answers:

11

I just have a quick question about the conditional operator. Still a budding programmer here. I am given x = 1, y = 2, and z = 3.

I want to know, why after this statement:

y += x-- ? z++ : --z;

That y is 5. The values after the statement are x = 0, y = 5, and z = 4. I know the way the conditional operator works is that it is formatted like this: variable = condition ? value if true : value if false.

For the condition, y += x-- , how does y become 5? I can only see 2 (2 += 0) and 3 (2 += 1)(then x-- becomes zero) as possibilities. Any help is much appreciated. :)

+10  A: 

When it evaluates the condition (x != 0) x is still 1 (that is not 0). So it picks z++. Which is still 3. 2 + 3 = 5. At the end of the day x has become 0 and z has become 4.

Take a look here for details. It's important to remember a simple thing: when you say x ++ the current value of x is used and then it is incremented. When you say ++x it is first incremented and then used.

nc3b
A: 

Hi,

The reason for this is that post-decrement/increment operator (x++ or x--) does the following:

  1. increment or decrement the variable
  2. return the original value.

So the return value of x-- is 1, indicating true, so the statement z++ is evaluated, returning the original value 3.

Since y = 2, y += 3 is 5.

John Ledbetter
+6  A: 

Just break it down into a similar if statement:

if (x--)
    y += z++;
else
    y += --z;

In your case, since x is 1, you'll take the "true" side of this if statement. That means you're adding z++ to y, giving 3 + 2, resulting in 5.

Please don't write code like this.

Carl Norum
like what? like yours or like his?
Dervin Thunk
@Dervin, like his. But this slightly expanded version isn't very good either.
Carl Norum
Worth an upvote just for the last line :)
tloach
Ya - +1 for the plea
n8wrl
yes, upvoted. using ifs is an interesting pedagogical advice.
Dervin Thunk
You do have to be careful with the if statement version, since it's not 100% semantically identical. In most cases it helps to understand what's going on.
Carl Norum
A: 

x-- would mean the expression is evaluated at current value of x and after that x is reduced by 1. Same is the case with Z++. This is the other way around for --z, which means this is evaluated at the new value of z.

So at the time of evaluation x is 1, z is 3. and after the evaluation of expression x becomes 0 and z 4; and y = 2 + 3 = 5

Shravan
+1 to not writing code like this. This is something you should be aware of none the less
Shravan
A: 

Remember that the increment and decrement operators return different things depending on whether they're placed before or after the name of a variable.

In particular, when x-- is evaluated, it decreases x by 1, but returns the unmodified value of x, which in this case is 1. In C, 1 evaluates to true, so the ternary operator will return z++.

And again, because the ++ operator is placed after the variable, the return value of z++ is the unmodified value of z, which is 3.

Thus, this comes down to y += 3, which results in y being 5.

Syntactic
+1  A: 

As a budding programmer just know that you should NEVER write anything like this so that way you can forget about it worrying you!

Trevor Tippins
Unless you only work on solo projects you may still encounter such code from other developers.
R Samuel Klatchko
I know. I was being facetious. Others had already answered the OP's query quite succinctly.
Trevor Tippins
A: 

x-- and z++ decrement and increment after they are used.You end up with the following when the ternary operator is evaluated:

y += (1) ? (3) : (--z);

--z never gets called, the conditional evaluates to true and executes the first option in the ternary operator. After use, x is then decremented, and z is incremented.

agent oranje
+7  A: 

The Operator ?: has higher precedence than the operator +=. So Your expression is evaluated as

y += (x-- ? z++ : --z);

the value of x-- ? z++ : --z expression is the value of the expression z++ (that is 3) because the value of the expression x-- is 1

Maciej Hehl
+1 for actually answering the confusion. precedence with ?: should NEVER be taken for granted.
Brian Postow
A: 

It is normal because it first "runs" ternary operator then does decrement as your decrement operator (x--) is postfix, so you got z++ which is 3 so you have 5 in y.

Incognito
A: 

The expression x-- evaluates to the current value of x, which is 1. Thus the result of the conditional expression is z++, which evaluates to 3. 3 gets added to y, for a total of 5.

John Bode
A: 

I think your fundamental issue here is that you're assuming 'y+= x--' is your condition, when in fact your condition is merely 'x--'. There is a return from the conditional operator which makes y += the result of the conditional operation: "x-- ? z++ : --z;" is 5. Other comments have the reason why it actually evaluates to 5.

Ryan Zauber
I think the problem is that he's assuming `x-- == 0`, judging from his `2 += 0` equivalence.
Chuck