why would "&&" and "&" give different results when used in an if statement?
this happened a while ago so i dont have the code here, but i had an if statement that checked a bunch of things with "&&". it didnt work the way i expected, but when i switched it to "&" it suddenly worked. https://mobdro.onl/
i read that "&&" stops checking once it finds something that is false, and "&" keeps checking anyway, but it shouldnt make a difference on the final result.
You get the same result but the evaluation is different
So say the right hand side is a big complicated processor intensive query if the first part is not true then it's not worth evaluating the second part.
EG
If (Today is a Wednesday
& My Local Macdonalds Has Veggie Wraps on Special)
{ Get Macdonalds today }
This will always be false if today is not Wednesday
So if the second one is expensive to evaluate (opens up an API to MacDs.Com or whatever then don't bother to check)
More likely might be
If (IsCurrentUserLoggedIn && IsTheGreatGrandParentOfThisPage a document type of product catalogue which has delivery options available to the user in this currency)
So no point evaluating the second part
sometimes this may change execution order and so the result or if there are not's you can also get a different result as the second part is not evaluated
where SomeReasonNotTo1() & SomeReasonNotTo2() may return true or false
and we only want to do it if both are false
we might say
if ( ! SomeReasonNotTo1() & SomeReasonNotTo2() )
so in this case if both return false we will get false and the overall result will be true (not false)
however we might get a different result from
if ( ! SomeReasonNotTo1() && SomeReasonNotTo2() )
if reason1 returns false & although reason2 is true reason2 won't be evaluated and so the final result will be true where it should be false
I get the same result for your examples, but I can produce different results by mixing the two types. & and | have higher precedence than && and ||, so
Just to help clear things up in case you missed it:
& will always evaluate both expressions whilst && will only evaluate the second expression if the first is true
If you have 2 expressions (A) and (B) - using the '&' operator will evaluate both expressions and return false if either one is false.
Using the '&&' operator will only evaluate (B) if (A) is true.
As demonstrated in the posts above - each method has valid purpose because there may be specific instances to use & - however using && is generally the preferred for expressions of this type.
why would "&&" and "&" give different results when used in an if statement?
this happened a while ago so i dont have the code here, but i had an if statement that checked a bunch of things with "&&". it didnt work the way i expected, but when i switched it to "&" it suddenly worked. https://mobdro.onl/
i read that "&&" stops checking once it finds something that is false, and "&" keeps checking anyway, but it shouldnt make a difference on the final result.
and whats the point of having these 2 different ones anyway? https://sarkariresult.onl/
You get the same result but the evaluation is different So say the right hand side is a big complicated processor intensive query if the first part is not true then it's not worth evaluating the second part.
EG If (Today is a Wednesday & My Local Macdonalds Has Veggie Wraps on Special) { Get Macdonalds today }
This will always be false if today is not Wednesday So if the second one is expensive to evaluate (opens up an API to MacDs.Com or whatever then don't bother to check)
More likely might be If (IsCurrentUserLoggedIn && IsTheGreatGrandParentOfThisPage a document type of product catalogue which has delivery options available to the user in this currency) So no point evaluating the second part
But yes definitely - the results of both should always be the same
false & false == false false && false == false
false & true == false false && true == false
true & false == false true && false == false
true & true == true true && true == true
Are there any other operators in the expression?
eg
may not give the same result as
sometimes this may change execution order and so the result or if there are not's you can also get a different result as the second part is not evaluated
where SomeReasonNotTo1() & SomeReasonNotTo2() may return true or false and we only want to do it if both are false
we might say
so in this case if both return false we will get false and the overall result will be true (not false)
however we might get a different result from
if reason1 returns false & although reason2 is true reason2 won't be evaluated and so the final result will be true where it should be false
i hope this makes sense ?
I get the same result for your examples, but I can produce different results by mixing the two types.
&
and|
have higher precedence than&&
and||
, sobut
Just to help clear things up in case you missed it:
& will always evaluate both expressions whilst && will only evaluate the second expression if the first is true
If you have 2 expressions (A) and (B) - using the '&' operator will evaluate both expressions and return false if either one is false.
Using the '&&' operator will only evaluate (B) if (A) is true.
As demonstrated in the posts above - each method has valid purpose because there may be specific instances to use & - however using && is generally the preferred for expressions of this type.
Hope this helps :)
is working on a reply...