可能重复:
Benefits of using the conditional ?: (ternary) operator
嗨,我正在查看这个freesource库,我看到这很奇怪 - 至少对我而言 - 语法
*currFrame = ( ( diff >= differenceThreshold ) || ( diff <= differenceThresholdNeg ) ) ? (byte) 255 : (byte) 0;
currFrame的类型为byte
diff,differenceThreshold和differenceThresholdNeg的类型为Int。
问号是做什么的? ,这个怪异的分配句子是什么意思?
提前致谢
答案 0 :(得分:8)
条件运算符(?:)返回两个值中的一个,具体取决于布尔表达式的值。以下是条件运算符的语法。
condition ? first_expression : second_expression;
C#参考:http://msdn.microsoft.com/en-us/library/ty67wk28.aspx
在您的情况下,如果( diff >= differenceThreshold ) || ( diff <= differenceThresholdNeg )
为true
,则currFrame的值将为255,否则将分配值0。
答案 1 :(得分:7)
这与
相同if(( diff >= differenceThreshold ) || ( diff <= differenceThresholdNeg ) )
currFrame = (byte) 255
else
currFrame = (byte) 0
答案 2 :(得分:4)
答案 3 :(得分:3)
条件运算符(?:)根据布尔表达式的值返回两个值中的一个。条件运算符的格式为
condition ? first_expression : second_expression;
答案 4 :(得分:1)
'?:'是一个条件运算符,你可以在这里阅读:http://msdn.microsoft.com/en-us/library/ty67wk28(v=vs.80).aspx
答案 5 :(得分:0)
这是三元运算符(请参阅MSDN)。它遵循以下语法:
result = condition ? result_if_condition_true : result_if_condition_false
答案 6 :(得分:0)
if ( diff >= differenceThreshold ) || ( diff <= differenceThresholdNeg ) )
*currFrame = (byte) 255;
else *currFrame = (byte) 0;