Whatever changes the "flow" of the program from line-by-line to skip some lines or repeat some lines
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
year = -1984;
if year < 0
year = -year; % the year probably shouldn't be negative
end
% most common form
if year > 1984
fprintf('Do something\n');
else
fprintf('Do something else\n');
end
i = 1;
while i < 100
i = i + 1;
end
i
i = 100;
while i < 100
i = 17;
end
i
for i = 1:10
fprintf('Do something 10 times, this is time %i\n', i);
fprintf('--------------------------------------\n');
end
v = [2, 3, 5, 7, 11, 13, 17, 19, 23];
for i = 1:length(v)
v(i) = v(i) * 2;
end
v
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
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
This works both in $\texttt{for}$ and $\texttt{while}$ loops
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
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