------------------------------------------------------------------------------- Help on WHILE from MATLAB The following MATERIAL is copied DIRECTLY from MATLAB is completely the property of MATHWORKS. I have given links to where you will find the following help. ------------------------------------------------------------------------------- >> help while WHILE Repeat statements an indefinite number of times. The general form of a WHILE statement is: WHILE expression statements END The statements are executed while the real part of the expression has all non-zero elements. The expression is usually the result of expr rop expr where rop is ==, <, >, <=, >=, or ~=. The BREAK statement can be used to terminate the loop prematurely. For example (assuming A already defined): E = 0*A; F = E + eye(size(E)); N = 1; while norm(E+F-E,1) > 0, E = E + F; F = A*F/N; N = N + 1; end See also FOR, IF, SWITCH, BREAK, END. ------------------------------------------------------------------------------- From MATLAB->Getting Started->Programming with MATLAB->while while The while loop repeats a group of statements an indefinite number of times under control of a logical condition. A matching end delineates the statements. Here is a complete program, illustrating while, if, else, and end, that uses interval bisection to find a zero of a polynomial. a = 0; fa = -Inf; b = 3; fb = Inf; while b-a > eps*b x = (a+b)/2; fx = x^3-2*x-5; if sign(fx) == sign(fa) a = x; fa = fx; else b = x; fb = fx; end end x The result is a root of the polynomial x3 - 2x - 5, namely x = 2.09455148154233 The cautions involving matrix comparisons that are discussed in the section on the if statement also apply to the while statement. ------------------------------------------------------------------------------- From MATLAB->Using MATLAB->M-File Programming-> Programming and Data Types: M-File Programming: while The while loop executes a statement or group of statements repeatedly as long as the controlling expression is true (1). Its syntax is while expression statements end If the expression evaluates to a matrix, all its elements must be 1 for execution to continue. To reduce a matrix to a scalar value, use the all and any functions. For example, this while loop finds the first integer n for which n! (n factorial) is a 100-digit number. n = 1; while prod(1:n) < 1e100 n = n + 1; end Exit a while loop at any time using the break statement. while Statements and Empty Arrays A while condition that reduces to an empty array represents a false condition. That is, while A, S1, end never executes statement S1 when A is an empty array. ------------------------------------------------------------------------------- From MATLAB Function Reference: while WHILE Repeat statements an indefinite number of times Syntax while expression statements end Description while repeats statements an indefinite number of times. The statements are executed while the real part of expression has all nonzero elements. expression is usually of the form expression rop expression where rop is ==, <, >, <=, >=, or ~=. The scope of a while statement is always terminated with a matching end. Examples 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 See Also all, any, break, end, for, if, return, switch -------------------------------------------------------------------------------