Operators

Operators are special symbols that tell a RapidScript program to perform a specific mathematical or logical operation.

Arithmetic

These arithmetic operators require both operands to be numeric types (except for the + operator).

OperatorNameUsage

+

addition

addend1 + addend2

-

subtraction, unary negative

minuend - subtrahend, -number

*

multiplication

factor1 * factor2

/

division

dividend / divisor

%

modulo

dividend % divisor

Notes

  • The addition operator can also take operands that are strings. If either operand is a string, the returned value will be a concatenation of both operands as a string (numeric types will be converted to their string equivalent, e.g. 4 becomes "4"). For example: "hello" + "there" returns "hello there".

  • The division operator will return its quotient as a double. If the quotient is assigned to an integer variable, the fractional values will be discarded.

  • The modulo operator performs an integer division, then returns the remainder. For example, 10 % 3 returns 1.

  • The unary negative sign operator returns the additive inverse of the given number.

Assignment

Assignment operators assign a new value to the variable on the left-hand side of the operator. The left-hand side must be a variable, while the right-hand side may be a literal value or a variable.

Assignment operators containing an arithmetic operator will modify the variable using the given value and arithmetic operation. The operation will be performed using the variable as the left-hand term and the given value as the right-hand term. The result of the operation will be assigned to the variable. The equivalent statements are shown in the table below.

OperatorUsageEquivalent

=

variable = value;

var = value;

+=

var += value;

var = var + value;

-=

var -= value;

var = var - value;

*=

var *= value;

var = var * value;

/=

var /= value;

var = var / value

%=

var %= value;

var = var % value;

Terms on the right-hand side of the = operator are evaluated before the left-hand side. In the statement var = var + value, var + value will use the original value of var before a new value is assigned to it.

Comparison and Relation

The comparison and relation operators form expressions that return a bool value representing whether or not the expression is true.

OperatorDescriptionUsage

==

Equal to

value1 == value2

!=

Not equal to

value1 != value2

>

Greater than

value1 > value2

<

Less than

value1 < value2

>=

Greater than or equal to

value1 >= value2

<=

Less than or equal to

value1 <= value2

Notes

  • The comparison and relation operators all operate on numeric types. The == and != operators can also take bool types. None of these operators can take string (or void) types.

Logical

The logical operators form logical expressions, and can only operate on bool values.

OperatorDescriptionUsage

&&

AND

value1 && value2

||

OR

value1 || value2

^

XOR

value1 ^ value2

!

NOT

!value

Notes

  • The logical XOR (also known as exclusive OR) operator returns true if and only if one of its operands is true. If both are true or false, then the exclusive OR returns false. The OR operator on the other hand will return true if either or both operands are true.

  • The logical NOT returns the complement of the given operand. !true returns false, and !false returns true.

  • The logical AND returns true if and only if both operands evaluate to true.

Increment and Decrement

Increment and decrement operations must operate on a variable. They alter the variable's value, increasing or decreasing it by one. They may also be applied as a prefix or a suffix, which changes when the increment/decrement occurs.

OperatorDescriptionUsage

++

Increment

++variable or variable++

--

Decrement

--variable or variable--

Notes

  • When used as a prefix (known as a pre-increment or pre-decrement), the operator increases (or decreases) the value of the variable by one, then returns the altered value.

  • When used as a suffix (known as a post-increment or post-decrement), the operator increases (or decreases) the value of the variable by one but returns the original, unaltered value.

Bitwise

Bitwise operators operate on the bits in the binary representation of numeric types.

OperatorUsage

&

Bitwise AND

value1 & value2

|

Bitwise OR

value1 | value2

Notes:

  • Assigning the value returned from these operators to a data type that is too small to contain all of the bits will truncate the most significant bits until the value fits. For example, doing a bitwise operation with two int64 values then assigning the result to an int16 variable will only keep the first 16 bits of the result.

Last updated