AutoIt has the following assignment, mathematical, comparison, and logical operators.
Operator | Description |
---|---|
Assignment operators | |
= | Assignment e.g. $vVar = 5 (assigns the number 5 to $vVar) |
+= | Addition assignment e.g. $vVar += 1 (adds 1 to $vVar) |
-= | Subtraction assignment |
*= | Multiplication assignment |
/= | Division assignment |
Concatenation operators | |
& | Concatenates/joins two strings e.g. "one" & 10 (results in "one10" and not 11) |
&= | Concatenation assignment e.g. $vVar = "one", and then $vVar &= 10 ($vVar is now "one10") |
Mathematical operators | |
+ | Adds two numbers e.g. 10 + 20 (is the result of 30) |
- | Subtracts two numbers e.g. 20 - 10 (is the result of 10) Also acts as a unary minus (sets the negative value of a variable) See explanatory note below |
* | Multiplies two numbers. e.g. 20 * 10 (is the result of 200) |
/ | Divides two numbers. e.g. 20 / 10 (is the result of 2) |
^ | Raises a number to the power. e.g. 2 ^ 4 (is the result of 16) |
Comparison operators (case-insensitive if used with strings except for ==) | |
= | Tests if two values are equal. e.g. If $vVar = 5 Then (true if $vVar equals 5). Case-insensitive when used with strings. See below about comparing with mixed datatypes. |
== | Tests if two strings are equal. Case-sensitive. The left and right values are converted to strings if they are not strings already. This operator should only be used if string comparisons need to be case-sensitive. |
<> | Tests if two values are not equal. Case-insensitive when used with strings. To do a case-sensitive not equal comparison use Not ("string1" == "string2") |
> | Tests if the first value is greater than the second. Strings are compared lexicographically even if the contents of the string happen to be numeric. |
>= | Tests if the first value is greater than or equal to the second. Strings are compared lexicographically even if the contents of the string happen to be numeric. |
< | Tests if the first value is less than the second. Strings are compared lexicographically even if the contents of the string happen to be numeric. |
<= | Tests if the first value is less than or equal to the second. Strings are compared lexicographically even if the contents of the string happen to be numeric. |
Logical operators | |
And | Logical And operation e.g. If $vVar = 5 And $vVar2 > 6 Then (True if $vVar equals 5 and $vVar2 is greater than 6) |
Or | Logical Or operation e.g. If $vVar = 5 Or $vVar2 > 6 Then (True if $vVar equals 5 or $vVar2 is greater than 6) |
Not | Logical Not operation e.g. Not 1 (False) |
Conditional operator | |
? : | Select conditionally an expression. e.g. $condition ? $expression1 : $expression2 ($expression1 if $condition is True or $expression2 if False) See the Ternary operator for an example. |
When more than one operator is used in an expression the order in which things happen is controlled by operator precedence. The precedence used in AutoIt is given below. Where two operators have the same precedence the expression is evaluated left to right.
From highest precedence to lowest:
Not
^
* /
+ -
&
< > <= >= = <> ==
And Or
e.g. 2 + 4 * 10 is evaluated as 42:
4 * 10 (equals 40)
2 + 40 (equals 42)
As the * has a higher precedence than + it occurs before the addition.
Although the operator precedence should suffice in most cases, is recommended to use brackets to force the order of evaluation if the result is critical or if the expression is complex.
e.g. (2 + 4) * 10 equals 60.
This is particularly true for the - operator which can be used for both binary subtraction (subtraction of 2 numbers)and unary negation (setting the negative of a value). The use of brackets is highly recommended in this case to prevent confusion.
Note the following when using the logical operators And, Or:
e.g. If MyFunc1() Or MyFunc2() Then (MyFunc2() is not called if MyFunc1() returns true)
e.g. If MyFunc1() And MyFunc2() Then (MyFunc2() is not called if MyFunc1() returns false)
Care is needed if comparing mixed datatypes, as unless the case-sensitive (==) string operator is used, mixed comparisons are usually made numerically. Most strings will be evaluated as 0 and so the result may well not be the one expected. It is recommended to force the items being compared into the same datatype using Number()/String() before the comparison.
#include <MsgBoxConstants.au3>
Local $iNumber = 0
Local $sString = "Some string"
; This will evaluate to true, as the string is converted to a number i.e. zero
If $iNumber = $sString Then
MsgBox($MB_SYSTEMMODAL, "", $iNumber & " and '" & $sString & "' are equal")
EndIf
; This will evaluate to false, as the string and number are compared by value and datatype
If $iNumber == $sString Then
MsgBox($MB_SYSTEMMODAL, "", $iNumber & " and '" & $sString & "' are equal")
EndIf