- 论坛徽章:
- 8
|
- 5.12 Conditional Expressions
- A conditional expression is a special kind of expression that has three operands. It allows you to use one expression's value to select one of two other expressions. The conditional expression is the same as in the C language, as shown here:
- selector ? if-true-exp : if-false-exp
- There are three subexpressions. The first, selector, is always computed first. If it is “true” (not zero or not null), then if-true-exp is computed next and its value becomes the value of the whole expression. Otherwise, if-false-exp is computed next and its value becomes the value of the whole expression. For example, the following expression produces the absolute value of x:
- x >= 0 ? x : -x
复制代码 |
|