-5
int a = 2, b = 3, c = 5, d = 4, e = 1;

if ((a = b > c ? d : e) == e) a++;

the answer is a=2

i do not understand what this is actually doing.

Can you help me figure out what this block of code does step by step?

LihO
  • 41,190
  • 11
  • 99
  • 167
user1082764
  • 1,973
  • 9
  • 26
  • 40
  • Learn the rules of precedence, my child. Study them. Become one with them. Only then will you achieve true enlightenment. – sizzzzlerz May 09 '13 at 17:30

6 Answers6

6
if ((a = b > c ? d : e) == e) a++;

equals to:

if (b > c)
    a = d;
else
    a = e;
if (a == e)
    a++;

Also have a look at: What does '?' do in C++?

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
2

to understand you have to scompose the if condition.

if ((a = b > c ? d : e) == e) a++;

There is an assignment first

a = b > c ? d : e

It says: if b > c then a = d, else a = e

in this case 3 > 5 is false so

a = e so a = 1

then there is another evaluation

if(a==e) a++;

in this case a = e = 1 so it does a++ -> a = 2

I hope it helps ;)

Alessandro
  • 137
  • 3
  • 14
  • Just a minor nitpick (since your logic is actually correct): the assignment is *second* - the ternary is evaluated first. – Nik Bougalis May 09 '13 at 17:50
1
int a = 2, b = 3, c = 5, d = 4, e = 1;

if ( (a = ( (b > c) ? d : e )) == e ) a++;
  1. b > c returns false
  2. false ? d : e returns e which is 1
  3. a = 1 assigns 1 to a and returns a
  4. 1 == e is true
  5. if (true) a++; increments a by 1

so a is equal to 2 at the end.

LihO
  • 41,190
  • 11
  • 99
  • 167
stardust
  • 5,918
  • 1
  • 18
  • 20
0

I think this is main spot you want to know

a ? b : c is called Conditional Operator, it means if a is true then use b, otherwise use c.

(a = b> c? d : e) 

just equals to

if(b>c)
  a = d;
else 
  a = e;
diwatu
  • 5,641
  • 5
  • 38
  • 61
0

First, so-called ternary operator (with ? mark) checks for the condition (b>c):

a= b > c ? d : e

and assigns value e to variable a (because the condition failed it took the second option), so, a=1

Then it checks if variable a is equal e (it's true) and correspondingly increases variable a by 1, so it became 2

Note: it looks like the most critical part for you is to learn ternary operator and operation precedence in C.

Alexander Bell
  • 7,842
  • 3
  • 26
  • 42
0

At first, if we look at the variables after this code executes it looks like not much is happening. But is that true?

Well, LiHo has already shown a decomposition of this and how it "translates" to more easy-to-read code and using his code, it should be simple to see what's happening. But just for fun, let's decompose that thing and run through this given the numbers we have been given, shall we?

if ((a = b > c ? d : e) == e) a++;

This is of the form:

if(<something> == e) a++;

We know what that means. So, let's focus on the <something> part:

(a = b > c ? d : e)

Looking at the C++ operator precedence list we see that the ternary operator (?:) ranks higher than the = so it will evaluated first:

b > c ? d : e

This is a glorified if statement: "if b greater than c then return d, otherwise return e. We have the values for both b and c and we also know that 3 is not greater than 5. So, we know that this statement will return e.

So now, we've simplified our expression a bit:

(a = e)

This is an assignment. It sets a to be equal to e. Now, we will step back out to get some more context:

if((a = e) == e) a++;

What happens here is that after the operation (a = e) is executed, it gives us back a result. That result is, conveniently, the value of a after the assignment. Therefore, we have this:

if(a == e) a++;

Since we've set a to be equal to e (i.e. equal to 1) the if will execute and a++ will increment a from 1 to 2.

So while it seems like nothing happened, in reality, we've done a lot of work.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37