【发布时间】:2013-10-01 22:34:37
【问题描述】:
我对以下场景(C++)有疑问:
说,我有一个 if 条件
if ( a ? b ? c : d : false)
{
// do something
}
else
{
// do something else
}
这是我对其工作原理的解释:
If a is true, it checks b. Then,
- If b is true, the if loop is reduced to if (c)
- If b is false, the if loop is reduced to if (d)
If a is false, the if loop is reduced to if (false)
我的理解正确吗?
使用这个更好还是多个if/else 检查?
【问题讨论】:
-
是的,你的解释是正确的。
-
您可以删除一个三元运算符:
if (a && (b ? c : d)) -
有关运算符优先级的说明,请参阅en.cppreference.com/w/cpp/language/operator_precedence。