Documentation

## Loops

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.

### Use Loops with a Fixed Number of Iterations (for Loops)

The `for` loop repeats a group of statements for a fixed number of iterations. The loop starts with the command `for` and ends with `end_for` or just `end`. MuPAD® executes all statements between these two words as many times as you specify. For example, compute the factorial of an integer using the loop:

```x := 1: for i from 1 to 5 do x := x * i; end_for```
` `

#### More Efficient Alternative to for Loops

You often can speed up the execution of MuPAD code by replacing `for` loops with the sequence generator `\$`. For example, instead of the loop

```x := 1: for i from 1 to 10 do x := x * i; end_for```
` `

use the statement:

``*`(i \$ i = 1..10)`
` `

#### Control Step Size and Count Up and Down

By default, the loop increments the value of a counter by `1` in each step. You can change the step size:

```for i from 1 to 3 step 1/2 do print(i) end_for```
` `
` `
` `
` `
` `

To count backwards decreasing the value of a counter with each step, instead of `to`, use the keyword `downto`:

```for i from 3 downto 1 do print(i) end_for```
` `
` `
` `

#### Use Mathematical Structures in for Loops

MuPAD supports the use of structures such as lists and matrices in for loops:

```for i in [1, -4, a, b] do print(i^2) end_for```
` `
` `
` `
` `
```s := 0: for i in linalg::hilbert(3) do s := s + i^2 end_for```
` `

### Use Loops with Conditions (while and repeat Loops)

#### Condition at the Beginning (while Loops)

MuPAD supports the use of loops with logical conditions. The `while` loop continues while the execution conditions are valid. The loop starts with `while` and ends with `end_while` or just `end`. MuPAD executes all the statements between these commands repeatedly as long as the execution conditions are true. In the `while` loop, MuPAD evaluates the conditions before each iteration. When the condition becomes false, the loop terminates without executing the statements of the current iteration:

```i := 2; while i < 16 do i := i^2; print(i) end_while:```
` `
` `
` `

#### Termination Condition at the End (repeat Loops)

The `repeat` loop continues until the termination condition becomes valid. The loop starts with `repeat` and ends with `end_repeat`. MuPAD executes all the statements between these commands repeatedly while the conditions are false. The `repeat` loop tests a termination condition at the end of each iteration. When the condition becomes true, the loop terminates after executing the statements of the current iteration:

```i := 2; repeat i := i^2; print(i) until i >= 16 end_repeat:```
` `
` `
` `

#### Avoid Infinite Loops: Set a Counter

The `while` and `repeat` loops do not operate for a fixed number of steps. If the execution or termination conditions of such loops never become true, MuPAD can execute the statements within these loops infinitely. For example, if the termination condition is not valid during the first iteration, and it does not change inside the loop, the loop is infinite:

```i := 1; repeat print(i) until i > 3 end_repeat:```

To avoid this infinite loop, use the additional statement to change it in each iteration:

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

#### Use Multiple Conditions

You can use multiple conditions combining the expressions by `and`, `or`, `xor`, or other logical operators:

```i := 2: j := 3: repeat i := i*j; j := j^2; print(i, j) until i > 100 and j > 10 end_repeat:```
` `
` `
` `
```i := 2: j := 3: repeat i := i*j; j := j^2; print(i, j) until i > 100 or j > 10 end_repeat:```
` `
` `

### Use Nested Loops

You can place one or several loops inside another loop. Internal loops can be of the same or different types:

```s := 0: for i from 1 to 3 do for j from 1 to 2 do s := s + i + j; end_for end_for: print(s)```
` `
```s := 0: for i from 1 to 3 do j := 1: while j <= 2 do s := s + i + j; j := j + 1; end_while end_for: print(s)```
` `

### Exit a Loop

To add a possibility to exit a loop, use the `break` command. Suppose you want to exit a loop if some condition is true:

```for i from 1 to 3 do for j from 1 to 2 do if i = j then print(Unquoted, "i = j = ".expr2text(i)); break end_if end_for end_for:```
```i = j = 1 ```
```i = j = 2 ```

The `break` command lets you exit the loop in which you place this command. If you create nested loops and use `break` inside an inner loop, MuPAD continues to execute the statements in the outer loops:

```for i from 1 to 3 do for j from 1 to 2 do if i = j then print(Unquoted, "break with i = j = ".i); break end_if; print(Unquoted, "i = ".i.", j = ".j); end_for end_for:```
```break with i = j = 1 ```
```i = 2, j = 1 ```
```break with i = j = 2 ```
```i = 3, j = 1 ```
```i = 3, j = 2 ```

Suppose you want to stop executing the statements and exit the nested loops as soon as the condition `i = j` is true. Use the additional variable for the break state of the inner loop. Use this variable to exit the outer loop:

```breakAll := FALSE: for i from 1 to 3 do for j from 1 to 2 do if i = j then print(Unquoted, "break with i = j = ".i); breakAll := TRUE; break end_if; print(Unquoted, "i = ".i.", j = ".j); end_for; if breakAll then break end_if; end_for:```
```break with i = j = 1 ```

### Skip Part of Iteration

To skip the commands from a particular point to the end of a loop and start the next iteration, use the `next` command:

```for i from 1 to 3 do for j from 1 to 2 do if i = j then print(Unquoted, "i = j = ".expr2text(i)); next end_if; print(Unquoted, "i = ".expr2text(i), "j = ".expr2text(j)) end_for end_for:```
```i = j = 1 ```
```i = 1, j = 2 ```
```i = 2, j = 1 ```
```i = j = 2 ```
```i = 3, j = 1 ```
```i = 3, j = 2 ```

### Return Value of a Loop

All MuPAD commands produce some return values. Loops in MuPAD return the result of the last executed statement:

```for i from 1 to 3 do x := 2*i; y := 3*i end_for```
` `

Suppress the output of the return value with a colon:

```for i from 1 to 3 do x := 2*i; y := 3*i end_for:```

### Display Intermediate Results

By default, MuPAD does not display intermediate results obtained inside a loop even if you use semicolons after commands. To see intermediate results, use the `print` command inside a loop:

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

To display results of each iteration in a `for` loop, also use the `print` command:

```for i from 1 to 5 do x := i!; print(Unquoted, expr2text(i)."! = ".expr2text(x)) end_for```
```1! = 1 ```
```2! = 2 ```
```3! = 6 ```
```4! = 24 ```
```5! = 120 ```

#### Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos