---
title: Conditions
canonical_url: https://mew-lang.org/language/control/conditions/
sidecar_url: https://mew-lang.org/language/control/conditions.md
content_hash: sha256:32cf1e948ccf9af9638745c70648ee700bb89422cf3fd63139ac75f7ff59ab38
tokens: 662
uid: language.control.conditions
reading_time_minutes: 2
---

Language
# Conditions

 
`if` runs a block when a condition holds, and `else` says what to do when it does not.

 
```mew
use std;
  
let value = 3;
  
if value == 0 {
    println("zero");
} else if value < 0 {
    println("negative");
} else {
    println("positive");
}
```

 
There are no parentheses around the condition, and the branches are always blocks. There is no single statement form, so a body is written in braces however short it is.

 
## The condition is a `bool`

 
A condition has to be a [bool](https://mew-lang.org/language/primitives/bool.md) and nothing else. Mew has no notion of a truthy value, so a number is not a condition, and neither is a value that might be `null`.

 
```mew
let count = 1;
  
if count {
    let unreachable = 0;
}
```

 
Compare it instead, and the comparison is the `bool`.

 
```mew
use std;
  
let count = 1;
  
if count != 0 {
    println("not zero");
}
```

 
The operators that produce one are the comparisons and the logical operators, and `&&` and `||` [short circuit](https://mew-lang.org/language/primitives/bool.md#short-circuiting), so a cheap test can guard an expensive one.

 
```mew
use std;
  
pub fn at(values: i32[], index: i32) -> i32 {
    if index >= 0 && index < values.count {
        return values[index];
    }
  
    return -1;
}
  
println($"{at(new i32[] { 1, 2, 3 }, 1)}");
println($"{at(new i32[] { 1, 2, 3 }, 9)}");
```

 
## An `if` produces no value

 
`if` is a statement. It does not produce a value, so it cannot be assigned from, and Mew has no conditional expression to reach for instead.

 
Write the branch as an assignment to a `mut` local.

 
```mew
use std;
  
let value = 3;
let mut label = "";
  
if value < 0 {
    label = "negative";
} else {
    label = "positive";
}
  
println(label);
```

 
Or return from each branch, which is usually clearer.

 
```mew
use std;
  
pub fn label(value: i32) -> string {
    if value < 0 {
        return "negative";
    }
  
    return "positive";
}
  
println(label(3));
```

 
[match](https://mew-lang.org/language/unions.md#matching-as-a-value) is the one conditional that does read as a value, and it works on a [union](https://mew-lang.org/language/unions.md) rather than on a `bool`.

 
> [!NOTE]
> An `if` runs a statement rather than producing a value. To choose between two values, the [conditional operator](https://mew-lang.org/language/operators.md#conditional) is what produces one: `let name = count == 1 ? "one" : "many";`.

 
[Previous
                
                Control Flow](/language/control/)[Next
                    
                Loops](https://mew-lang.org/language/control/loops.md)