Phantom supports
many different operators. Operators perform an operation between two
variables or constants. The following is a list of operators that Phantom
supports:
+ - Addition operator
- - Subtraction operator
* - Multiplication operator
/ - Division operator
% - Modulo operator
^ - Power operator
= - Assignment operator
& - Bit-wise AND
operator
| - Bit-wise OR operator
> - Greater than operator
< - Less than operator
== - Equality operator
>= - Greater than
or equal to operator
<= - Less than or
equal to operator
!= - Inequality operator
&& - Logical
AND operator
|| - Logical OR operator
! -
Logical NOT operator
The syntax to use most operators is:
Var1 OP Var2
where,
Var1,Var2 - Variables operated on
OP - One of the operators mentioned above
The exception to this is the Logical NOT operator (!), which does not
have Var1 on the left side.
The order of precedence of the operators are as follows (earlier in
list is higher precedence):
1. ^
2. *,/,%
3. +,-
4. <,>,!,>=,<=,!=
5. &,|,&&,||
6. =,==
Operators with a higher
precedence are performed before operators with a lower precedence.
If a line contains operators with the same precedence, the operators
with the same precedence will be performed from left to right. Statements
placed in parentheses '()' are moved to the highest precedence.
Not all variable types support all operators. To see if a variable type
supports an operator, see the Variables
section.
Example
Code |
int
i;
bool b;
# Assignment operator
i = 10;
disp(i);
# Plus operator
i = 3 + 6;
disp(i);
# Multiply operator
i = 4 * 8;
disp(i);
# Power operator
i = 4^3;
disp(i);
# Modulo operator
i = 6 % 4;
disp(i);
# Bitwise OR operator
i = 4 | 1;
disp(i);
# Bitwise AND operator
i = 4 & 1;
disp(i);
# Logical NOT operator
b = !0;
disp(b);
# Logical AND operator
b = 1 && 5;
disp(b);
# Equality operator
b = (i==10);
disp(b);
# Inequality operator
b = (i!=10);
disp(b);
# Greater than
operator
b = (i>20);
disp(b);
# Precedence example
# This gives 12
i = 2*4 + 16/2^2;
disp(i);
# Changing precedence
using ()
# This gives 288
i = 2*(4+16/2)^2;
disp(i);
|
Output |
10
9
32
64
2
5
0
true
true
false
true
false
12
288 |