Command Sequence Controls

Counter Variable

Often, loops involve a count or a counter variable that is updated each time the code within a certain condition is executed.

Exercise: Counter variable

> Type in the following code and verify the result.

wink Tip: Clear the Command Window by going to Edit>Clear Command Window.

% determine the number of elements in allNumbers bigger

% than 100

count = 0                         % initialize count

allNumbers = 200*rand(1,10);      % array of random numbers

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

end
count                             % print count