Scripting:Operators

From STNE Wiki

(Difference between revisions)
Jump to: navigation, search
(Arithmetic operators)
 
(20 intermediate revisions not shown)
Line 1: Line 1:
 +
[[Category:Scripting]]
 +
{{ScriptingMenu}}
 +
 +
== Table ==
 +
In this section, a and b represent either literal values, object references or expressions evaluating to the appropriate type.
 +
 +
 +
=== String operators ===
 +
{| cellpadding="3"
{| cellpadding="3"
-
! Operator !! Usage !! Returns
+
! Operator name !! Syntax
|-
|-
-
| colspan="3" | String operators
+
| Concatenation || a & b
 +
|}
 +
 
 +
 
 +
=== Comparison operators ===
 +
 
 +
{| cellpadding="3"
 +
! Operator name !! Syntax
|-
|-
-
| & || String1 & String2 || The concatenatenation of String1 and String 2
+
| Equals                  || a = b
|-
|-
-
| colspan="3" | Arithmetic operators
+
| References equal        || a Is b
|-
|-
-
| + || Number1 + Number2 || Number1 added to Number2
+
| Not equal to            || a <> b
 +
|-
 +
| Greater than            || a > b
 +
|-
 +
| Less than                || a < b
 +
|-
 +
| Greater than or equal to || a >= b
 +
|-
 +
| Less than or equal to    || a <= b
 +
|}
 +
 
 +
 
 +
=== Arithmetic operators ===
 +
 
 +
{| cellpadding="3"
 +
! Operator name !! Syntax
 +
|-
 +
| Assignment    || a = b
 +
|-
 +
| Addition      || a + b
 +
|-
 +
| Subtraction    || a - b
 +
|-
 +
| Multiplication || a * b
 +
|-
 +
| Division      || a / b
 +
|-
 +
| Post Increment      || a++
 +
|-
 +
| Pre Increment      || ++a
 +
|-
 +
| Post Decrement      || a--
 +
|-
 +
| Pre Decrement      || --a
 +
|}
 +
 
 +
=== Logical operators ===
 +
 
 +
{| cellpadding="3"
 +
! Operator name !! Syntax
 +
|-
 +
| Logical NOT    || NOT a
 +
|-
 +
| Logical AND    || a AND b
 +
|-
 +
| Logical OR    || a OR b
 +
|}
 +
 
 +
 
 +
=== Other operators ===
 +
 
 +
{| cellpadding="3"
 +
! Operator name !! Syntax
|-
|-
-
| - || Number1 - Number2 || Number2 subtracted from Number1
+
| Function call      || a()
|-
|-
-
| * || Number1 * Number2 || Number1 multiplied by Number2
+
| Function reference || AddressOf a
|-
|-
-
| / || Number1 / Number2 || Number1 divided by Number2
+
| Member (b of a)    || a.b
|}
|}
 +
 +
 +
== Precedence ==
 +
 +
The exact operator precedence is unknown. However, the following order of precedence seems to apply:
 +
 +
* Member, Function call
 +
* Increment, Decrement
 +
* Multiplication
 +
* Addition, Subtraction
 +
* Comparison
 +
* Logical NOT
 +
* Logical OR, Logical AND
 +
* Assignment
 +
 +
Operators with the same precedence seem to be applied from left to right. Keep in mind that the logical AND and OR are lazy. That is, if the left argument already dictates the outcome, the right argument is not evaluated. This is mainly important for arguments with side effects, such as function calls or the increment/decrement operators. It can also prevent referencing a NULL reference.
 +
 +
== Notes ==
 +
 +
 +
=== Assignment operator v.s. equals operator ===
 +
 +
The {{code | assignment}} operator and the {{code | equals}} operator use the same symbol. It depends on the context which operator is used. The {{code | assignment}} operator is used when a line of code follows the syntax {{code | Identifier {{=}} Value;}}. In all other cases, the {{code | equals}} operator is used.
 +
 +
 +
=== Increment and decrement operators ===
 +
 +
There are two versions of the increment and decrement operators, namely the suffix and the prefix versions. The suffix versions increment or decrements the value hold by the identifier and then returns the old value. The prefix versions increment or decrement the value and return the result.
 +
 +
For example:
 +
Var i As Integer = 5;
 +
WriteLine(i++); // Writes 5
 +
