Illustrate '?' operator.
Advertisements
प्रश्न
Illustrate '?' operator.
कोड लेखन
लघु उत्तर
Advertisements
उत्तर
The Conditional Operator (?:) in Java is a ternary operator that evaluates a condition and selects one of two expressions depending on whether the condition is true or false. It is a shorter alternative to an if-else statement.
variable = (condition) ? expression1 : expression2;
Example:
class ConditionalOperator
{
public static void main(String[] args)
{
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Greater Number: " + max);
}
}
Output:
Greater Number: 20
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
