Conditionals with Omitted Operands (x ? : y)

Conditionals with Omitted Operands

The middle operand in a conditional expression may be omitted. Thenif the first operand is nonzero, its value is the value of the conditionalexpression.

Therefore, the expression

     x ?

: y

has the value of x if that is nonzero; otherwise, the value of y.

This example is perfectly equivalent to

     x ? x : y

In this simple case, the ability to omit the middle operand is not especially useful. When it becomes useful is when the first operand does,or may (if it is a macro argument), contain a side effect. Then repeating the operand in the middle would perform the side effect twice. Omitting the middle operand uses the value already computed without the undesirable effects of recomputing it.


x? : y is equal to x?

x:y

posted @ 2016-03-14 16:30  mengfanrong  阅读(236)  评论(0)    收藏  举报
众安尊享e升2025版 - 底部横幅广告
尊享e升 2025版
🚀 全新升级
百万医疗险全面升级 守护健康人生
✓ 一般医疗300万
✓ 重疾600万
✓ 6年保证续保
✓ 1万免赔额
600万
最高保障
首月特惠价
低至1元起
🛡️ 立即投保