WriteLine(i);  // Writes 6
 +
WriteLine(i--)  // Writes 6
 +
WriteLine(i);  // Writes 5
 +
 +
i = 5;
 +
WriteLine(++i); // Writes 6
 +
WriteLine(i);  // Writes 6
 +
WriteLine(--i); // Writes 5
 +
WriteLine(i);  // Writes 5
 +
 +
 +
=== References equal ===
 +
 +
The {{code | References equal}} operator returns true if both identifiers hold the same reference (or: point to the same object).
 +
 +
For example:
 +
  Var i As String = "a";
 +
  Var j As String = "a";
 +
  WriteLine(i Is j); // false
 +
 +
  i = j;
 +
  WriteLine(i Is j); // true
 +
 +
For types defined as Struct in the Object Explorer, this operator will never return true. When assigning one struct variable to another, the value is copied instead of the reference.
 +
 +
 +
=== AddressOf ===
 +
 +
The {{code | AddressOf}} operator actually returns a CDelegate instance.
 +
 +
An example:
 +
Var Delegate As CDelegate = AddressOf MyFunction;
 +
 +
Function MyFunction() {
 +
  WriteLine("Hello universe!");
 +
}
 +
 +
Delegate.Invoke();  // Writes "Hello universe!";

Latest revision as of 17:04, 7 November 2011


Main | Syntax | Operators | Interfaces | FAQ | Contents | API Reference | Index


Contents

Table

In this section, a and b represent either literal values, object references or expressions evaluating to the appropriate type.


String operators

Operator name Syntax
Concatenation a & b


Comparison operators

Operator name Syntax
Equals a = b
References equal a Is b
Not equal to a <> b
Greater than a > b
Less than a < b
Greater than or equal to a >= b
Less than or equal to a <= b


Arithmetic operators

Operator name Syntax
Assignment a = b
Addition a + b
Subtraction a - b
Multiplication a * b
Division a / b
Post Increment a++
Pre Increment ++a
Post Decrement a--
Pre Decrement --a

Logical operators

Operator name Syntax
Logical NOT NOT a
Logical AND a AND b
Logical OR a OR b


Other operators

Operator name Syntax
Function call a()
Function reference AddressOf a
Member (b of a) a.b


Precedence

The exact operator precedence is unknown. However, the following order of precedence seems to apply:

  • Member, Function call
  • Increment, Decrement
  • Multiplication
  • Addition, Subtraction
  • Comparison
  • Logical NOT
  • Logical OR, Logical AND
  • Assignment

Operators with the same precedence seem to be applied from left to right. Keep in mind that the logical AND and OR are lazy. That is, if the left argument already dictates the outcome, the right argument is not evaluated. This is mainly important for arguments with side effects, such as function calls or the increment/decrement operators. It can also prevent referencing a NULL reference.

Notes

Assignment operator v.s. equals operator

The assignment operator and the equals operator use the same symbol. It depends on the context which operator is used. The assignment operator is used when a line of code follows the syntax Identifier = Value;. In all other cases, the equals operator is used.


Increment and decrement operators

There are two versions of the increment and decrement operators, namely the suffix and the prefix versions. The suffix versions increment or decrements the value hold by the identifier and then returns the old value. The prefix versions increment or decrement the value and return the result.

For example:

Var i As Integer = 5;
WriteLine(i++); // Writes 5
WriteLine(i);   // Writes 6
WriteLine(i--)  // Writes 6
WriteLine(i);   // Writes 5

i = 5;
WriteLine(++i); // Writes 6
WriteLine(i);   // Writes 6
WriteLine(--i); // Writes 5
WriteLine(i);   // Writes 5


References equal

The References equal operator returns true if both identifiers hold the same reference (or: point to the same object).

For example:

 Var i As String = "a";
 Var j As String = "a";
 WriteLine(i Is j); // false

 i = j;
 WriteLine(i Is j); // true

For types defined as Struct in the Object Explorer, this operator will never return true. When assigning one struct variable to another, the value is copied instead of the reference.


AddressOf

The AddressOf operator actually returns a CDelegate instance.

An example:

Var Delegate As CDelegate = AddressOf MyFunction;

Function MyFunction() {
  WriteLine("Hello universe!");
}

Delegate.Invoke();  // Writes "Hello universe!";
Personal tools