Operators in CTRL
Arithmetic operators
The binary arithmetic operators are +, -, *, '/ and the modulus operator %, which returns the remainder after an integer division. The % operator cannot be used on float values. Only the + and - operators are allowed for time values.
Example
time t1, t2;
//....
// The value of t1 is decreased by one
hour
t2 += 1.003
// The value of
t2 is increased by 1003 milliseconds
Increment and decrement operator
The increment operator is ++, placed either before or after the variable, and the decrement operator is a preceding or following --. These operators, which can only be used on variables, increase or decrease the variable value by one. If this type of operator is placed in front of a variable its value is increased (decreased) before it is used. If an increment (decrement) operator is placed after a variable then its value is only increased (decreased) after it has been used.
Example
main()
{
int a, b, d;
a = 3;
b = 5;
d=a*b++;
DebugN(d); //Result for ++b = 18, for b++ = 15
DebugN(b); //Result always = 6
}
Conditional operator
expr1 ? expr2 : expr3
Depending on expr1, either expr2 or expr3 is evaluated and returned as a result. This is similar to the control structures if ... else
Example
main()
{
int a = 3;
string answer;
answer = (a == 3) ? "a equals 3" : "a is not equal 3";
DebugN(answer);
}
expr2 and expr3 must return a value of the same type, otherwise unexpected results can occur.
Relational and logical operators
The relational operators are >, >=, <, <=, ==, !=. The logical operators are && for logical AND and || for logical OR.
The ! character is the negations operator (NOT).
Bit-wise operators and shift operators
The bit-wise operators are & for bit-wise AND, | for bit-wise OR and ^ for an XOR operation (bit-wise exclusive OR).
The difference between XOR and normal OR is that the result of an XOR operation with 2 bits (both bits are 1) is 0. The result of normal OR | is 1.
a ^ b
a b Result
---------------
0 0 0
0 1 1
1 0 1
1 1 0
~ is the negations operator (NOT, bit complement).
The shift operators << and >> can be used to shift to left or right respectively all bits in the binary representation of the left-hand operand by that number of bit positions specified in the right-hand operand. Zeros are added to the end. The right-hand operand must be positive.
Example
1 << 2 converts the binary expression 1 to the binary expression 100.
Note that if you assign values, which use the highest bit, to a bit32 variable you have to use the suffix 'u' (unsigned) or 'l' (log) , for example, bit32 b = 0x8FFFFFFFU;
If the highest bit of a variable is set and you want to execute the following operation:
bit32 Test;
Test = Test >> 16;
you can execute it as follows:
main()
{
bit32 Test;
Test=0x8FFFFFFFU;
DebugN("Firstly",Test);
Test=Test>>16u;
DebugN("Secondly",Test);
}
/* By adding a "u" (for unsigned) at Test = Test >>16u the 32bit word is moved by 16 bits to the right and the first 16 bits are filled with 0 (nulls) */
Assignment operators
In the expression
-
i = i + 2
the variable on the left-hand side is repeated immediately at the beginning of the right-hand side. This can be written in a more compact form as
-
i += 2
The operator += is an assignment operator. Other assignment operators are -=, *=, /= and %=. There are also assignment operators for bit-wise operators. Thus a = a& b could also be written as a &= b. The operators |= and ^= also exist. Shift assignment operators can be implemented with <<= and >>=.
Multiple assignments
A multiple assignment is not supported by CONTROL, e.g.:
RECTANGLE1.visible = RECTANGLE2.visible = RECTANGLE3.visible =
bVisible;
In this example only RECTANGLE3.visible
gets the value of
thebVisible
variable.RECTANGLE1.visible
andRECTANGLE2.visible
will
not be affected.
Please use the function setMultiValue() instead in order to assign a variable to any number of object attributes:
main()
{
setMultiValue("RECTANGLE1", "visible", bVisible, "RECTANGLE2", "visible", bVisible, "RECTANGLE3", "visible", bVisible);
}