1. Find your way through the deepest dungeon in the 18th Mini Mapping Contest Poll.
    Dismiss Notice
  2. A brave new world lies beyond the seven seas. Join the 34th Modeling Contest today!
    Dismiss Notice
  3. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
Hive 3 Remoosed BETA - NOW LIVE. Go check it out at BETA Hive Workshop! Post your feedback in this new forum BETA Feedback.
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

Precedence in JASS

Discussion in 'JASS/AI Scripts Tutorials' started by Earth-Fury, Nov 21, 2007.

  1. Earth-Fury

    Earth-Fury

    Joined:
    Feb 18, 2004
    Messages:
    538
    Resources:
    1
    Tutorials:
    1
    Resources:
    1
    Precedence in JASS

    Definition of Precedence
    In a programming language, the precedence of an operator determines in what order it will be evaluated. An operator with a higher precedence will be evaluated before another with a lower precedence. For instance multiplication has a higher precedence than addition.
    http://www.google.ca/search?q=define:+precedence

    The rules of precedence are important if you don’t want your code littered with needless parenthesis. JASS has only a few operators, so remembering their precedence is not so hard:

    ( )
    not
    * /
    + -
    <= >= > < == !=
    and or


    The higher on the list, the higher an operators precedence. If two operators are in the same row, they share the same precedence and are thus executed from left-to-right. By this chart, the following examples are all true:

    Code (vJASS):
    a == b != c // Is the same as…
    (a == b) != c


    Code (vJASS):
    a != b == c // Is the same as…
    (a != b) == c


    Code (vJASS):
    1 + 2 * 34 / 2 // Is the same as…
    1 + (2 * 3)(4 / 2)


    Code (vJASS):
    false == 1 > 2 // This will fail to even compile, as it’s the same as…
    (false == 1) > 2


    Code (vJASS):
    not 4 > 3 // This will also fail to compile, as it’s the same as…
    (not 4) > 3


    Code (vJASS):
    a == b or c == d // Is the same as…
    (a == b) or (c == d)



    The
    or
    and the
    and
    operators are executed in a short circuit manner. This means that if from the current execution, an answer is determinate, execution stops. By this, the following are both true:

    Code (vJASS):
    f() // Returns false
    t() // Returns true

    t() or f() // f() will never be executed.

    f() and t() // t() will never be executed.




    Much credit to PurplePoot for helping me test precedence
    Edit: Tiny formatting edit and removal of signature
     
    Last edited: Feb 29, 2008
  2. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,430
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    I never even knew this. =D

    Thanks.

    Seems good to me.
     
  3. PurplePoot

    PurplePoot

    Joined:
    Dec 14, 2005
    Messages:
    11,161
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    Really no reason not to approve this, IMO.

    ~Guess what?
     
  4. Herman

    Herman

    Joined:
    Aug 20, 2007
    Messages:
    877
    Resources:
    0
    Resources:
    0
    Woah, thats really useful to reference to

    +rep
     
  5. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,413
    Resources:
    1
    JASS:
    1
    Resources:
    1
    you have forgotten to write the t() and f() functions
    Else, nice to know but i think for my mental sanity (to keep the code easily readable) i will still use () ^^
     
    Last edited: May 4, 2008
  6. PurplePoot

    PurplePoot

    Joined:
    Dec 14, 2005
    Messages:
    11,161
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    Both will be executed, of course.
     
  7. Herman

    Herman

    Joined:
    Aug 20, 2007
    Messages:
    877
    Resources:
    0
    Resources:
    0
    Uhhh, wait a second....

    Does that mean if you have a function for converting indexes, only the first function will fire, bugging out your JASS???

    Ex.
    Code (vJASS):
    udg_UnitArray[IndexFunction(u1)] == udg_UnitArray[IndexFunction(u2)]


    Or maybe...

    Code (vJASS):
    udg_UnitArray[IndexFunction(u1)] == udg_UnitArray[IndexFunction(u2)] and udg_UnitArray[IndexFunction(u3)] == udg_UnitArray[IndexFunction(u4)]
     
  8. PurplePoot

    PurplePoot

    Joined:
    Dec 14, 2005
    Messages:
    11,161
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    Ands and Ors are short circuit, meaning in any circumstance where the answer is known after one of the two booleans is evaluated, they will immediately finish execution.
     
  9. Herman

    Herman

    Joined:
    Aug 20, 2007
    Messages:
    877
    Resources:
    0
    Resources:
    0
    Ohhh, I gotcha, so if lets say it is an Or, and the first one is true, it will.... oh thats what the example means ><

    So, if your second boolean function has some kind of unrelated call statement in there, you should be careful, because it will not always go through that secondary conditional function
     
  10. PurplePoot

    PurplePoot

    Joined:
    Dec 14, 2005
    Messages:
    11,161
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    Yes, exactly.