Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

while - Repeatedly execute statements while condition is true

Syntax

while expression, statements, end

Description

while expression, statements, end repeatedly executes one or more MATLAB statements in a loop, continuing until expression no longer holds true or until MATLAB encounters a break, or return instruction. thus forcing an immediately exit of the loop. If MATLAB encounters a continue statement in the loop code, it immediately exits the current pass at the location of the continue statement, skipping any remaining code in that pass, and begins another pass at the start of the loop statements.

expression is a MATLAB expression that evaluates to a result of logical 1 (true) or logical 0 (false). expression can be scalar or an array. It must contain all real elements, and the statement all(A(:)) must be equal to logical 1 for the expression to be true.

expression usually consists of variables or smaller expressions joined by relational operators (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.

(count < limit) && ((height - offset) >= 0)

statements is one or more MATLAB statements to be executed only while the expression is true or nonzero.

The scope of a while statement is always terminated with a matching end.

See Program Control Statements in the MATLAB Programming Fundamentals documentation for more information on controlling the flow of your program code.

Remarks

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 – Nonscalar Expression, 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.

1)   while (A && B)              2)   while (A || B)

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))

Empty Arrays

In most cases, using while on an empty array returns false. There are some conditions however under which while evaluates as true on an empty array. Two examples of this are

A = [];
while all(A), do_something, end
while 1|A, do_something, end

Short-Circuiting Behavior

When used in the context of a while or if expression, and only in this context, the element-wise | and & operators use short-circuiting in evaluating their expressions. That is, A|B and A&B ignore the second operand, B, if the first operand, A, is sufficient to determine the result.

See Short-Circuiting in Elementwise Operators for more information on this.

Examples

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.

eps = 1;
while (1+eps) > 1
    eps = eps/2;
end
eps = eps*2

This example is for the purposes of illustrating while loops only and should not be executed in your MATLAB session. Doing so will disable the eps function from working in that session.

Example 2 – Nonscalar Expression

Given matrices A and B,

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

Expression

Evaluates As

Because

A < Bfalse

A(1,1) is not less than B(1,1).

A < (B + 1)true

Every element of A is less than that same element of B with 1 added.

A & Bfalse

A(1,2) is false, and B is ignored due to short-circuiting.

B < 5true

Every element of B is less than 5.

See Also

end, for, break, continue, return, all, any, if, switch

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS