Skip to content

Iteration

Iteration (loops) allow a program to repeat work: processing items, accumulating results, or running until a condition changes. Use loops for repeated tasks; prefer vectorized operations (Matlab, NumPy) when they are simpler and faster.

# for over a list
names = ["Alice", "Bob", "Carol"]
for name in names:
    print(name)

# range-based loop
for i in range(5):  # 0..4
    print(i)

# enumerate to get index and value
for i, v in enumerate(names):
    print(i, v)

# while loop
n = 5
while n > 0:
    print(n)
    n -= 1

# break and continue
for x in [1, -1, 3, 0]:
    if x < 0:
        continue
    if x == 0:
        break
    print(x)
% for loop with range (1-based)
for i = 1:5
  disp(i)
end

% for over array (column iteration)
A = [10 20 30];
for v = A
  disp(v)
end

% while loop
n = 5;
while n > 0
  disp(n)
  n = n - 1;
end

% break and continue
for x = [1 -1 3 0]
  if x < 0
    continue
  end
  if x == 0
    break
  end
  disp(x)
end

% Vectorized alternative example (prefer when possible)
vals = [1 2 3 4]; weights = [0.1 0.2 0.3 0.4];
s = sum(vals .* weights);  % no explicit loop

Gotchas

  • Indexing origin: Python is 0-based; Matlab is 1-based. Off-by-one errors are common.
  • Modifying a list/array while iterating it can lead to skipped elements or unexpected behavior.
  • while loops can become infinite; ensure the loop condition will eventually be false.
  • Prefer vectorized operations in Matlab and NumPy for performance and clarity when operating on arrays.