% Demonstrates use of while statement.
%
% Uses a while loop to compute an estimate of machine epsilon.
%
% Semi-colons omitted so that script execution is traced.

% Loop initialization.
myeps = 1.0
niter = 0

% Loop while addition of myeps to 1 returns a floating point
% number that is distinct from 1.  Keep track of how many times
% (number of iterations) the body of the loop executes.
while (1 + myeps) ~= 1 
   myeps = myeps / 2
   niter = niter + 1
end

% Multiply by 2 to recover the last value for which loop condition was true.
myeps = 2 * myeps
fprintf('Estimated value of machine epsilon: %.16e\n', myeps);
fprintf('Loop executed %d times.\n', niter);
