§ Conditional operator is an equivalent form of if-else structure.
§ It is called as ternary operator because it is used on three operands.
§ The general syntax of the ternary operator is shown below
The above expression evaluates statement1 or statement2 depending on the condition. If the condition is true statement1 gets executed; otherwise statement2 gets executed.
Example:
int x=30, y=40, big;
big = x > y ? x : y;
System.out.println(big); // prints 40
In the above example the variable named ‘big’ holds 40 as the condition ‘x>y’ is false, and value of ‘y’ is assigned to ‘big’.
0 comments:
Post a Comment