Getting Started |
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
The cautions involving matrix comparisons that are discussed in the section on the if
statement also apply to the while
statement.
for | continue |