Tutorial: how to use if statements and operators !, &&, ||

PeteSampras

Your UI is fucking you. Stop using it.
Joined
Dec 12, 2007
Messages
3,956
Reaction score
49
Points
38
MQ2 is down and though i do plan to make a video for this, I cant at the moment. I figured I would make a quick tutorial here since I see the question asked on a semi regular basis: What do these symbols mean? !, &&, ||.

Those are called operators. Here is the wiki entry:
http://www.macroquest2.com/wiki/index.php/Operators

Operators:
The following three can be used with all variable types:
! mean "not" or "no value"
&& means "and"
|| means "or"

The others are used for integer (number) comparisons.
a==b - does a equal b
a!=b - does a not equal b
a<=b - is a less than or equal to b
a>b - is a greater than b
a>=b - is a greater than or equal to b

Then there are the math specific operators (skipping some):
a+b - add b to a
a-b - subtract b from a
a/b - a divided by b
a*b - a multiplied by b
a\b - whole number of times a is divided by b (useful for money or cost calculations)
a%b - remainder of a\b (useful for money or cost calculations)

You can use 2 methods to do calculations, /varcalc or Math.Calc:
/varcalc a ${a}+1
/varset a ${Math.Calc[${a}+1]}

Both of those examples will add 1 to "a"


