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?
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?
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++?
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 ;)
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;
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.
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.