---
title: "Conditional Statements and Expressions"
canonical: "https://docs.magma-maths.org/v2.30-1/MagmaLanguage/StatementsAndExpressions/conditional.html"
source: "https://docs.magma-maths.org/v2.30-1/MagmaLanguage/StatementsAndExpressions/conditional.md"
part: "The Magma Language"
chapter: "Statements and Expressions"
breadcrumbs:
  - {"title": "Magma Handbook", "source": "https://docs.magma-maths.org/v2.30-1/index.md"}
  - {"title": "The Magma Language", "source": "https://docs.magma-maths.org/v2.30-1/MagmaLanguage/index-magma-language.md"}
  - {"title": "Statements and Expressions", "source": "https://docs.magma-maths.org/v2.30-1/MagmaLanguage/StatementsAndExpressions/index-statements-and-expressions.md"}
prev: {"title": "The where … is Construction", "source": "https://docs.magma-maths.org/v2.30-1/MagmaLanguage/StatementsAndExpressions/where-is.md"}
next: {"title": "Error Handling Statements", "source": "https://docs.magma-maths.org/v2.30-1/MagmaLanguage/StatementsAndExpressions/error-handling.md"}
intrinsics:
  - {"name": "if", "signature": "if Boolean expression then statements₁ else statements₂ end if;"}
  - {"name": "if", "signature": "if Boolean expression then statements end if;"}
  - {"name": "elif", "signature": "if Boolean expression₁ then statements₁ elif Boolean expression₂ then statements₂ else statements₃ end if;"}
  - {"name": "select", "signature": "Boolean expression select expr₁ else expr₂"}
  - {"name": "case", "signature": "case expr : when exprᵢ : statements end case"}
  - {"name": "case", "signature": "case< expr | expr_left,1 : expr_right,1, ..., expr_left,n : expr_right,n, default : expr_def>"}
examples:
  - "ex-6d1681"
  - "ex-bbc380"
  - "ex-62b01f"
---

# Conditional Statements and Expressions

The conditional statement has the usual form `if ... then ... else ... end if;`. It has several variants. Within the statement, a special prompt will appear, indicating that the statement has yet to be closed. Conditional statements may be nested. The conditional expression, `select ... else`, is used for in-line conditionals.

## The Simple Conditional Statement

<a id="literal-literal-if-if-boolean-expression-then-br-indent-statements1-br-else-br-indent-statements2-br-end-if"></a>
### `if Boolean expression then statements₁ else statements₂ end if;`

<a id="literal-literal-if-if-boolean-expression-then-br-indent-statements-br-end-if"></a>
### `if Boolean expression then statements end if;`

The standard conditional statement: the value of the Boolean expression is evaluated. If the result is `true` then the first block of statements is executed, while if the result is `false` the second block of statements is executed. If no action is desired in the latter case, the construction may be abbreviated to the second form above.

<a id="literal-literal-elif-if-boolean-expression1-then-br-indent-statements1-br-elif-boolean-expression2-then-br-indent-statements2-br-else-br-indent-statements3-br-end-if"></a>
### `if Boolean expression₁ then statements₁ elif Boolean expression₂ then statements₂ else statements₃ end if;`

Since nested conditions occur frequently, `elif` provides a convenient abbreviation for `else if`, which also restricts the ‘level’:

```magma
if Boolean expression then
   statements₁
elif Boolean expression₂ then
   statements₂
else
   statements₃
end if;
```

is equivalent to

```magma
if Boolean expression₁ then
    statements₁
else
    if Boolean expression₂ then
       statements₂
    else
       statements₃
    end if;
end if;
```

<a id="example-ex-6d1681"></a>
### `Example: A Simple Conditional Statement (ex-6d1681)`

```magma
> m := Random(2, 10000);
> if IsPrime(m) then
>     m, "is prime";
> else
>     Factorization(m);
> end if;
[ <23, 1>, <37, 1> ]
```

## The Simple Conditional Expression

<a id="literal-literal-select-boolean-expression-select-expr1-else-expr2"></a>
### `Boolean expression select expr₁ else expr₂`

This is an expression, of which the value is that of expr$_1$ or expr$_2$, depending on whether Boolean expression is `true` or `false`.

<a id="example-ex-bbc380"></a>
### `Example: Inline Conditional Expressions (ex-bbc380)`

Using the `select ... else` construction, we wish to assign the sign of $y$ to the variable $s$.

```magma
> y := 11;
> s := (y gt 0) select 1 else -1;
> s;
1
```

This is not quite right (when $y = 0$), but fortunately we can nest `select ... else` constructions:

```magma
> y := -3;
> s := (y gt 0) select 1 else (y eq 0 select 0 else -1);
> s;
-1
> y := 0;
> s := (y gt 0) select 1 else (y eq 0 select 0 else -1);
> s;
0
```

The `select ... else` construction is particularly important in building sets and sequences, because it enables in-line `if` constructions. Here is a sequence containing the first 100 entries of the Fibonacci sequence:

```magma
>  f := [ i gt 2 select Self(i-1)+Self(i-2) else 1 : i in [1..100] ];
```

## The Case Statement

<a id="literal-literal-case-case-expr-when-expri-statements-end-case"></a>
### `case expr : when exprᵢ : statements end case`

The expression following `case` is evaluated. The statements following the first expression whose value equals this value are executed, and then the `case` statement has finished. If none of the values of the expressions equal the value of the `case` expression, then the statements following `else` are executed, in the first form. If no action is desired in the latter case, the construction may be abbreviated to the second form above.

<a id="example-ex-62b01f"></a>
### `Example: Case Statement on a Sign (ex-62b01f)`

```magma
> x := 73;
> case Sign(x):
>    when 1:
>       x, "is positive";
>    when 0:
>       x, "is zero";
>    when -1:
>       x, "is negative";
> end case;
73 is positive
```

## The Case Expression

<a id="literal-literal-case-case-expr-expr-left-1-expr-right-1-expr-left-n-expr-right-n-default-expr-def"></a>
### `case< expr | expr_left,1 : expr_right,1, ..., expr_left,n : expr_right,n, default : expr_def>`

This is the expression form of `case`. The expr is evaluated to the value $v$. Then each of the left-hand expressions expr$_{{\rm left},i}$ is evaluated until one is found whose value equals $v$; if this happens the value of the corresponding right-hand expression expr$_{{\rm right},i}$ is returned. If no left-hand expression with value $v$ is found the value of the default expression expr$_{\rm def}$ is returned.

The default case cannot be omitted, and must come last.
