Conditional or Ternary Operator

 

Conditional or Ternary Operator (?:)

Ternary operator is used to perform any operation on three operands
Syntax :

Expression1 ? Expression2 : Expression3;

Here
Expression1 is a condition
Expression2 is a operation
Expression3 is a operation

In the above syntax
if condition is true ,The statements of Expression2 is execute.
if condition is false ,The statements of Expression3 is execute

Odd or Even Number Using Ternary Operator

class Main
{
public static void main(String args[])
{
int x=10;
String s = x%2==0 ? “EvenNumber” : “OddNumber”;
System.out.println(s);
}
}

Output:EvenNumber

https://sateeshm.com/java-operators.html#Ternary

https://sateeshm.com/java-operators.html#Ternary

Comments