Conditonal Operator | Sololearn: Learn to code for FREE!


Conditonal Operator

1/13/2018 3:51:01 PM


5 Answers

New Answer


This behavior is caused by operator precedence rules in C++. conditional operator has higher precedence than , (comma) operator. so the whole conditional statement is interpreted as two statements and not one. Since the condition is true, "hi" is printed twice. The last print "bye" is separated out and will always be executed irrespective of the condition. check my code to understand how the statement is evaluated


It's because of operator precedence . check your updated code ..



I put the two outputs (with by) after the colon into parentheses ( ) and it worked


conditional operator are also known as ternary operator. It acts upon 3 opprents and is represented by ? = Question mark : = Colon ; = Semi-colon In conditional operator if condition evaluates true then it will execute the expression after question mark otherwise, it will execute after colon.