如果条件为单和(&) [英] If condition with single and (&)
问题描述
我们都知道&&(double和)for和condition.单个以及内部发生什么情况如何执行条件.
We all know && (double and) for and condition. for single and What happen internally how condition gets executed.
if(true & bSuccess)
{
}
推荐答案
true & bSuccess
在此表达式中,两个操作数都提升为 int
,然后对&
求值.如果bSuccess为true,则您将获得 1&1
,即 1
(或 true
).如果bSuccess为false,您将得到 1&0
,即 0
(或 false
)
in this expression both operands are promoted to int
and then &
is evaluated. If bSuccess is true you will get 1 & 1
which is 1
(or true
). If bSuccess is false you'll get 1 & 0
which is 0
(or false
)
因此,在布尔值的情况下,&&
和&
始终会产生相同的结果,但在&中并不完全等效;
将始终同时评估其参数,并且如果第一个参数为false,则&
不会评估其第二个参数.
So, in case of boolean values &&
and &
will always yield the same result, but they are not totally equivalent in that &
will always evaluate both its arguments and &&
will not evaluate its second argument if the first one is false.
示例:
bool f() { std::cout << "f"; return false; }
bool g() { std::cout << "g"; return true; }
int main()
{
f() && g(); //prints f. Yields false
f() & g(); //prints fg or gf (unspecified). Yields 0 (false)
}
这篇关于如果条件为单和(&)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!