Can anyone explain what is this ? | SoloLearn: Learn to code for FREE!

+6

Can anyone explain what is this ?

Please tell about how the operators work. Cout<< ( ( 1 > 2 ) ? 3 : 4 ) ; ans: 4 int i=0 ; Cout <<( i=0 ? 1 : 2 ? 3 : 4 ) ; ans:3

10/20/2019 1:34:36 PM

Kashyap (✷‿✷)

10 Answers

New Answer

+12

The ternary operator works like a shorthand of `if` statement <expression> ? <true-block> : <false-block> Equal to: if(<expression>) <true-block> else <false-block> ● Now for first case: ((1 > 2) ? 3 : 4) • 1 is less than 2, so expression `1 > 2` is evaluated as false. ((false) ? 3 : 4) • 4 is returned here, because expression evaluates to false. ● For the second case: int i = 0; - Remember in C/C++ any non zero integral value is considered true. (i = 0 ? 1 : 2 ? 3 : 4) • Here zero is assigned to variable <i> then <i> is evaluated, which yields false because <i> is zero. ((false ? 1 : 2) ? 3 : 4) • `(false ? 1 : 2)` returns 2. Again non zero value is considered true, and zero is false. ((2) ? 3 : 4) • Since 2 is considered true (as it is non zero), here ternary operator returns 3. Hth, cmiiw

+7

You're very welcome buddy 👍

+4

Thanks Ipang , your answer is like a tutorial,it cleared all my doubts.

+3

Ipang 😵

+2

Ipang I have a doubt. This thread is making me think too much: https://www.sololearn.com/Discuss/2221739/?ref=app In the example: ( i = 0 ? 1 : 2 ? 3 :4) Have assignment operator higher precedence than ( ? : ternary )? Shouldn't it be ( i = (0?1:(2?3:4)))? I am confused. [Edit] Solved

+2

Kevin Star In general assignment operation precedence (priority) is low, because the RHS operand needs to be fully evaluated before the result can be assigned to the LHS operand. But in the case of ternary operation `i = 0 ? 1 : 2 ? 3 : 4`, the assignment for variable <i> is evaluated first, because it is needed to proceed with decision making. When an assignment operation be used as a condition (as boolean state), it is generally considered a truthy statement. However, use of parentheses may or may not alter the final evaluation result of the entire expression (the nested ternary operation). I still find myself puzzled at times when faced with single or nested ternary operation. Check this snippet and see how it can be quite tricky. #include <stdio.h> int main() { int i = 0; printf("1. %d\n", (i = 0 ? 1 : 2) ? 3 : 4); printf("2. %d\n", (i = 1 ? 1 : 2) ? 3 : 4); printf("3. %d\n", (i = 0 ? 1 : 2 ? 3 : 4)); printf("4. %d\n", (i = 1 ? 1 : 2 ? 3 : 4)); return 0; } (Edited)

+2

Ipang cool examples. In my first answer I said that assignment is evaluated first but then I ran this. https://code.sololearn.com/cVpHwJqf0f59/?ref=app And now this: https://code.sololearn.com/cA9i6w2dShPJ/?ref=app

+1

Kevin Star That's exactly what I mean by tricky 🤔 Still parentheses position plays a role I guess ...

+1

Kevin Star Though I'm not sure, *maybe* this types of problem works differently among languages. I recall there was this question about nested ternary in PHP, where I learned that PHP requires nested ternary to be written with parentheses. PHP refuse to proceed when given nested ternary without parentheses (forgot the warning message). https://www.sololearn.com/Discuss/2219956/?ref=app

+1

Kevin Star 😁 you tell me about it 😆