Documentation

## Conditional Control

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 if Statements

You can execute different groups of statements depending on particular conditions. Use `if` to define a condition, and use `then` to define the group of statements you want to execute when the condition is true:

```x := -3: if x < 0 then y := x + 2; x := -x; print(x, y) end_if:```
` `

You also can define the group of statements that you want to execute when the condition is false:

```x := 3: if x < 0 then y := x + 2; x := -x; print(x, y) else y := x + 2; x := x; print(x, y) end_if```
` `

MuPAD® does not require parentheses around conditions:

```x := 10: if testtype(x, Type::Positive) = TRUE and type(x) = DOM_INT then print(Unquoted, "x = ".x." is a positive integer"); end_if```
```x = 10 is a positive integer ```

### Apply Multiple Conditions

You can use multiple conditions in conditional statements. Combine multiple conditions by the logical operators:

```x := 5: y := 6: if x > 0 and 1 - y > 0 then print(Unquoted, "the condition is true") else print(Unquoted, "the condition is false") end_if:```
```the condition is false ```
```x := 5: y := 6: if x > 0 or 1 - y > 0 then print(Unquoted, "the condition is true") else print(Unquoted, "the condition is false") end_if:```
```the condition is true ```

### Use Nested Conditional Statements

MuPAD supports the use of nested conditional statements. For example:

```x := 5: if testtype(x, DOM_COMPLEX) = TRUE then print("The Heaviside function is undefined for complex numbers") else if x = 0 then heavisideX := 1/2 else if x < 0 then heavisideX := 0 else heavisideX := 1 end_if: end_if; end_if```
` `

For nested `if ... else if`, use the `elif` command as a shortcut:

```x := 0: if (testtype(x, DOM_COMPLEX) = TRUE) then print("The Heaviside function is undefined for complex numbers") elif x = 0 then heavisideX := 1/2 elif x < 0 then heavisideX := 0 else heavisideX := 1; end_if```
` `

### Use case and otherwise Statements

To choose between several cases, use the `case` command. The `case` statement looks for the first valid condition. After that, it executes all the statements between this condition and the keyword `end_if`, without checking the conditions:

```x := 4: case x of 1 do of 2 do of 3 do print("three or less") of 4 do print("four") of 5 do print("five") otherwise print("6 or more") end_case:```
` `
` `
` `

To exit the `case` statement after executing the statements written under the first valid condition, use the `break` command. See Exiting a Conditional Statement for more details.

### Note

MuPAD executes the `case` statements differently from MATLAB®. MATLAB executes only the first matching `case` statement and skips the following `case` statements. In MuPAD, you must use the `break` command to stop execution of the following statements.

### Exit a Conditional Statement

To exit a conditional statement after executing the statements written under the first valid condition, use the `break` command. For example, select the meaning of the traffic light signals:

```trafficLight := yellow: case trafficLight of red do print(Stop); break; of yellow do print(Caution); break; of green do print(Go); break; end_case```
` `

### Return Value of a Conditional Statement

All MuPAD commands produce some return values. Conditional statements return the result of the last executed command:

```mapColor := blue: if mapColor = blue then "water" else "land" end_if```
` `

Use the return value of a conditional statement like any other value. For example, assign the return value to a variable. By default, MuPAD does not allow conditional statements in assignments. To create a valid assignment, enclose conditional statements in parentheses. Suppress the output of the return value of a conditional statement with a colon:

```mapColor := blue: terrain := (if mapColor = blue then "water" else "land" end_if):```

Write a sentence by concatenating the following string and the variable `terrain`:

`print(Unquoted, "Blue color on maps usually shows ".terrain)`
```Blue color on maps usually shows water ```

The following `case` statement also returns the result of the last assignment:

```palette := color: case palette of monochrome do [color1, color2] := [black, white]; break; of color do [color1, color2, color3] := [red, green, blue]; break; end_case```
` `

### Display Intermediate Results

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

```Omega := 2: if Omega > PI/2 and Omega < PI then signSinOmega := 1; signCosOmega := -1; print(signSinOmega, signCosOmega) end_if:```
` `

#### Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos