while loops

while loops are usually designed to be repeated until a condition is met. See for loops for the alternative looping structure

The generic format is

while condition_is_true

    %  loop body consists of statements to be repeated
    
end

Simple example

x = 10;
i = 0;
while x > 0.1
  x = x/2.0;
  i = i + 1;
  fprintf('%3d  %8.4f\n',i,x)
end

Document updated 2016-10-10.

Go back to the Reference page.