# Operators
DWScript supports standard Pascal operators for arithmetic, logic, and comparisons.
## Assignment & Arithmetic
```pascal
var a := 10;
var b := 20;
PrintLn(a + b);
PrintLn(10 div 3);
PrintLn(10 mod 3);
PrintLn(10 / 3);
// OUTPUT
// 30
// 3
// 1
// 3.33333333333333
```
## Arithmetic Operators
| Operator | Operation | Example |
| :--- | :--- | :--- |
| `+` | Addition (or Unary +) | `1 + 2` (3) |
| `-` | Subtraction (or Unary -) | `5 - 3` (2) |
| `*` | Multiplication | `2 * 4` (8) |
| `/` | Division (Float) | `10 / 2` (5.0) |
| `div` | Integer Division | `10 div 3` (3) |
| `mod` | Modulus (Remainder) | `10 mod 3` (1) |
## Logical Operators
Used with Boolean values. Logical operators in DWScript are **short-circuiting**, meaning the second operand is only evaluated if the first one doesn't determine the final result.
| Operator | Description |
| :--- | :--- |
| `not` | Negation (True becomes False) |
| `and` | Logical AND (True if both are True) |
| `or` | Logical OR (True if either is True) |
| `xor` | Logical XOR (True if only one is True) |
| `implies` | Material Implication (False only if True implies False) |
## Comparison Operators
| Operator | Description |
| :--- | :--- |
| `=` | Equal to |
| `<>` | Not equal to |
| `<` | Less than |
| `>` | Greater than |
| `<=` | Less than or equal to |
| `>=` | Greater than or equal to |
| `is` | Type compatibility check (for classes and interfaces) |
:::info
#### Reference Comparison
For **Dynamic Arrays** and **Objects**, the `=` and `<>` operators perform a **reference comparison** (checking if both point to the same memory instance) rather than comparing their contents.
:::
| Operator | Description |
| :--- | :--- |
| `as` | Checked type cast |
| `implements` | Checks if an object/class implements an interface |
| `in` | Set membership, array inclusion, or substring check |
| `not in` | Negation of `in` (True if NOT a member) |
### Interface Implementation
The `implements` operator is used to check if a class or object supports a specific interface, which is useful for conditional casting or logic.
```pascal
type ILogger = interface end;
type TMyLogger = class(TObject, ILogger) end;
if TMyLogger implements ILogger then
PrintLn('Implements interface');
// OUTPUT
// Implements interface
```
## Collection & String Inclusion
In DWScript, the `in` operator is highly versatile and works with sets, arrays, and strings.
### Set Inclusion & Ranges
You can check if a value belongs to a set of values or a specific range.
```pascal
var c := 'k';
if c in ['a'..'z', '0'..'9', '_'] then
PrintLn('Is Alphanumeric');
var x := 5;
if x in [1..10, 20..30] then
PrintLn('Is in range');
var s := 'Grape';
if s in ['Apple'..'Orange'] then
PrintLn('Between Apple and Orange');
// OUTPUT
// Is Alphanumeric
// Is in range
// Between Apple and Orange
```
### Array Inclusion
You can check if an element exists within an array. This works for simple types, records, and objects.
```pascal
var colors := ['Red', 'Green', 'Blue'];
if 'Green' in colors then
PrintLn('Found Green');
if 'Yellow' not in colors then
PrintLn('Yellow is missing');
// OUTPUT
// Found Green
// Yellow is missing
```
### String Inclusion
You can check if a string contains another string using the `in` operator or the `.Contains()` method. This is more readable and idiomatic than using `Pos() > 0`.
```pascal
var msg := 'Hello World';
// Using 'in' operator
if 'World' in msg then
PrintLn('Found using in');
// Using .Contains method
if msg.Contains('Hello') then
PrintLn('Found using .Contains');
// Legacy style (not recommended)
if Pos('Hello', msg) > 0 then
PrintLn('Found using Pos');
// OUTPUT
// Found using in
// Found using .Contains
// Found using Pos
```
## Operator Precedence
Higher priority operators are evaluated first. When operators have the same priority, they are evaluated from left to right.
1. `not`, Unary `-`, Unary `+`
2. `*`, `/`, `div`, `mod`, `and`, `shl`, `shr`, `sar`
3. `??` (Coalesce)
4. `+`, `-`, `or`, `xor`
5. `=`, `==`, `<>`, `!=`, `<`, `>`, `<=`, `>=`, `in`, `not in`, `is`, `as`, `implies`
6. `if then else` (Ternary)
## Custom Operators
You can define custom behavior for operators when working with your own types.
* **[Class Operators](/lang/oop/oop)**
Define operators (like `+=`, `in`) for classes.
* **[Record Operators](/lang/basics/records)**
Define global operators for record value types.
:::info
#### Specialized Operators
DWScript also supports several specialized operators for concise expression:
* **[Ternary Operator](/lang/basics/ternary)**: Expression-based `if then else`.
* **[Coalesce Operator](/lang/basics/coalesce)**: Handle default values with `??`.
* **[Bitwise Operators](/lang/basics/bitwise)**: Low-level bit manipulation.
* **[Compound & Aliases](/lang/basics/compound)**: C-style operators like `+=`, `==`, and `===`.
:::
Operators
DWScript supports standard Pascal operators for arithmetic, logic, and comparisons.
Assignment & Arithmetic
var a :=10;var b :=20;
PrintLn(a + b);
PrintLn(10div3);
PrintLn(10mod3);
PrintLn(10/3);
Result
30
3
1
3.33333333333333
Arithmetic Operators
Operator
Operation
Example
+
Addition (or Unary +)
1 + 2 (3)
-
Subtraction (or Unary -)
5 - 3 (2)
*
Multiplication
2 * 4 (8)
/
Division (Float)
10 / 2 (5.0)
div
Integer Division
10 div 3 (3)
mod
Modulus (Remainder)
10 mod 3 (1)
Logical Operators
Used with Boolean values. Logical operators in DWScript are short-circuiting, meaning the second operand is only evaluated if the first one doesn't determine the final result.
Operator
Description
not
Negation (True becomes False)
and
Logical AND (True if both are True)
or
Logical OR (True if either is True)
xor
Logical XOR (True if only one is True)
implies
Material Implication (False only if True implies False)
Comparison Operators
Operator
Description
=
Equal to
<>
Not equal to
<
Less than
>
Greater than
<=
Less than or equal to
>=
Greater than or equal to
is
Type compatibility check (for classes and interfaces)
Reference Comparison
For Dynamic Arrays and Objects, the = and <> operators perform a reference comparison (checking if both point to the same memory instance) rather than comparing their contents.
Operator
Description
as
Checked type cast
implements
Checks if an object/class implements an interface
in
Set membership, array inclusion, or substring check
not in
Negation of in (True if NOT a member)
Interface Implementation
The implements operator is used to check if a class or object supports a specific interface, which is useful for conditional casting or logic.
type ILogger =interfaceend;type TMyLogger =class(TObject, ILogger)end;if TMyLogger implements ILogger then
PrintLn('Implements interface');
Result
Implements interface
Collection & String Inclusion
In DWScript, the in operator is highly versatile and works with sets, arrays, and strings.
Set Inclusion & Ranges
You can check if a value belongs to a set of values or a specific range.
var c :='k';if c in['a'..'z','0'..'9','_']then
PrintLn('Is Alphanumeric');var x :=5;if x in[1..10,20..30]then
PrintLn('Is in range');var s :='Grape';if s in['Apple'..'Orange']then
PrintLn('Between Apple and Orange');
Result
Is Alphanumeric
Is in range
Between Apple and Orange
Array Inclusion
You can check if an element exists within an array. This works for simple types, records, and objects.
var colors :=['Red','Green','Blue'];if'Green'in colors then
PrintLn('Found Green');if'Yellow'notin colors then
PrintLn('Yellow is missing');
Result
Found Green
Yellow is missing
String Inclusion
You can check if a string contains another string using the in operator or the .Contains() method. This is more readable and idiomatic than using Pos() > 0.
var msg :='Hello World';// Using 'in' operatorif'World'in msg then
PrintLn('Found using in');// Using .Contains methodif msg.Contains('Hello')then
PrintLn('Found using .Contains');// Legacy style (not recommended)if Pos('Hello', msg)>0then
PrintLn('Found using Pos');
Result
Found using in
Found using .Contains
Found using Pos
Operator Precedence
Higher priority operators are evaluated first. When operators have the same priority, they are evaluated from left to right.
not, Unary -, Unary +
*, /, div, mod, and, shl, shr, sar
?? (Coalesce)
+, -, or, xor
=, ==, <>, !=, <, >, <=, >=, in, not in, is, as, implies
if then else (Ternary)
Custom Operators
You can define custom behavior for operators when working with your own types.