---
title: "8.1 Every form of if"
url: https://una-lang.org/en/learn/choices/every-form-of-if/
release: v0.4.0
edition: "0.2"
---

# 8.1 Every form of if

## 8.1.1 What an `if` without `else` is

[Section 3.4](https://una-lang.org/en/learn/text-and-conditions/choosing-between-two-values-if/#s3-4) taught `if` as an expression, a choice between two values: on one line as `if c then a else b`, with `else if` for more cases, in parentheses inside a larger expression, and over several lines with a block in each branch. Every one of those forms gives a value, so every one has an `else`.

One form is left: an `if` that gives no value, and only does something when its condition holds. Think of a note on a packing table: "if the order is fragile, wrap each unit". When the order is not fragile, there is nothing to do, and no "otherwise" to write. Such an `if` is a **statement**: a line of a block that does something, as a call to `@console.print` does, instead of giving a value. It is the last form of `if`, and this section teaches it, then how to choose between all the forms.

## 8.1.2 Why a program needs an `if` that gives no value

The forms of section 3.4 always produce a value, and a value must exist for both answers of the condition. Some steps have no value at all: a line to print, a failure to raise. With only those forms, a function that prints an extra note for bulk orders would have to print something in the `else` branch too, or build a text that is sometimes empty. And a function that refuses a bad quantity would have to put the rest of its body inside an `else`, one level deeper for each check.

An `if` without `else` says exactly what the rule says: when the condition holds, run these lines; otherwise, go on.

## 8.1.3 How to write an `if` without `else`

The step of this section has one function for each use of the form, and its `main` prints what each does:

```una include="steps/01-if-forms/src/choices.una#packing_notes" title="src/choices.una"
--- The packing notes for an order: each `if` without `else` prints its line only when its
--- condition holds.
packing_notes(units: Int, fragile: Bool) -> Unit
  uses @console: Console[print]
=
  @console.print("{units} units")
  if units > 10
    @console.print("  bulk: use a pallet")
  if fragile
    @console.print("  fragile: wrap each unit")
```

The form has three parts:

1. **`if units > 10`.** The keyword `if` and the condition, alone on the line, with no `then`, as in the block form of section 3.4.
2. **The lines under it, indented two spaces.** They run, in order, only when the condition is `true`. Here one line prints a note.
3. **No `else`.** When the condition is `false`, the lines are skipped, and the function goes on with the line after them.

`packing_notes` has two such `if`s, one after the other, and each one decides on its own. `main` calls it for 4 units that are not fragile, then for 24 that are:

```text include="steps/01-if-forms/expected/output.txt#L2-L5"
4 units
24 units
  bulk: use a pallet
  fragile: wrap each unit
```

For 4 units, both conditions are `false`, and only the first line prints. For 24 fragile units, both hold, and both notes print.

### 8.1.3.1 A branch of several lines

The lines under the `if` are a block, as a branch of section 3.4 is: they may name values with `let`, and hold another `if`:

```una include="steps/01-if-forms/src/choices.una#stock_note" title="src/choices.una"
--- A stock note: a branch of several lines, with a name of its own and an `if` inside it.
stock_note(sku: Text, on_hand: Int) -> Unit
  uses @console: Console[print]
=
  if on_hand < 5
    let missing = 5 - on_hand
    @console.print("{sku}: order {missing} more")
    if on_hand == 0
      @console.print("{sku}: hide it from the shop")
  @console.print("{sku}: counted")
```

```text include="steps/01-if-forms/expected/output.txt#L6-L9"
LAMP-01: counted
RUG-07: order 5 more
RUG-07: hide it from the shop
RUG-07: counted
```

The lamp has 12 units, so the whole branch is skipped, and only "counted" prints. The rug has none: the branch names `missing`, prints the order, then its inner `if` holds too, and prints the second note. `missing` exists only inside the branch.

### 8.1.3.2 Failing when a condition holds

A branch may also end the function with `fail`, as in [section 7.2](https://una-lang.org/en/learn/failure/declaring-a-failure-fails-and-fail/#s7-2). Several such `if`s in a row check a value one rule at a time, and the last line gives the value once every rule has passed:

```una include="steps/01-if-forms/src/choices.una#quantity" title="src/choices.una"
--- A quantity from 1 to 99. Each `if` without `else` fails when its condition holds, and the
--- last line gives the value.
quantity(units: Int) -> Int
  fails QuantityError
=
  if units < 1
    fail TooFew(units)
  if units > 99
    fail TooMany(units)
  units
```

```text include="steps/01-if-forms/expected/output.txt#L10"
12, 0
```

12 passes both checks, so `quantity(12)` gives 12. 140 is above 99, so the second `if` fails with `TooMany`, and `main` recovers with 0. Written this way, each rule is one line, and the value stays at the left margin, not inside nested `else` blocks. [Section 8.3](https://una-lang.org/en/learn/choices/what-a-branch-proves-guards/#s8-3) shows what the verifier knows after such a check.

## 8.1.4 How the form behaves

### 8.1.4.1 It gives no value

An `if` without `else` is a statement, so it can stand only where a statement can: on a line of a block, before other lines or as the last one. It cannot be the value of a `let`, the body of a one-line function or an argument, since there would be no value when the condition is `false`. When it is the last line of a block, as in `packing_notes`, the block gives `Unit`, nothing useful, which is what a function that only prints returns.

### 8.1.4.2 It has no `else if`

The form has no `else` of any kind, so it has no `else if` either. A chain of `else if` always ends with `else`, and is a choice of section 3.4 that gives a value. When several conditions should each decide on their own, write several `if`s, one after the other, as `packing_notes` does.

### 8.1.4.3 An `if` with `else` is the last line, or a value that is kept

The forms with `else` give a value, so Una wants that value used. Over several lines, such an `if` stands only as the last line of its block, whose value it then gives, after `let`, or as the value of an assignment to a `let edit` name ([section 9.2](https://una-lang.org/en/learn/loops/names-that-change-let-edit/#s9-2)), as in `note = if …`. Add an `else` to the first `if` of `packing_notes`, which has a line after it:

```diff include="steps/01-if-forms..steps/03-else-mid-block:src/choices.una" title="src/choices.una"
--- a/src/choices.una
+++ b/src/choices.una
@@ -14,6 +14,8 @@
   @console.print("{units} units")
   if units > 10
     @console.print("  bulk: use a pallet")
+  else
+    @console.print("  standard: use a box")
   if fragile
     @console.print("  fragile: wrap each unit")
 
```

```text include="steps/03-else-mid-block/expected/rendered.txt#L1-L6"
[U0101] Error: an `if` with blocks must be the last line of its block or the value of a `let` or of an assignment
    ╭─[ src/choices.una:15:3 ]
    │
 15 │   if units > 10
    │   ─┬
    │    ╰── an `if` with blocks must be the last line of its block or the value of a `let` or of an assignment
```

The marks point at the `if`. Here both branches print, so the `if` gives no useful value, but its form is the one that does. To print one note or the other and then go on, write two `if`s without `else`, one with the condition and one with its opposite, `if units <= 10`.

### 8.1.4.4 `then` belongs to the one-line form

`then` comes only in the one-line form, where the value follows it on the same line. A block after `then`, as some other languages write it, does not parse:

```diff include="steps/01-if-forms..steps/02-then-at-end:src/choices.una" title="src/choices.una"
--- a/src/choices.una
+++ b/src/choices.una
@@ -4,7 +4,12 @@
 kind QuantityError = TooFew(units: Int) | TooMany(units: Int)
 
 --- Whether a parcel counts as heavy: an `if` on one line, as section 3.4 wrote it.
-weight_class(grams: Int) -> Text = if grams > 2_000 then "heavy" else "light"
+weight_class(grams: Int) -> Text
+=
+  if grams > 2_000 then
+    "heavy"
+  else
+    "light"
 
 --- The packing notes for an order: each `if` without `else` prints its line only when its
 --- condition holds.
```

```text include="steps/02-then-at-end/expected/rendered.txt#L1-L8"
[U0101] Error: unexpected end of line, expected an expression
   ╭─[ src/choices.una:9:24 ]
   │
 9 │   if grams > 2_000 then
   │                        ┬
   │                        ╰── unexpected end of line, expected an expression
   │
   │ Help: repair 1: write the `if` with blocks: `if` and its condition, the indented branch, `else`, then the indented branch; edit src/choices.una:9:1..13:1 to "  if grams > 2_000\n    \"heavy\"\n  else\n    \"light\"\n"
```

The marks point at the end of the line, where a value was expected after `then`. The `Help` line gives the exact edit to the block form, without `then`, and the last line of the diagnostic, which this excerpt leaves out, says that `una fix .` can apply it.

## 8.1.5 Choosing the form of `if`

:::decide
**Which to use:**

| You want to… | Use |
| --- | --- |
| Choose between two short values | `if c then a else b`, on one line |
| Choose among several cases, by conditions tried in order | `else if`, ending with `else` |
| Name values on the way to a branch's answer | `if` over several lines, with a block in each branch |
| Use a choice as one part of a larger expression | `(if c then a else b)`, in parentheses |
| Do something only when a condition holds, such as print a note | `if` without `else` |
| Refuse a value that breaks a rule, then carry on | `if` without `else`, whose branch is `fail` |
| Choose by the variant of a sum, or by many numbers or texts | `when` ([section 8.2](https://una-lang.org/en/learn/choices/when-in-full/#s8-2)) |
:::

Choose the one-line form while both branches are short, and switch to several lines when a branch needs a name or the line stops reading at a glance. `una fmt .` keeps whichever form you wrote: both are canonical.

## 8.1.6 Check yourself: an `if` without `else`

`carton_labels` prints the labels of a carton:

```una include="steps/01-if-forms/src/choices.una#carton_labels" title="src/choices.una"
--- The labels on a carton. Check yourself: what does `main` print for 3 units, and for 30?
carton_labels(units: Int) -> Unit
  uses @console: Console[print]
=
  if units > 20
    @console.print("  heavy: lift with two people")
  if units > 5
    @console.print("  stack at most three high")
  if units < 10
    @console.print("  fits on a shelf")
  @console.print("  this side up")
```

`main` prints a line `carton of 3`, calls `carton_labels(3)`, then does the same for 30. What does it print?

:::answer Show the answer
```text include="steps/01-if-forms/expected/output.txt#L11-L17"
carton of 3
  fits on a shelf
  this side up
carton of 30
  heavy: lift with two people
  stack at most three high
  this side up
```

Each `if` decides on its own. For 3 units only `units < 10` holds. For 30, both `units > 20` and `units > 5` hold, so two labels print; `units < 10` does not. The last line has no `if`, so it prints every time.
:::
