一个简单的问题
printf("%d", 99 || 44) prints "1" in C
print 99 || 44 prints "99" in perl
有两种不同的评价。每一个都有名字吗?
编辑:我很想知道与 C 相比,通常如何调用 Perl 求值。当您说“C 示例是 X,而 perl 示例不是 X,而是 Y”时,您会使用哪些词来表示 X 和Y.“短路”不是我要找的。p>
最佳答案
阅读here .
Binary || performs a short-circuit logical OR operation. That is, if the left operand is true, the right operand is not even evaluated. Scalar or list context propagates down to the right operand if it is evaluated.
In Perl the || and && operators differ from C's in that, rather than returning 0 or 1, they return the last value evaluated.
printf("%d", 99 || 44) prints "1" in C
那是因为 99||44
返回 true(由于 ||
的短路 Action ,只有 99(非零)被评估)其等效于 1
因此 printf()
打印 1
。
print 99 || 44 prints "99" in perl
..不是返回 0 或 1,而是返回最后计算的值(此处为 99
)。
关于c++ - "value-based"OR 运算符的通用术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2172448/