The following mathematical operators are used in KoLmafia:
|+||Addition||Performs addition and string concatenation|
|%||Modulo||Returns the remainder after division|
Note that, with the exception of using "+" for string concatenation, these operators can only be used on int or float datatypes.
The following mathematical operators are used for operating on the bits of integers. The logical operators (&, |, ^) work either with booleans or integers while the others operate upon integers only. If the operands are booleans, then result will also be a boolean.
|&||and||a & b|
||||or||a | b|
|^||XOR||a ^ b|
|<<||left shift||a << b|
|>>||right shift||a >> b|
|&=||and||a &= b --> a = a & b|
||=||or||a |= b --> a = a | b|
|>>>||unsigned right shift||a >>> b|
The following assignment operators are used in KoLmafia (let a = left operand, b = right operand):
|=||a = b|
|+=||a = a + b|
|-=||a = a - b|
|*=||a = a * b|
|/=||a = a / b|
|%=||a = a % b|
|**=||a = a ** b|
|^=||a = a ^ b|
|>>=||a = a >> b|
|>>>=||a = a >>> b|
Of these, only += and = are usable for strings. See Mathematical Operators for information regarding the basic Mathematical Operators.
To follow these examples, a basic understanding of the concepts found on Control Structures would be helpful.
In order to create more complex if statements, we need to understand the basic relational operators:
|!=||not equal to|
|<=||less than or equal to|
|>=||greater than or equal to|
Note that you cannot mix most datatypes within a comparison or KoLmafia will abort with an error, with the exception of mixing types int and float, where KoLmafia will do a transparent type conversion behind-the-scenes. If you need to compare different datatypes, use one or more of the Datatype Conversion functions. Also, == is case-insensitive with respect to strings.
Another exception is with strings. If the first datatype is a string, and the second isn't, the latter will be silently converted into a string. Remember that strings are compared alphabetically (with the alphabet here being the characters' ASCII value), just like in a dictionary, in which "smaller" means "would come before".
Take the habit of always converting your datatypes to avoid unexpected problems.
Note that the above operators only work with boolean values & datatypes. To make use of them with other datatypes, you will either need to first perform a Datatype Conversion, or you will need to nest your operations such that a boolean value is used with the boolean operators.
ASH uses Short-circuit_evaluation.
What this means is that in both of these examples, expensive_function() is not evaluated (i.e. is skipped):
KoLmafia follows Java's Operator Precedence rules with a few exceptions.
(The exceptions being operators that exist in only one or the other; operators that exist in both have the same precedence in both.)
|14||(reserved for postfix ++ and --)|
|13||! ~ contains remove (reserved for prefix ++ and --)|
|11||* / %|
|9||<< >> >>>|
|8||< > <= >=|
|1||(reserved for ?:(ternary conditional))|
|0||(reserved for assignments)|
Statements inside a () pair are always evaluated first, then in order of precedence as listed above (highest number precedence first), then left-to-right.