Please demonstrate how the ternary operator works with a regular if/else block. Example:
Boolean isValueBig = value > 100 ? true : false;
Exact Duplicate: How do I use the ternary operator?
Please demonstrate how the ternary operator works with a regular if/else block. Example:
Boolean isValueBig = value > 100 ? true : false;
Exact Duplicate: How do I use the ternary operator?
The difference between the ternary operation and if/else is that the ternary expression is a statement that evaluates to a value, while if/else is not.
To use your example, changing from the use of a ternary expression to if/else you could use this statement:
In this case, though, your statement is equivalent to this:
As quoted from the ?: Operator MSDN page, "the conditional operator (?:) returns one of two values depending on the value of a Boolean expression."
So you can use the ternary operator to return more than just booleans:
Bad example, because you could easily write
as:
Beyond that, everyone else has already answered it. I would just not recommend using ternary operators to set bool values, since what you are evaluating is already a boolean value.
I realize it was just an example, but it was worth pointing out.
PHP Example
PHP Documentation on Comparison Operators