带三元运算符的 return 是什么意思?

What does return with a ternary operator mean?

我目前在阅读这段代码时遇到问题。 有谁知道它的作用吗?

if (c == a)
    return q[a] ? 1 : 0;

如果 q[a] 是 non-zero 它 returns 1 否则 0.

如果 c==aq[a] = true return 1 否则 return 0.

if 中的代码按照匹配运算符优先级的顺序求值。

所以首先计算 a + 1,然后计算 b == (a + 1)

如果b等于(a + 1)则如果q[a]true则返回1,否则0.

C# operators and expressions or Precedence and order of evaluation (根据语言,您需要 google“语言名称”+ 运算符优先级)。

声明

return q[a] ? 1 : 0;

相当于

// Exact type of "zero" depends on the type of q[a]
if (q[a] != 0) 
    return 1;
else
    return 0;

或者

return !!q[a];