• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Question about 'if's

Status
Not open for further replies.
Well it depends. If you have two boolean expressions connected with a logical and
then the second one will be only evaluated if the first one returns true.

JASS:
if true and false then
    // both are evaluted
endif

if false and false then
    // first is evlauted
    // second is ignored
endif

So, it depends if IsUnitEnemy(u, p), or as you want to change it, if not IsUnitEnemy(u, p) will be true or false.
 
Level 22
Joined
Aug 27, 2013
Messages
3,973
Even in language and reality, something that has "and" with them means both of them are necessary. (This is ignored most of the times though)
For example, "You and Her are summoned to attend the meeting".
It means both of them are summoned to attend the meeting, not just one of them.
If only one of them, that condition will not return true.
same goes with the if statement. Both conditions must be fulfilled for it to return true.
It takes both of the conditions into consideration before starting the actions of the other tasks.
 
Level 19
Joined
Dec 12, 2010
Messages
2,069
Even in language and reality, something that has "and" with them means both of them are necessary. (This is ignored most of the times though)
For example, "You and Her are summoned to attend the meeting".
It means both of them are summoned to attend the meeting, not just one of them.
If only one of them, that condition will not return true.
same goes with the if statement. Both conditions must be fulfilled for it to return true.
It takes both of the conditions into consideration before starting the actions of the other tasks.

don't apply human's logic with jass
false and true or true == false (jass)
 
Level 24
Joined
Aug 1, 2013
Messages
4,657
if false and true then
!=
if true and false then

The point where this becomes interesting is:
if dothisandgivefeedback() and false then
vs
if false and dothisandgivefeedback() then

In fact, today I actually made use of this to skip a check that would fail because it would be missing from the object's fields by doing:
if ($isCustom || $widget->getRequest())
If you really want to use it is just user preference, but you should be aware of what happens in order to properly debug your code.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,206
JASS is not a normal language. It lacks most common language features like bitwise operators and native modulus support.

It likely treats "and" and "or" as the same precedence level. It makes little difference anyway as one should be bracketing in such situations for readability.

Presented with...
A && B || C && D || E && F && G || H
Even knowing the precedence of the language (this example was C/C++) it is still confusing and can be error prone to modify as it is easy to make a mistake and modify the wrong group of terms. Imagine if the letters were large comparison terms. Imagine if there were 3 or 5 times as many terms.

Hence why it is often better for readability to bracket the terms...
(A && B) || (C && D) || (E && F && G) || H
There is absolutely no room for ambiguity if you do this.
 
Status
Not open for further replies.
Top