Practical 4: For- and While- Loops, If-statements

Command Sequence Controls

Indenting

Indenting

Note that the above code has indents under each of for and if statement

Indenting makes code more readable, as you can see the logical structure which becomes especially important when you have several for/if statements in one M-file. While working on this practical, make sure that you follow this formatting convention.

Exercise: Counter variable

> Reconsider the above example without indenting. Which is easier to read?

 

wink Tip: There is an option called Smart Indent, which will indent highlighted code properly and automatically. See Indent>Smart Indent (the first icon) in the M-file Editor menu bar.

Example:

% determine the number of elements in allNumbers bigger
% than 100

count = 0;
for index = 1:1:10
if allNumbers(index)>100
count = count+1;
end

end
count