English

Illustrate '?' operator.

Advertisements

Question

Illustrate '?' operator.

Code Writing
Short Answer
Advertisements

Solution

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
  Is there an error in this question or solution?
Chapter 6: Variables and Expressions - EXERCISE [Page 132]

APPEARS IN

Dheeraj Mehrotra Computer Science [English] Class 12
Chapter 6 Variables and Expressions
EXERCISE | Q 37. | Page 132
Advertisements
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×