Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • AlexCamden 1 post 71 karma points
    Feb 18, 2020 @ 09:48
    AlexCamden
    0

    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/

  • John C Scott 473 posts 1183 karma points
    Feb 18, 2020 @ 10:14
    John C Scott
    0

    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

  • John C Scott 473 posts 1183 karma points
    Feb 18, 2020 @ 10:18
    John C Scott
    0

    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

  • John C Scott 473 posts 1183 karma points
    Feb 18, 2020 @ 10:35
    John C Scott
    0

    Are there any other operators in the expression?

    eg

    if (i>1 & today.Year>=2020 || i<0) 
    

    may not give the same result as

    if (i>1 && today.Year>=2020 || i<0) 
    

    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 hope this makes sense ?

  • Steve Megson 151 posts 1024 karma points MVP c-trib
    Feb 18, 2020 @ 11:03
    Steve Megson
    1

    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

    true || true && false
    == true || (true && false)
    == true || false
    == true
    

    but

    true | true && false
    == (true | true) && false
    == true && false
    == false
    
  • Tiffany 11 posts 86 karma points MVP 4x c-trib
    Feb 18, 2020 @ 17:59
    Tiffany
    2

    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 :)

Please Sign in or register to post replies

Write your reply to:

Draft