Syntax: Operators

In a script symbols such as + and – perform a function. The plus sign can add two numbers together and the minus sign can subtract one number from another. These symbols are called Operators, and OpusScript provides a wide range of operators, which are listed below.

Operator Precedence

Some operators have a higher precedence than others, which means they will be calculated before other operators. Take for example Arithmetic Operators:

x = 2 + 3 * 4 / 2

The * and / symbols are used for multiplication and division. The * and / operators are of equal precedence, the + and - operators are also of equal precedence. However, the * and / operators have a higher precedence than + and -, which means the * and / operators are calculated before the + and - operators in an expression.

In the example above, the system will calculate the expression as:

3 * 4 = 12
12 / 2 = 6
2 + 6 = 8
x = 8

If you are in doubt as to what order an expression will be calculated or wish to force a particular order to the calculation use parenthesis. Parenthesis (i.e. round brackets) are calculated before other operators in an expression. For example, if you want x to equal 10 you could do the following calculation:

x = (3 + 3) * 4 / 2

Note that you can "nest" sets of parentheses, that is you can put sets of brackets inside other brackets.

Debugging Note:

The order operators are calculated can mean forgetting a bracket can change a result significantly, which can cause bugs in your program.

Arithmetic Operators

Basic arithmetic and some more advanced syntax.

Symbol

Meaning

Example

+

Addition

x = 5 + 4

-

Subtraction

x = 5 – 4

*

Multiplication

x = 5 * 5

/

Division

x = 27 / 3

++

Increment (add one to the value)

x = 1;
while (x < 10)
{
 x++;
}

--

Decrement (subtract one from the value)

x = 10;
while (x > 0)
{
 x--;
}

%

Modulo

Returns the remainder of an expression. Thus 8%3=2 while 9%3=0 (3 goes into 9 with no remainder)

 

Assignment Operators

An assignment operator assigns a value to the left hand side of the equal sign based on the value in the right hand side of the equal sign.

Symbol

Meaning

Example

=

Assign

Y = 10;
x = y;

will result in

x == 10

+=

Increment by

X += y is the same as X = x + y

-=

Subtract from

X -= y is the same as X = x – y

*=

Multiply by

X *= y is the same as X = x * y

/=

Divide by

X /= y is the same as X = x / y

Comparison Operators

A comparison operator compares the left hand side of the equal sign with the right hand side of the equal sign and returns a logical value of true or false based on whether the comparison matches or does not match. You can compare numerical or string values.

Strict equality does not permit the automatic conversion of types so with normal equals the text 2 and a number 2 would be equal but with strict equality they would not.

Symbol

Meaning

Example

==

Equals

if (2 == 2)

===

Strict equals

if ("2"==="2")

!=

Not Equal

if (3 != 2)

<

Less than

if (2 < 3 )

>

Greater than

if (3 > 2)

<=

Less than or equal to

if (2 <= 2)

>=

Greater than or equal to

if (2 >= 2)

Logical Operators

Logical Operators are normally used with Boolean values that return a value of true or false. The Logical Operators return the value of one of the specified expressions.

Symbol

Meaning

Example

&&

Logical AND

x = true && (2 == 2) will set x as true

||

Logical OR

x = true || (2 == 2) will set x as true

!

Logical NOT

x = !true will set x as false

String Operators

As well as the comparison operators that can be used on strings, the concatenate operator ( + ) concatenates two string values together.

Tip:

If you concatenate " /n" to the end of string you will insert a new line when displaying the string in a text object). Thus "line1"+"/n"+"line 2" will be displayed as: line1 line2

 

Symbol

Meaning

Example

+

Concatenates strings

x = "my " + "string"

will result in

x == "my string"

and

x = "my " + "second " + "string"

will result in

x == "my second string"

+=

Append a string

x = "my ";
x += "string";

will result in

x == "my string"