+ 1

explain why ternary return this

can someone explain why there are differences in output between if/else and ternar operator..thanks :). https://code.sololearn.com/cbhsBjdu3ro2/?ref=app

2nd Jul 2021, 1:35 PM
durian
durian - avatar
4 Respostas
+ 2
Lily Mea Not necessary to have b and c same type in ternery operator but if you have different type of b and c and you are assigning to an another variable (in case of String and int or double) then it should be same type otherwise you will get compiler error but in case of double and int you will not get any error because you can assign int value to double. Check last statement. https://code.sololearn.com/cBX3Wk239VmP/?ref=app
2nd Jul 2021, 2:15 PM
A͢J
A͢J - avatar
+ 1
Lily Mea 'A' is a character because it has single quote so if you print 'A' then ASCII value will be print that is why you got input like that 65.0 because a is a type of double.
2nd Jul 2021, 3:13 PM
A͢J
A͢J - avatar
0
Jazz so do u mean if a?b:c, then b and c must have same type..so thats why it cast int to double?
2nd Jul 2021, 2:03 PM
durian
durian - avatar
0
I ᴀᴍ "Tɪᴍᴇ" i see..also, i tried 'A':a and the result = 65.0 .. in this case,'A' was casted to double i guess..so if i do "A":a,then result of a(false) will be String?
2nd Jul 2021, 2:33 PM
durian
durian - avatar