177k views
3 votes
What is x after evaluating?

x=(2>3) ? 2 : 3;
A. 2
B. 3
C. 4
D. none of the above

1 Answer

6 votes

Answer:

3

Step-by-step explanation:

In programming, there is a lot of conditional operators.

like NOT (&&) operator, OR (||) operator.

similarly, there is an operator called the ternary operator. It takes three operands and it behaves like the if-else statement.

syntax:

(condition) ? expression 1 : expression 2;

Working:

If the condition is true, then it executes the expression 1 and if the condition is false, then it executes the expression 2.

let see the Question,

x=(2>3) ? 2 : 3;

the condition (2 > 3) is false, 2 always less than 3 not greater than 3.

Therefore, it executes the value 3 and assigns to x.

User Mhluzi Bhaka
by
7.4k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.