0
Thank you for your explanation brothers.
0
The ternary operator tests a condition. It compares two values. It produces a third value that depends on the result of the comparison. This can be accomplished with if-statements or other constructs.
The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.
The condition must evaluate to true or false. If condition is true, first_expression is evaluated and becomes the result. If condition is false, second_expression is evaluated and becomes the result. Only one of the two expressions is evaluated.
0
The ternary operator takes this general form:
a ? b : c
where 'a' is a bool expression and 'b' and 'c' are expessions of the same type.
If 'a' is true, the value is 'b'.
If 'a' is false, the value is 'c'.
In the first example:
int max = (n1 > n2) ? n1 : n2;
10 > 20 is false and so the value is n2 which is 20.
In the second example:
int min = (n1 < n2) ? n1 : n2;
10 < 20 is true and so the value is n1 which is 10.