Page tree
Skip to end of metadata
Go to start of metadata

Operators are the base commands each programming language is built on. They provide ability to perform basic mathematical and logical operations.

Requirements

To understand this article, you should read the following articles:

Terms

Operand
An Operand is any value given to an operator.
Expression
An expression is basically any code that returns a value. Read Expression for more information.
Unary Operator
An unary operator is an operator that requires only one operand.
Unary operation:
operator expression
Binary Operator
A binary operator is an operator that requires two operands.
Binary operation:
expression operator expression

Assignment Operators

Assignment operators are used to assign values to a Variables. OFP's scripting language provides only one assignment operator.


Assignment:

Identifier = expression


Example 1:

a = b

You might think that this operator compares a and b, but that is not the case. = simply sets the left value to be the right one. There don't exist any other assignment operators like +=, -= etc., that can be found in other programming languages.


Example 2:

a = b*c

Arithmetic Operators

Remember arithmetic operations from school? These work just the same way.

All operands of arithmetic operations must be Number. Arithmetic operations always return a Number.

Unary arithmetic operators
OperatorNameExample
-Negation-a
Binary arithmetic operators
OperatorNameExample
+Additiona + b
-Substractiona - b
*Multiplicationa * b
/Divisiona / b
 %Moduloa % b
modModuloa mod b
^Raise to the power ofa ^ b

Modulo returns the remainder of the division a / b.


See also: Math Commands

Logical Operators

Logical operators evaluate Boolean values. All operands of logical operations are Booleans.

A logical operation always returns a Boolean.

Unary logical operators
OperatorNameExample
 !Not!a
notNotnot a

The Not-operator always returns the inverse Boolean value. If a Boolean a is true, !a returns false and vice versa.

Binary logical operators
OperatorNameExample
&&Anda && b
andAnda and b
||Ora || b
orOra or b
<>Xora <> b
xorXora xor b
  • And only returns true if both operands are true
  • Or returns true if one or both operands are true
  • Xor returns true if exactly one operands is true (V 2.06+)


Xor, Nor and Nand operators can be simulated using the basic operators though:

Combined logical operators
NameCombination
Xor((a || b) && !(a && b))
Nor!(a || b)
Nand!(a && b)
  • Xor returns true if exactly one of both values is true
  • Nor returns true if none of both values is true
  • Nand returns true if not both values are true at the same time

Comparison Operators

Comparison operators compare two values. Operands of comparisons may be of type Number, Side - Data Type, String, Object, Group, Structured_Text, Config-2, Display or Control.

Comparisons always return a Boolean: true if the comparison matches, false if not.

Comparison operators
OperatorNameExample
==Equala == b
 !=Not equala != b
<Less thana < b
>Greater thana > b
<=Less or equala <= b
>=Greater or equala >= b

Note: You can't compare Boolean values. Comparing a Boolean value with true is the same as the value itself. Comparing a Boolean value with false is the same as the inverse value.

a == true
// invalid, but equal to
a
a == false
// invalid, but equal to
!a

Array Operators

The scripting language offers own operators to deal with arrays. All operands, of course, have to be of type Array.

The return value of an array operation is an Array.

Unary array operators
OperatorNameExample
+Copy+myArray

Normally arrays are assigned by reference. That means, if you assign array a to array b and change a afterwards, also b is changed. Use the copy operator to avoid this otherwise useful feature.

Example 1:

_arrayA = [1,2];
_arrayB = _arrayA;
_arrayA set [0,5];

_arrayA => [5,2]
_arrayB => [5,2]

Example 2:

_arrayA = [1,2];
_arrayB = +_arrayA;
_arrayA set [0,5];

_arrayA => [5,2]
_arrayB => [1,2]
Binary array operators
OperatorNameExample
+ConcatenationmyArray1 + myArray2
-RemovalmyArray1 - myArray2
  • + adds the second operand on the end of the first operand
  • - removes all elements of the second operand from the first operand

Example 1:

_arrayA = [1,2];
_arrayB = [3,2,4];

_arrayC = _arrayA + _arrayB;

_arrayC => [1,2,3,2,4]

Example 2:

_arrayA = [1,2,3,2,4];
_arrayB = [2,3];

_arrayC = _arrayA - _arrayB;

_arrayC => [1,4]

String Operators

The scripting language offers one single string operator to concatenate strings. Both operands must be String.

The return value of a string operation is a String.

Binary string operators
OperatorNameExample
+ConcatenationmyString1 + myString2
  • + adds the second operand on the end of the first operand

Example:

_stringA = "Hello ";
_stringB = "World!";

_stringC = _stringA + _stringB;

_stringC => "Hello World!"