14.12 Conditional operator
来源:互联网 发布:讲人工智能的电影 编辑:程序博客网 时间:2024/05/17 06:49
The ?: operator is called the conditional operator. It is at times also
called the ternary operator.
conditional-expression:
conditional-or-expression
conditional-or-expression ? expression : expression
A conditional expression of the form b ? x : y first evaluates the
condition b. Then, if b is true, x is
evaluated and becomes the result of the operation. Otherwise, y is
evaluated and becomes the result of the
operation. A conditional expression never evaluates both x and y.
The conditional operator is right-associative, meaning that operations are
grouped from right to left.
[Example: For example, an expression of the form a ? b : c ? d : e is
evaluated as
a ? b : (c ? d : e). end example]
The first operand of the ?: operator must be an expression of a type that
can be implicitly converted to
bool, or an expression of a type that implements operator true. If neither
of these requirements is
satisfied, a compile-time error occurs.
The second and third operands of the ?: operator control the type of the
conditional expression. Let X and Y
be the types of the second and third operands. Then,
?If X and Y are the same type, then this is the type of the conditional
expression.
?Otherwise, if an implicit conversion (?3.1) exists from X to Y, but not
from Y to X, then Y is the type of
the conditional expression.
?Otherwise, if an implicit conversion (?3.1) exists from Y to X, but not
from X to Y, then X is the type of
the conditional expression.
?Otherwise, no expression type can be determined, and a compile-time error
occurs.
The run-time processing of a conditional expression of the form b ? x : y
consists of the following steps:
?First, b is evaluated, and the bool value of b is determined:
If an implicit conversion from the type of b to bool exists, then this
implicit conversion is performed to
produce a bool value.
Otherwise, the operator true defined by the type of b is invoked to produce
a bool value.
?If the bool value produced by the step above is true, then x is evaluated
and converted to the type of
the conditional expression, and this becomes the result of the conditional
expression.
?Otherwise, y is evaluated and converted to the type of the conditional
expression, and this becomes the
result of the conditional expression.
called the ternary operator.
conditional-expression:
conditional-or-expression
conditional-or-expression ? expression : expression
A conditional expression of the form b ? x : y first evaluates the
condition b. Then, if b is true, x is
evaluated and becomes the result of the operation. Otherwise, y is
evaluated and becomes the result of the
operation. A conditional expression never evaluates both x and y.
The conditional operator is right-associative, meaning that operations are
grouped from right to left.
[Example: For example, an expression of the form a ? b : c ? d : e is
evaluated as
a ? b : (c ? d : e). end example]
The first operand of the ?: operator must be an expression of a type that
can be implicitly converted to
bool, or an expression of a type that implements operator true. If neither
of these requirements is
satisfied, a compile-time error occurs.
The second and third operands of the ?: operator control the type of the
conditional expression. Let X and Y
be the types of the second and third operands. Then,
?If X and Y are the same type, then this is the type of the conditional
expression.
?Otherwise, if an implicit conversion (?3.1) exists from X to Y, but not
from Y to X, then Y is the type of
the conditional expression.
?Otherwise, if an implicit conversion (?3.1) exists from Y to X, but not
from X to Y, then X is the type of
the conditional expression.
?Otherwise, no expression type can be determined, and a compile-time error
occurs.
The run-time processing of a conditional expression of the form b ? x : y
consists of the following steps:
?First, b is evaluated, and the bool value of b is determined:
If an implicit conversion from the type of b to bool exists, then this
implicit conversion is performed to
produce a bool value.
Otherwise, the operator true defined by the type of b is invoked to produce
a bool value.
?If the bool value produced by the step above is true, then x is evaluated
and converted to the type of
the conditional expression, and this becomes the result of the conditional
expression.
?Otherwise, y is evaluated and converted to the type of the conditional
expression, and this becomes the
result of the conditional expression.
- 14.12 Conditional operator
- conditional operator in Xcode
- C++条件运算符Conditional operator(?)
- C# 6.0 (C# vNext) 新功能之:Null-Conditional Operator
- AxureRP7.0教程 AxureRP7.0条件操作符详解 条件操作符 Conditional Operator
- operator->*
- operator[]
- operator
- operator
- operator.*()
- operator[]
- Operator
- operator
- operator
- operator ->
- operator
- operator ++
- .operator
- 合金装备No.2 Guitarrrrr!!!!
- windows2000下下载出错的问题
- linux操作系统下c语言编程入门 -- (7)线程操作
- “谷歌”在中国:他不是Google(翻译自纽约时报)
- 14.11 Conditional logical operators
- 14.12 Conditional operator
- 14.13 Assignment operators
- JAVA学习笔记(一)-基本编程技巧
- 14.13.1 Simple assignment
- 14.13.2 Compound assignment
- 14.13.3 Event assignment
- JAVA学习笔记(二)-面向对象程序设计
- 14.14 Expression
- 14.15 Constant expressions