# Operators and expressions

> Use arithmetic, comparisons, Boolean expressions, collection operators, and custom operators.

Canonical: https://alice-lang.org/documentation/language/operators/

[Documentation index](https://alice-lang.org/documentation/llms.txt) · [Complete book](https://alice-lang.org/llms-full.txt)

## Operator families

| Family      | Operators                               |
| ----------- | --------------------------------------- |
| Arithmetic  | `+`, `-`, `*`, `/`, `%`, `**`           |
| Comparison  | `==`, `!=`, `<`, `<=`, `>`, `>=`, `<=>` |
| Boolean     | `&&`, `\|\|`, `!`, `and`, `or`, `not`   |
| Assignment  | `=`, `+=`, `-=`, `*=`, `/=`             |
| Conditional | `condition ? yes : no`                  |
| Regex       | `=~`, `!~`                              |
| Append      | `<<` on supported lists and builders    |

Multiplication and division bind more tightly than addition and subtraction. Parenthesize mixed expressions when intent could be unclear, especially when mixing assignment, Boolean operators, and method calls.

## Arithmetic

```alice
val price = 12
quantity = 2
quantity += 1
puts price * quantity
puts 7 / 2
puts 7.0 / 2.0
```

Expected output:

```text
36
3
3.5
```

[Edit and run in the Alice playground](https://playground.alice-lang.org/#code=u.dmFsIHByaWNlID0gMTIKcXVhbnRpdHkgPSAyCnF1YW50aXR5ICs9IDEKcHV0cyBwcmljZSAqIHF1YW50aXR5CnB1dHMgNyAvIDIKcHV0cyA3LjAgLyAyLjA)

With integers, `7 / 2` yields `3`. With floating-point operands, `7.0 / 2.0` yields `3.5`. `%` computes a remainder, and `**` exponentiation. `-x` negates a value.

Floating-point operations use .NET numeric types. Fused multiply-add and parallel reductions can affect rounding; see [performance](https://alice-lang.org/documentation/guides/performance.md#floating-point-results).

## Comparisons and conditions

`==` asks for equality according to the type. Records supply value equality. `<=>` is a three-way comparison: negative, zero, or positive. It is the method required by the [Comparable mixin](https://alice-lang.org/documentation/language/modules.md#comparable).

`&&` and `||` short-circuit. Put checks before dependent work, for example checking bounds before indexing. Use explicit Boolean conditions for code that crosses .NET boundaries; do not assume all Ruby truthiness idioms transfer to every typed expression.

## Collection and string operators

| Expression         | Result                                              |
| ------------------ | --------------------------------------------------- |
| `a + b` for arrays | Concatenation                                       |
| `a - b`            | Elements remaining after subtraction                |
| `a & b`            | Intersection                                        |
| `a \| b`           | Union                                               |
| `list << value`    | Append; chainable for supported mutable collections |
| `"ab" * 3`         | Repeated string                                     |
| `"%05d" % [42]`    | Formatted string                                    |

These operations can allocate new collections or strings. Use iterators and builders where appropriate rather than assuming every compact expression is allocation-free.

## Define an operator

A user type can define operators with `def`, such as `def +(other : Vec) : Vec`. Match the signature and result to the meaning of the operation. For ordering, implement `<=>` and `include Comparable` to derive the supported comparison operations consistently.

## Continue reading

- Previous: [Types and nil safety](https://alice-lang.org/documentation/language/types.md)
- Next: [Conditions and loops](https://alice-lang.org/documentation/language/control-flow.md)
