About Ternary Operator In Java :
Ternary operator can be used to replace certain types of if-then-else statements. It is called ternary operator because it uses three operands.
The syntax for ternary operator is:
expression1 ? expression2 : expression3
expression1 can be any expression that evaluates to boolean value, if expression1 is true the expression2 is evaluated else expression3 is evaluated.
int marks = 80; marks are greater than 60, a bonus of 5 marks are given else 2 marks will be given.
Ternary Example CODE class TernaryExample OUTPUT Marks = 50 DESCRIPTION This example uses ternary operator twice, once to decide how many bonus marks to give and once to assign grade to the student. Since THINGS TO TRY
2-min video about ternary operator
|