Question

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. :)

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

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

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.

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

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.

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.

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.

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.

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.

y += (x-- ? z++ : --z); so this is your question and the answer is simple................

As we know that something like X-- Or x++ are called post increment or decrement. So according to the rules of post increment or decrement the expression will be evaluated first and then only increment or decrement will come into action. i.e first evaluate and then increase or decrease.....

NOW lets solve your question:

Y+=X--?Z++:--Z....now it is containing three parts i.e left,middle and right...now the point of consideration is:"if left part is true then it will return middle part, otherwise right side part...and execution always starts from left part as it is the condition part"

Now simplify the statement as:Y+=X?Z:Z;....Now see whether left part is having pre or post increment or decrement.....if post ++/-- is der den first evaluate the simplified statement......den go for ++/--.....

Now left part is having post decrement...so lets first evaluate the expression...i.e

y+=1:3:3 //any non zero value in the condition part is a true condition(i.e 1)

so now our condition is true and it will return the middle part and when the control goes to middle part at that time only x value will be decremented i.e it becomes 0....

Now 2nd simplified statement is Y+=Z. (\\as condition is true and we got middle part,compiler will skip rest of the part i.e right part.)

Now observe whether Z is post ++/-- (or)pre ++/--) ...hahh..its post increment ..so simply first evaluate the simplified statement2 and then increase the value of Z....i.e

                     Y+=Z =>Y=Y+Z
                    =>Y=2+3 =>y=5

Now the expression is evaluated i.e Y=5,so now increment the value of Z i.e it become 4

As we know operators checks condition if it is true i.e 1 it executes true statement.

If it is false i.e 0 it executes false statement

Being we are initialising value of x to 1 it executes true statement

As a result it exposes result 5 from true part y=(y+z++)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top