Friday 21 October 2016

Ternary Operators

Ternary Operators

  • It is used to evaluate Boolean expression,
    expr1 ? expr2 : expr3
  • If expr1 Condition is true? Then value expr2 : Otherwise value expr3.

--------------------------------------------------------------------------------------- 
Program        :       Program on conditional operators
Program name :       TerOpeDemo1.java
Output           : 
                             true
                             true

class TerOpeDemo1
{
        public static void main(String args[])
        {
             int x = 10;
             int y = 20;

             int result1 = (x > 10) ? x : y;
             int result2 = (y > 10) ? x : y;

             System.out.println("result 1 is: " + result1);
             System.out.println("result 2 is: " + result2);
        }
}

Compile     :            javac TerOpeDemo1.java
Run           :            java TerOpeDemo1
Output       : 
                             result 1 is: 20
                             result 2 is: 10
--------------------------------------------------------------------------------------- 
Thanks for your time.
Nireekshan