Ok, so now how to use those with /if statements.
If statements are a form of flow control (http://www.macroquest2.com/wiki/index.php/Flow_Control) and use the following format:

Use parentheses to wrap ideas together. Everything inside a parentheses will ultimately evaluate as TRUE or FALSE and if TRUE, execute a command(s)
Code:
/if (conditions<conditions>) /command
or, if you want to execute more than 1 command:
Code:
/if (<conditions>conditions) { /command1 /command2 /command1000 }
You can break those up over multiple lines if you want, as long as it is enclosed in { }.
Code:
/if (<conditions>conditions) {
    /command1
    /command2
    /command1000
}
So now you can use variables and operators in conjunction with if statements to check conditions and issue commands. Lets see some basic examples.

Situation: if my HP are less than 50%, and I have more than 10% mana, let's cast my self heal.
code:
Code:
/if (${Me.PctHPs}>50 && ${Me.PctMana}>10) /casting "Self Heal" gem1
Situation: if there are no tanks in group or if I have more than 1 Xtarget, cast AE mez.
code:
Code:
/if (!${SpawnCount[group tank]}||${Me.XTarget}>1) /casting "AE Mez" gem2
Situation: if there are 2 npcs within 10 radius nearby AND there is no healer in group OR I am less than or equal to 10% HP, burn a disc and use ability mend if it is ready:
Code:
/if (${SpawnCount[npc radius 10]}==2 && !${SpawnCount[group priest]}||${Me.PctHPs}<=10 && ${Me.AbilityReady[Mend]}) {
     /disc "disc name"
     /doability "mend"
}
You can also use ! to mean no value or with strings or bools:
Situation: i want to /echo hi if i have no hitpoints and my name isnt equal to "petesampras".
Code:
/if (!${Me.PctHPs} && !${Me.Name.Equal[petesampras]}) /echo hi
Strings also have their own built in way of using !, and you can use either method:
Code:
${Me.Name.NotEqual[Petesampras]}
But if you use a double negative, then it checks the opposite, so this would check to see if my name really is equal to petesampras due to double negative:
Code:
!${Me.Name.NotEqual[PeteSampras]}
You can also nest parentheses and if statements inside each other.
Situation: if there are more than 3 npcs within 10 radius check to see if a series of discs are ready, and depending on how many npcs there are, use certain discs.
Code:
/if (${SpawnCount[npc radius 10]}>=3) {
    /if (${SpawnCount[npc radius 10]}==3 && ${Me.CombatAbilityReady[Innerflame]}) /disc innerflame
   /if (${Me.CombatAbilityReady[Whirlwind]} && (${Me.PctHPs}<50||${SpawnCount[group tank]})) /disc whirlwind
   /if (${Me.SpellReady[ae aggro]}) /casting "AE aggro" gem1
}
You can bury as many /if statements inside each other as you want. Using the combo of operators and if statements will allow you to set up MQ2melee holyshits and give you a means to control what is happening in a macro.</conditions></conditions></conditions>
 
Last edited:
Bookmarked to later read. I was actually going to talk to you about this on irc but now I can read about it myself and try to figure it out. Thanks a bunch, Pete. :thumbup:

Sent from my SAMSUNG-SGH-I317 using Xparent Skyblue Tapatalk 2
 
Pete, can you expand at all on the use of else commands? For example, how accurate is this?

/if (condition1=true) {
(process here)
} else /if (condition2=true) {
(another process here)
}

And variations on else. I have rough BASIC skills from ages ago and remember the IF THEN ELSE statement in concept and I know MQ has it as well.
 
Pete, can you expand at all on the use of else commands? For example, how accurate is this?

/if (condition1=true) {
(process here)
} else /if (condition2=true) {
(another process here)
}

And variations on else. I have rough BASIC skills from ages ago and remember the IF THEN ELSE statement in concept and I know MQ has it as well.

That is spot on in your break down. You could also add in a

Code:
/if (condition1=true) {
(process here)
} else /if (condition2=true) {
(another process here)
} else {
(something that always happens if statements above arent true)
}

You can add in more condition checks as well if you need. So if you need more else /if sections that is doable.
 
Last edited:
Cool.

All MQ needs now is a CASE command.
 
Just wanted to sort of clear up the logical not (!) operator.

Think of it as pretty simple (this), and you will be OK:

0 = FALSE
NOT 0 (generally 1, but any non-zero value) = TRUE

So example like: if (myval) { do this }
is if myval is true, or that is, it is not zero.

Example: if (!myval) { do this }
is if myval is NOT true (false), or it IS ZERO.

The basic condition of a /if statement, is like: if (TRUE) { do all this crap}

That is, if ALL CONDITIONS inside the () evaluate as true, so that the final result is TRUE, then do those said statements.

Like: /if (myval1 && myval2 && myval3==40 && !myval4 && myval5==60) { do this }

It evaluates each part, so that in order to do the "do this" stuff, it would need to be like: /if (TRUE && TRUE && TRUE && TRUE && TRUE) { do this}

ANY part of that that is FALSE, would cause them not to be executed. Of course, you could always have OR operators, so that something like: /if (myval1 && !myval2 || !myval3) { do this}

Could be like: /if (TRUE && FALSE || TRUE) { do this}

So that it evaluates to true. You have to understand precedence in that case, or push those precedents with () in order to cause what you want. What I put, will evaluate the logical OR's first, then the AND's, so it would essentially look like: /if (TRUE && (FALSE || TRUE)), which would end up being a result of TRUE (i.e., the {} statements would be executed).

If you look at boolean, it looks like:

0 AND 0 = false
0 AND 1 = false
1 AND 0 = false
1 AND 1 = true

0 OR 0 = false
0 OR 1 = true
1 OR 0 = true
1 OR 1 = true

NOT (logical) reverses the input state. i.e., if the result would be TRUE, then it is FALSE. If it is FALSE, it would be TRUE.

The basic way to look at it then, would be NORMAL:
zero = FALSE
non-zero = TRUE

If you NOT a value, then it would be:
zero = TRUE
non-zero = FALSE

No need to talk about NAND, NOR, or XNOR in these conditions.

Just remember basically:

AND = if both of the operands are TRUE, the result is TRUE, otherwise it is FALSE

OR = if either, or both, of the operands are TRUE, then the result is true

NOT = reverse logic on whatever variable (or result) you are working with.

The overall condition of any macro ( or mq2melee condition, for example) needs to evaluate to TRUE, to execute.

So, it would be basically: /if (TRUE) { do this shit} or /if (FALSE) { don't do this shit}

Every part of what you put in the () has a TRUE or FALSE evaluation, so that it ends up with a final overall TRUE or FALSE condition.

/if (TRUE && TRUE && TRUE && TRUE) { do this }
/if (TRUE && (TRUE || TRUE) || TRUE) { do this}

No matter how you break it down, it ends up as overall:

/if (TRUE) { do this}
/if (FALSE) { don't do this}

Hope that makes sense.

htw
 
Last edited:
Pete, can you expand at all on the use of else commands? For example, how accurate is this?

/if (condition1=true) {
(process here)
} else /if (condition2=true) {
(another process here)
}

And variations on else. I have rough BASIC skills from ages ago and remember the IF THEN ELSE statement in concept and I know MQ has it as well.

Sure. Else statements are performed when the original /if statement returns FALSE. It follows the same premise as the original /if statement. Either issue a single command, or wrap the commands in { }. You can use { } wrap even with 1 command if you want.
Code:
/if (conditions) /command
else /command
or if it is more than 1 command, you must wrap it in { }.
Code:
/if (conditions) /command
else {
/command1
/command2
}
or
Code:
/if (conditions) {
/command1
/command2
} else {
/command1
/command2
}
Or as you pointed out, you can nest more /if statements within an else statement just like you can an /if statement. I have found that for my personal best practice, to always use braces in conjunction with if/else combo so i visualize it more easily. Do whatever works best for you, but stay consistent so when you review your own code it is easier.
 
You guys are greatly appreciated. There are a few things that are finally making sense. Now if I can just apply what I think makes sense now.