Forum Discussion

squirreltown's avatar
squirreltown
Roku Guru
12 years ago

If not

Is there a difference between these two statements?

if not m.motionbit = invalid

if m.motionbit <> invalid


thanks

6 Replies

  • "squirreltown" wrote:
    Is there a difference between these two statements?
    if not m.motionbit[x] = invalid

    if m.motionbit[x] <> invalid


    No, they are the same.
    "not" has lower precedence than comparison so things are allright.
    You can use "da" in console to look at the bytecode when in doubt.
  • Potentially. Depends on the type of "m.motionbit". If it were a boolean, for instance, then your first statement would compare it's inverse to invalid (which would probably cause a type mismatch). There is no difference between these two statements, though:

    if not (m.motionbit[x] = invalid)

    if m.motionbit[x] <> invalid

    Note the parentheses enforce the priority of how the expression is evaluated, so the equality gets evaluated prior to having the "Not" applied to it.
  • "TheEndless" wrote:
    Potentially. Depends on the type of "m.motionbit". If it were a boolean, for instance, then your first statement would compare it's inverse to invalid

    Nope - it does not depend at all on the value.
    Operations are performed in order of precedence, as provided in TFM. In the case of if not m.motionbit = invalid then ..., dot-operator has highest priority and goes first, followed by [], followed by comparison operator, and NOT is last. In other words, it is the same as this (which by the way works too):
    if (not ( ( (m.motionbit)[x] ) = invalid) ) then ...

    The order/precedence of the operations has been done in such a way to minimize need for parenthesis in day-to-day coding.
  • "EnTerr" wrote:
    "TheEndless" wrote:
    Potentially. Depends on the type of "m.motionbit". If it were a boolean, for instance, then your first statement would compare it's inverse to invalid

    Nope - it does not depend at all on the value.
    Operations are performed in order of precedence, as provided in TFM. In the case of if not m.motionbit = invalid then ..., dot-operator has highest priority and goes first, followed by [], followed by comparison operator, and NOT is last. In other words, it is the same as this (which by the way works too):
    if (not ( ( (m.motionbit)[x] ) = invalid) ) then ...

    The order/precedence of the operations has been done in such a way to minimize need for parenthesis in day-to-day coding.

    I guess I should have tried it first. But I think the better question is why would you want to "Not" a check for equality, instead of directly checking for inequality? What's the use-case in which you'd want/need to do that?
  • Well, docendo discimus.
    It's useful to understand how expressions are evaluated/calculated in principle. There is some order and logic in how B/S works - aside from the half-documented, no-type-checks roComponents - or i would have forsaken it by now as "another PHP / mySQL". I am quite interested where the VM underneath comes from but doubt someone will tell me.

    And of course, "when in doubt - use parenthesis".