KnowledgeBoat Logo
|

Computer Applications

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?

  1. z = a > b ? 35 : 25;
  2. z = a > b ? 25 : 35;
  3. z = a > b : 35 ? 25;
  4. z = a > b : 25 ? 35;

Java Conditional Stmts

4 Likes

Answer

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;.

Answered By

2 Likes


Related Questions