Control flow just means things like $\texttt{if}$, $\texttt{while}$, $\texttt{for}$

Whatever changes the "flow" of the program from line-by-line to skip some lines or repeat some lines

$\texttt{if}$ - making your program choose alternatives

In [1]:
a = 5;
if a > 1
    fprintf('a is larger than 1!\n');
elseif a == 0
    disp('a is zero');
elseif a < -1
    'a is less than '
    -1
else
    fprintf('A must be -1 or 1 here\n');
end
a is larger than 1!
In [2]:
year = -1984;
if year < 0
    year = -year; % the year probably shouldn't be negative
end
In [3]:
% most common form
if year > 1984
    fprintf('Do something\n');
else
    fprintf('Do something else\n');
end
Do something else

$\texttt{while}$ - repeat while something is true

In [4]:
i = 1;
while i < 100
    i = i + 1;
end
i
i =  100
In [5]:
i = 100;
while i < 100
    i = 17;
end
i
i =  100

$\texttt{for}$ - loop over elements (the most useful loop by far)

In [6]:
for i = 1:10
    fprintf('Do something 10 times, this is time %i\n', i);
    fprintf('--------------------------------------\n');
end
Do something 10 times, this is time 1
--------------------------------------
Do something 10 times, this is time 2
--------------------------------------
Do something 10 times, this is time 3
--------------------------------------
Do something 10 times, this is time 4
--------------------------------------
Do something 10 times, this is time 5
--------------------------------------
Do something 10 times, this is time 6
--------------------------------------
Do something 10 times, this is time 7
--------------------------------------
Do something 10 times, this is time 8
--------------------------------------
Do something 10 times, this is time 9
--------------------------------------
Do something 10 times, this is time 10
--------------------------------------
In [7]:
v = [2,  3, 5, 7, 11, 13, 17, 19, 23];
for i = 1:length(v)
    v(i) = v(i) * 2;
end
v
v =

    4    6   10   14   22   26   34   38   46

In [8]:
M = rand(5, 2);
size(M) % both sizes, rows and columns
size(M, 1) % row size
size(M, 2) % column size

total_passes = 0;
% custom matrix printing
for i = 1:size(M, 1)
    fprintf('[ ');
    for j = 1:size(M, 2)
        fprintf('%f ', M(i, j));
        
        total_passes = total_passes + 1;
    end
    fprintf(']\n');
end
total_passes
ans =

   5   2

ans =  5
ans =  2
[ 0.247579 0.960385 ]
[ 0.291291 0.846207 ]
[ 0.631633 0.946783 ]
[ 0.732580 0.846692 ]
[ 0.577156 0.252428 ]
total_passes =  10

The For Loop technically iterates through the elements, but this is a less common form

In [9]:
v = [2, 3, 5, 7, 11, 13, 17, 19, 23];
for el = v
    fprintf('I''m doubling element %f to %f\n', el, el * 2);
end
I'm doubling element 2.000000 to 4.000000
I'm doubling element 3.000000 to 6.000000
I'm doubling element 5.000000 to 10.000000
I'm doubling element 7.000000 to 14.000000
I'm doubling element 11.000000 to 22.000000
I'm doubling element 13.000000 to 26.000000
I'm doubling element 17.000000 to 34.000000
I'm doubling element 19.000000 to 38.000000
I'm doubling element 23.000000 to 46.000000

$\texttt{break}$ and $\texttt{continue}$ - finishing a loop early and skipping lines in a loop

This works both in $\texttt{for}$ and $\texttt{while}$ loops

In [10]:
i = 0;
while 1 % 1 is true, so while true, so never finish
    i = i + 1;
    if i == 12313
        break % out of the infinite loop
    end
end
i
i =  12313
In [11]:
greeting = 'Hello World!';
for i = 1:length(greeting)
    c = greeting(i);
    if ~isupper(c) % is NOT an upper case character, in-built function
        continue
    end
    greeting(i) = '?';
end
greeting
greeting = ?ello ?orld!