Conditional Ternary Operators
Understanding the use of conditional ternary operator with three operands
Learning Objectives
- Learning to use conditional ternary operator of format <condition>?<if_true>:<if_flase>
Source Code
|
Run Output
Code Understanding
int a=10; //Integer variable a is initialised with 10
boolean b=(a==10)?true:false;
Ternary operation applied with condition check for a==10 which is true so the true condition will be filled in boolean variable b.
System.out.println(b); //b is printed as computed in previous statement
b=(a>10)?true:false;
Ternary operation applied with condition check for a>10 which is false so the false condition will be filled in boolean variable b.
System.out.println(b); //b is printed as computed in previous statement
Notes
- Ternary operators implement if-then-else type of logic within the same line. This often help in printing or quick calculation of values based on simple condition.
Suggested Filename(s): ConditionalOP.java
sunmitra| Created: 3-Mar-2018 | Updated: 3-Mar-2018|