for

Programming

Execute a block of statements repeatedly. The loop variable iterates over the given range or array. Ranges use MATLAB colon notation: start:end (step of 1) or start:step:end.

Syntax

for var = start:end ... end
for var = start:step:end ... end
for var = array ... end

Examples

for i = 1:5
  disp(i)
end
▶ Run
for i = 0:0.5:2
  disp(i)
end
▶ Run
total = 0;
for i = [10, 20, 30]
  total = total + i;
end
disp(total)
▶ 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.