# Operators

An operator is something that takes one or more values (or expressions) and yields another value (so that the construction itself becomes an expression)

Operators can be grouped according to the number of values they take. Unary operators take only one value, for example `!` (the logical not operator). Binary operators take two values, such as the familiar arithmetical operators `+` (plus) and `-` (minus). The majority of Zaidlang's operators fall into this category.

### Precedence and Associativity <a href="#precedence-and-associativity" id="precedence-and-associativity"></a>

The precedence of an operator specifies how "tightly" it binds two expressions together. For example, in the expression `1 + 5 * 3`, the answer is `16` and not `18` because the multiplication (`*`) operator has a higher precedence than the addition (`+`) operator. Parentheses may be used to force precedence, if necessary. For example, `(1 + 5) * 3` evaluates to `18`.

When operators have equal precedence their associativity decides how the operators are grouped. For example, `=` is left-associative, so `1 - 2 - 3` is grouped as `(1 - 2) - 3` and evaluates to `-4`. `=` on the other hand is right-associative, so `a = b = c` is grouped as `a = (b = c)`.

Use of parentheses, even when not strictly necessary, can often increase readability of the code by making grouping explicit rather than relying on the implicit operator precedence and associativity.

The following table summarizes the operator precedence in Zaidlang, from highest to lowest. Operators in the same box have the same precedence.

<table><thead><tr><th width="142">Precedence</th><th>Operator</th><th>Description</th><th>Associates</th></tr></thead><tbody><tr><td>1</td><td><code>()</code> <code>.</code></td><td>Grouping, Method call</td><td>Left</td></tr><tr><td>2</td><td><code>-</code></td><td>Negate</td><td>Right</td></tr><tr><td>3</td><td><code>*</code> <code>/</code> <code>%</code></td><td>Multiply, Divide, Modulo</td><td>Left</td></tr><tr><td>4</td><td><code>+</code> <code>-</code></td><td>Add, Subtract</td><td>Left</td></tr><tr><td>5</td><td><code>&#x3C;</code> <code>&#x3C;=</code> <code>></code> <code>>=</code></td><td>Comparison</td><td>Left</td></tr><tr><td>6</td><td><code>==</code> <code>!=</code></td><td>Equals, Not equal</td><td>Left</td></tr><tr><td>7</td><td><code>and</code></td><td>Logical and</td><td>Left</td></tr><tr><td>8</td><td><code>or</code></td><td>Logical or</td><td>Left</td></tr><tr><td>9</td><td><code>=</code></td><td>Assignment</td><td>Right</td></tr></tbody></table>

### Arithmetic Operators <a href="#arithmetic-operators" id="arithmetic-operators"></a>

Remember basic arithmetic from school? These work just like those.

<table><thead><tr><th width="120">Example</th><th width="151">Name</th><th>Result</th></tr></thead><tbody><tr><td><code>-a</code></td><td>Negation</td><td>Opposite of <code>a</code>.</td></tr><tr><td><code>a + b</code></td><td>Addition</td><td>Sum of <code>a</code> and <code>b</code>.</td></tr><tr><td><code>a - b</code></td><td>Subtraction</td><td>Difference of <code>a</code> and <code>b</code>.</td></tr><tr><td><code>a * b</code></td><td>Multiplication</td><td>Product of <code>a</code> and <code>b</code>.</td></tr><tr><td><code>a / b</code></td><td>Division</td><td>Quotient of <code>a</code> and <code>b</code>.</td></tr><tr><td><code>a % b</code></td><td>Modulo</td><td>Remainder of <code>a</code> divided by <code>b</code>.</td></tr></tbody></table>

The result of the modulo operator (`%`) has the same sign as the dividend - that is, the result of `a % b` will have the same sign as `a`. For example:

```dart
printftw(5 % 3) // >> 2
printftw(5 % -3) // >> 2
printftw(-5 % 3) // >> -2
printftw(-5 % -3) // >> -2
```

### Assignment Operator <a href="#assignment-operator" id="assignment-operator"></a>

The assignment operator is `=`. This declares and assigns the value of the expression on the right.

```dart
message = "Hello, world!"

printftw(message) // >> Hello, world!
```

### Comparison Operators <a href="#comparison-operators" id="comparison-operators"></a>

Comparison operators, as their name implies, allow you to compare two values.

| Example  | Name                     | Result                                         |
| -------- | ------------------------ | ---------------------------------------------- |
| `a == b` | Equal                    | `true` if `a` is equal to `b`.                 |
| `a != b` | Not equal                | `true` if `a` is not equal to `b`.             |
| `a < b`  | Less than                | `true` if `a` is less than `b`.                |
| `a > b`  | Greater than             | `true` if `a` is greater than `b`.             |
| `a <= b` | Less than or equal to    | `true` if `a` is less than or equal to `b`.    |
| `a >= b` | Greater than or equal to | `true` if `a` is greater than or equal to `b`. |
