1(vi)
Read the if program segment given below:
if(a > b) z = 25; else z = 35;
Which one of the following is the correct conversion of the if program segment to ternary?
- (a)z = a > b ? 35 : 25;
- (b)z = a > b ? 25 : 35;
- (c)z = a > b : 35 ? 25;
- (d)z = a > b : 25 ? 35;
Show SolutionHide Solution↓
z = a > b ? 25 : 35;
Reason — The ternary operator follows the format condition ? value_if_true : value_if_false. In the given if-else statement, when a > b is true, z is assigned 25, otherwise it is assigned 35. Hence, the correct equivalent ternary expression is z = a > b ? 25 : 35;.