MATLAB Function Reference |

Repeat statements an indefinite number of times

**Syntax**

**Description**

```
while
```

repeats `statements`

an indefinite number of times. The `statements`

are executed while the real part of

has all nonzero elements.*expression*

is usually of the form*expression*

where

is ==, <, >, <=, >=, or ~=.*rel_op*

The scope of a `while`

statement is always terminated with a matching `end`

.

**expression**

is a MATLAB expression, usually consisting of variables or smaller expressions joined by relational operators *expression*`(`

e.g., `count < limit),`

or logical functions (e.g., `isreal(A)`

).

Simple expressions can be combined by logical operators (`&`

,`|`

,`~`

) into compound expressions such as the following. MATLAB evaluates compound expressions from left to right, adhering to operator precedence rules.

**statements**

is one or more MATLAB statements to be executed only while the *statements*

is *expression*`true`

or nonzero.

**Nonscalar Expressions**

If the evaluated `expression`

yields a nonscalar value, then every element of this value must be `true`

or nonzero for the entire expression to be considered `true`

. For example, the statement, `while`

`(A < B)`

is `true`

only if each element of matrix `A`

is less than its corresponding element in matrix `B`

. See Example 2, below.

**Partial Evaluation of the Expression Argument**

Within the context of an `if`

or `while`

expression, MATLAB does not necessarily evaluate all parts of a logical expression. In some cases it is possible, and often advantageous, to determine whether an expression is `true`

or `false`

through only partial evaluation.

For example, if `A`

equals zero in statement 1 below, then the expression evaluates to `false`

, regardless of the value of `B`

. In this case, there is no need to evaluate `B`

and MATLAB does not do so. In statement 2, if `A`

is nonzero, then the expression is `true`

, regardless of `B`

. Again, MATLAB does not evaluate the latter part of the expression.

You can use this property to your advantage to cause MATLAB to evaluate a part of an expression only if a preceding part evaluates to the desired state. Here are some examples.

while (b ~= 0) & (a/b > 18.5) if exist('myfun.m') & (myfun(x) >= y) if iscell(A) & all(cellfun('isreal', A))

**Example 1 - Simple while Statement**

The variable `eps`

is a tolerance used to determine such things as near singularity and rank. Its initial value is the *machine epsilon*, the distance from 1.0 to the next largest floating-point number on your machine. Its calculation demonstrates `while`

loops.

**Example 2 - Nonscalar Expression**

A = B = 1 0 1 1 2 3 3 4

**Expression****Evaluates As****Because**`A < B`

`false`

`A < (B + 1)`

`true`

Every element of

`A`

is less than that same element of`B`

with`1`

added.`A & B`

`false`

`B < 5`

`true`

**See Also**

`end`

, `for`

, `break`

, `continue`

, `return`

, `all`

, `any`

, `if`

, `switch`

which | whitebg |