Expressions

Q.

Are the following two statement same?

1.a <= 20 ? (b = 30): (c = 30);

2.(a <=20) ? b : (c = 30);

A. Yes
B. No

Answer : Option B

Explanation :

No, the expressions 1 and 2 are not same. 1. a <= 20 ? (b = 30) : (c = 30); This statement can be rewritten as,

if(a <= 20)
{
    b = 30;
}
else
{
    c = 30;
}

2. (a <=20) ? b : (c = 30); This statement can be rewritten as,

if(a <= 20)
{
    //Nothing here
}
else
{
    c = 30;
}