while

Programming

Execute a block of statements repeatedly as long as the condition is true (nonzero). Use break to exit early. Beware of infinite loops.

Syntax

while condition ... end

Examples

x = 1;
while x < 100
  x = x * 2;
end
disp(x)
▶ Run
n = 10;
while n > 0
  disp(n)
  n = n - 1;
end
▶ Run

See Also

Try SimLab — Free MATLAB® Alternative

466 functions. Runs in your browser. No install.

Open SimLab

Stay Updated

Get notified about new simulations and tools. We send 1-2 emails per month.