TERNARY OPERATOR
The conditional operator is a ternary operator (it has 3 operands) & used to evaluate Boolean expressions, much like an if statement.
Based on its condition - it assigns a value to a variable.
Syntax:
X= (Boolean Expression) ? value1 : value2
It assigns value1 if the Expressions evaluates to true, otherwise assigns value2.
Example:
Output:
Your are: Not eligible for Pension
The conditional operator is a ternary operator (it has 3 operands) & used to evaluate Boolean expressions, much like an if statement.
Based on its condition - it assigns a value to a variable.
Syntax:
X= (Boolean Expression) ? value1 : value2
It assigns value1 if the Expressions evaluates to true, otherwise assigns value2.
Example:
public class Salary{
public static void main(String[] args){
int age=45;
String pension=(age>=50)?"Eligible for pension":"Not eligible for Pension";
System.out.println("Your are: "+pension);
}
}
Output:
Your are: Not eligible for Pension
No comments:
Post a Comment