c ++中的多个三元运算符 [英] Multiple Ternary operator in c++
本文介绍了c ++中的多个三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在以下场景(C ++)中有疑问:
I have a doubt in the following scenario (C++):
说,我有一个if条件
if ( a ? b ? c : d : false)
{
// do something
}
else
{
// do something else
}
这是我对它的工作原理:
This is my interpretation of how it works:
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)
我的理解是否正确?
使用这个更好或多个如果
/ else
检查?
Is my understanding correct?
Is using this better or multiple if
/else
checks?
推荐答案
请使用括号括起来,因为它有助于提高可读性。此外,使用多个三元运算符是很好的。
Please use this in parenthesis, as it helps improve readability. Also, it is fine using multiple ternary operators.
if ( a ? (b ? c : d) : false)
{
// do something
}
else
{
// do something else
}
这篇关于c ++中的多个三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文