Control Flow: if, for, while
Conditionals
x = 7;
if x > 5
disp('big')
elseif x == 5
disp('exactly five')
else
disp('small')
end
for loops
total = 0;
for i = 1:5
total = total + i;
end
disp(total)
while loops
n = 1;
while n < 100
n = n * 2;
end
disp(n)
Why This Matters for Your Exams
MATLAB is vectorized — a lot of the time, an operation on a whole vector (like sum(v) from Unit 1) replaces what would otherwise be a for loop. Knowing both means you can write the short vectorized version AND understand/debug a loop-based one when a problem specifically asks for it.
Try it yourself
lock
Real-time practice is a paid add-on
Reading units and the practice quiz are free. Pay once to unlock running real code for LaTeX & MATLAB.

