What is the value of the following 'C' language expression in which x is an integer variable whose value is 4? (x - 5) ? 15: 25
What is the value of the following 'C' language expression in which x is an integer variable whose value is 4? (x - 5) ? 15: 25 Correct Answer 15
Key PointsThe conditional operator is kind of similar to the if-else statement as it does follow the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.
Syntax:
The conditional operator is of the form,
variable = Expression1? Expression2: Expression3;
It can be visualized into the if-else statement as:
if(Expression1)
{
variable = Expression2;
}
else
{
variable = Expression3;
}
Given that,
(x - 5) ? 15 : 25;
here x=4, (4-5)= -1 and take it as true and print 15 as output.
If x=6, (6-5)= 1 and take it as true and print 15 as output.
If x=5, (5-5)= 0 and take it as false and print 25 as output.
Note:
Negative values, and any non-zero values in general, are treated as true when used as conditions. For C, there are a number of contexts in which an expression is treated as a condition.
Hence the correct answer is 15.