Documentation

# `repeat`, `until`, `end_repeat`, `_repeat`

“repeat” loop

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

## Syntax

```repeat
`body`
until `condition` end_repeat

_repeat(`body`, `condition`)
```

## Description

`repeat - end_repeat` is a loop that evaluates its body until a specified stopping criterion is satisfied.

In a `repeat` loop, first `body` and then `condition` are evaluated until `condition` evaluates to `TRUE`.

In contrast to the `while` loop, the body of a `repeat` loop is always evaluated at least once.

The body can consist of any number of statements which must be separated either by a colon `:` or a semicolon `;`. Only the last evaluated result inside the body (the return value of the loop) is printed on the screen. Use `print` to see intermediate results.

The Boolean expression `condition` must be reducible to either `TRUE` or `FALSE`. Internally, the condition is evaluated in the lazy evaluation context of the functions `_lazy_and` and `_lazy_or`.

The statements `next` and `break` can be used in `repeat` loops in the same way as in `for` loops.

The keyword `end_repeat` can be replaced by the keyword `end`.

The imperative form `repeat - end_repeat` is equivalent to corresponding call of the function `_repeat`. In most cases, the imperative form leads to simpler code.

The `\$` operator is often a more elegant notation for loops.

`_repeat` is a function of the system kernel.

## Examples

### Example 1

`repeat` loops do not show intermediate results of statements within a loop:

```i := 1: s := 0: repeat s := s + i; i := i + 1; until i >= 3 end_repeat```

Above, only the return value of the loop is displayed. Use `print` to see intermediate results:

```i := 1: s := 0: repeat print("intermediate sum" = s); s := s + i; i := i + 1; s until i >= 3 end_repeat```

`delete i, s:`

### Example 2

Express the same statements as a `repeat` loop and as an equivalent `while` loop. In more complicated cases, you might need additional initializations of variables:

```i := 1: repeat print(i); i := i + 1; until i = 3 end:```

```i := 1: while i < 3 do print(i); i := i + 1; end:```

`delete i:`

### Example 3

The Boolean expression `condition` must evaluate to `TRUE` or `FALSE`:

```repeat condition := UNKNOWN; print(Condition = condition); until condition end_repeat:```

```Error: 'TRUE' or 'FALSE' expected. [repeat] ```

To avoid this error, change the stopping criterion to ```condition <> TRUE```:

```repeat condition := UNKNOWN; print(Condition = condition); until condition = UNKNOWN end_repeat:```

`delete condition:`

### Example 4

You also can create a `repeat` loop by using the functional form `_repeat`:

`hold(_repeat((statement1; statement2), condition))`
```repeat statement1; statement2 until condition end_repeat ```

## Parameters

 `body` The body of the loop: an arbitrary sequence of statements `condition`

## Return Values

Value of the last command executed in the body of the loop. If no command was executed, the value `NIL` is returned. If the body of a `while` loop is not evaluated due to a false condition, the void object of type `DOM_NULL` is returned.