An operator is a special symbol used to perform assignment, arithmetic, comparison or other operation. Here are listed binary operators, that is, operators that are used with two operands, like a+b. See also: unary operators (used with single operand), member access operator . and array element access operator [].
result = operand1 operator operand2
For example, a = b + 1 means "add b and 1, and assign the result to a". Here b is operand1, + is operator, 1 is operand2.
operand1 operator operand2
Here operand1 receives result. For example, a + 1 means "add 1 to a" (the same as a = a + 1).
operands - any expressions (variables, constants, functions, expressions with operators).
operator - one of symbols listed below.
To assign something to a variable, use =. Example: a = 1
When used with if or in other assignment, = is interpreted as comparison operator (see below).
Arithmetic | |
+ | add. Example: a = b + 1 |
- | subtract |
* | multiply |
/ | divide |
% | modulus |
Bitwise | |
& | AND |
| | OR |
^ | XOR |
~ | AND NOT |
<< | left shift |
>> | right shift (unsigned) |
Priority of all arithmetic and bitwise operators is equal. For example, in a=b+c*d, at first is calculated b+c, then multiplied by d.
= == |
equal. Example: if(a=5) out "a is 5" |
! != <> |
not equal |
> | more |
>= | more or equal |
< | less |
<= | less or equal |
Priority of comparison operators is lower than of arithmetic and bitwise operators. For example, if(a < b+c), at first is calculated b+c, then compared with a.
Result of comparison operators is 1 (true) or 0 (false).
and && |
logical AND. Example: if(a=b and c<d) out "a is equal to b, and c is less than d" |
or || |
logical OR |
Priority of logical operators is lower than of arithmetic, bitwise and comparison operators. For example, in if(a>b and c!=d), at first are compared a>b, then c!=d.
Result of logical operators is 1 or 0. Operator and gives 1 if both operands are not 0. If operand1 is 0, operand2 is not evaluated. Operator or gives 1 if one of operands is not 0. If operand1 is not 0, operand2 is not evaluated.
Comparison | |
= | equal, case sensitive. Example: if(s="abc") out "s is ''abc''" |
! | not equal, case sensitive |
~ | equal, case insensitive |
Note that "" and null are not equal. Don't use if(s="") to check if s is empty. Instead use if(empty(s)) or if(!s.len). See also: empty, StrCompare.
Join (syntax2) | |
+ | append. Example: str s; s="notepad"; s+".exe" |
- | append to the beginning |
Operators + and - are not used with syntax1 to join strings. Instead of s1=s2+s3 use s1.from(s2 s3) or s1=F"{s2}{s3}".
See also: strings
Only the assignment operator can be used with all variable types. Other operators can be used with QM intrinsic types (int, byte, word, long, double, str, lpstr). With other types, some operators cannot be used, or the result may be not as you need. Therefore usually it is better to convert to the intrinsic type that you need. Example: BSTR b="xx"; str s=b; if(s="xx") ;;don't use if(b="xx).
Operands of type str are interpreted as lpstr, except with str comparison operators =, !, ~.
lpstr and pointers are interpreted as numeric (unsigned int). For example if(s1=s2) compares pointers, but not the strings. Another example: in s1=s2+1, s1 receives address of the second character of s2 (if s2 was "tree", s1 will be "ree").
Like in C/C++, arithmetic and bitwise operators may be followed by =. Example: a+=1.
See also: operator priority expression type and precision C run-time library conversions, etc.
i = 5 ;;assign 5 to variable i i + 2 ;;add 2 to variable i a = b + 10 ;;calculate sum of b and 10, and assign it to variable a a + b + 10 ;;calculate sum of b and 10, and add it to variable a int j = i + 2 ;;declaration and assignment in the same statement ave = i + j / 2 ;;add j to i, divide by 2, then assign result to variable ave f = i - (10 * j) + Func(a b) * 10 ;;multiply 10 and j, extract result from i, call function Func and add its return value, multiply by 10, then assign result to variable f str s = "notepad" ;;declare variable s and assign string "notepad" s + ".exe" ;;append ".exe" if(i < 10 and s = "notepad.exe") i + j / 10 ;;if i is less than 10, and s is equal to "notepad.exe", calculate j / 10 and assign result to variable i i = Func2(j s (i + 5) f) ;;call function Func2 and assign its return value to variable i; pass four arguments, one of them is sum of i and 5 lef a-10 100 ;;left click; x is a-10, y is 100