| MATLAB Function Reference | ![]() |
while expression, statements, end
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 with the value of the loop counter incremented by 1.
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 Statementsin the MATLAB Programming Fundamentals documentation for more information on controlling the flow of your program code.
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.
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))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
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.
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*2This 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.
Given matrices A and B,
A = B =
1 0 1 1
2 3 3 4Expression | Evaluates As | Because |
|---|---|---|
| A < B | false | 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 & B | false | A(1,2) is false, and B is ignored due to short-circuiting. |
| B < 5 | true | Every element of B is less than 5. |
end, for, break, continue, return, all, any, if, switch
![]() | which | whitebg | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |