Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/expressions/comparison-and-logical.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ Operate on `bool` values:
| `\|\|` | `a \|\| b` |
| `!` | `!a` |

## Short-Circuit Evaluation

`&&` and `||` short-circuit: the right operand is evaluated only when the left
operand does not already determine the result. `a && b` skips `b` when `a` is
`false`; `a || b` skips `b` when `a` is `true`.

This makes guard expressions safe — the check on the left protects the
operation on the right:

```inference
pub fn is_big_ratio(y: i32, x: i32) -> bool {
// `y / x` runs only when `x != 0` is true,
// so this can never divide by zero.
return x != 0 && y / x > 1;
}
```

The same pattern guards array accesses: `i < len && arr[i] > 0` never reads
out of bounds. If you need both operands evaluated unconditionally, use the
[bitwise operators](./bitwise.md) `&` and `|` — they do not short-circuit.

## Example

```inference
Expand Down
Loading