Conditional Ternary Operators – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #JAVA#3702 siteicon   siteicon   siteicon  

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

TC++ #3702

Source Code

Run Output

true
false

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



Share

sunmitra| Created: 3-Mar-2018 | Updated: 3-Mar-2018|






Back