简体   繁体   English

关于逻辑 AND 运算符

[英]Regarding logical AND operator

int a=8, b=10,c=2,d;
d= ++a && ++b || ++b; 

In the above code, how do I know if (++a) , (++b) are true or false.在上面的代码中,我怎么知道(++a) , (++b)是真还是假。 I know true is 1 and false is 0. But I can't understand how to determine if these expressions are true or false.我知道真为 1,假为 0。但我不明白如何确定这些表达式是真还是假。 Please help.请帮忙。

0 is false, any non-zero value is true. 0为假,任何非零值都为真。 So you just need to determine whether ++a and ++b are zero or not zero.所以你只需要确定++a++b是否为零。

Since a is initially 8 , ++a is 9 , which is non-zero, so it's true.由于a最初是8++a9 ,它是非零的,所以它是真的。

Since b is initially 10 , ++b is 11 , which is non-zero, so it's true.由于b最初是10++b11 ,它是非零的,所以它是真的。

9 && 11 is true because both the operands are true. 9 && 11为真,因为两个操作数都为真。

|| only evaluates the second operand if the first operand is false.仅当第一个操作数为假时才计算第二个操作数。 So the second ++b ie never executed.所以第二个++b即从未执行过。 The value of true || anything true || anything的值true || anything is true . true || anything都是true

Therefore, d will be set to true, which is 1 .因此, d将设置为 true,即1

In C, logical operators ( && , || , ! , etc.) assume that zero is false and all other values are true .在 C 中,逻辑运算符( &&||!等)假定零为false ,所有其他值为true

Based on the operator precedence (operator && precedence is higher than || operator), the expression will be evaluated as:基于运算符优先级(运算符&&优先级高于||运算符),表达式将被计算为:

d = (++a && ++b) || ++b; 

Note that logical AND operation expr1 && expr2 employs short-circuiting behaviour.请注意,逻辑AND运算expr1 && expr2采用短路行为。 With logical short-circuiting, the second operand, expr2 , is evaluated only when the result is not fully determined by the first operand, expr1 .使用逻辑短路,仅当结果未完全由第一个操作数expr1确定时,才计算第二个操作数expr2 That is, expr2 is not evaluated if expr1 is logical 0 (false).也就是说,如果expr1为逻辑0 (假),则不计算expr2

++a will result in 9 , a non zero value, hence, results in true , so right hand side operand of && operator, which is ++b , will be evaluated. ++a将导致9 ,一个非零值,因此,导致true ,因此&&运算符的右侧操作数,即++b ,将被评估。
++b will result in 11 , a non zero value, hence, results in true . ++b将导致11 ,一个非零值,因此,导致true

true && true will result in true . true && true将导致true

Logical OR operation expr1 || expr2逻辑OR运算expr1 || expr2 expr1 || expr2 employs short-circuiting behaviour. expr1 || expr2采用短路行为。 That is, expr2 is not evaluated if expr1 is logical 1 (true).也就是说,如果expr1为逻辑1 (真),则不计算expr2

So, in the given expression, the left hand side of ||因此,在给定的表达式中, ||的左侧is evaluated as true hence the right hand side operand of ||被评估为true ,因此||的右侧操作数operator, which is ++b , will not be evaluated and the result of whole expression will be true .运算符,即++b ,不会被评估,整个表达式的结果将为true Hence, the value of d will be 1 .因此, d的值为1

In C, C++ and many other programming languages, for integers, 0 is considered false and any other integer (including negative numbers) is true.在 C、C++ 和许多其他编程语言中,对于整数,0 被认为是假的,而任何其他 integer(包括负数)都是真。 So here d will be evaluated to true所以这里 d 将被评估为 true

In 'C' we know that the true is '1' and false is '0' so above expression will be true because both the expressions are non zero.在“C”中,我们知道真为“1”,假为“0”,因此上述表达式将为真,因为两个表达式都不为零。 and if you want to print actual values like true and false i think you should try out printf("%s", x?"true":"false");如果你想打印像 true 和 false 这样的实际值,我认为你应该试试 printf("%s", x?"true":"false");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM