Answer:
A ternary operator, also called a conditional operator, is a special kind of operator used in some programming languages as a shorter way of writing an if..else block. It is denoted by a question mark and then a colon (? : ). It takes 3 operands:
i. A boolean expression which would evaluate to a true or a false.
ii. A first value that will be assigned if the boolean expression gives a true.
iii. A second value that will be assigned if the boolean expression gives a false.
The general format is:
variable = boolean expression ? first_value : second_value;
For example:
A ternary expression can be written as;
String f = (3 > 5) ? "yes" : "no";
In the above expression;
The boolean expression is 3 > 5
The value to be assigned, to f, if the boolean expression is true is "yes"
The value to be assigned, to f, if the boolean expression is false is "no"
So in this case, since (3 > 5) evaluates to false, "no" will be assigned to f.
Therefore, the above expression will give the same result as;
String f = "no"
Another example:
=================
int a = 5:
int b = 3;
int max = (a > b)? a : b;
==================
In the above code snippet,
Since a > b i.e 5 > 3, the first value a (which is equal to 5) will be assigned to max.
Therefore, int max = 5.
The value of max is 5
PS:
These examples and explanations are given using Java. Similar scenario, maybe different syntax, will be obtainable in other programming languages.