playground_03_control_flow - VMinhoto/workshop-matlab GitHub Wiki

Control Flow

Decision making

  • If
  • Switch Statement

Loops

  • For
  • While
  • Nested Loops

Decision Making

If

Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise.

if (x < 0) %parenthesis are optional
    disp('smaller than zero') 
elseif (x == 0) % else if
    % commands can be written in the same line as long as they are separated by ;
    disp('zerooooooo'); disp('oooooooooo');
else
    fprintf('bigger than %d\n', 0);
    disp('I am still within the else clause!')
end

disp('They kicked me out of the clause :(')

Predict the outcome of this block of code and then test it in Matlab 😄

Switch Statement

A switch block conditionally executes one set of statements from several choices. Each choice is covered by a case statement.

grade = 'B';
   switch(grade)
   case 'A' 
      fprintf('Excellent!\n' );
   case 'B' 
      fprintf('Well done\n' );
   case 'C' 
      fprintf('Well done\n' );
   case 'D'
      fprintf('You passed\n' );
   
   case 'F' 
      fprintf('Better try again\n' );
     
   otherwise
      fprintf('Invalid grade\n' );
   end

Predict the outcome of this block of code and then test it in Matlab 😄 Change the grade variable to the grade you think you'll have in this workshop!

Loops

For

A for loop statement allows us to execute a statement or group of statements multiple times, usually a fixed number of times.

%Print numbers from 10 to 20
for a = 10:20 
    fprintf('value of a: %d\n', a);
end

%Fill elements in a vector
vectorNumbers=zeros(1,10);
for n = 1:length(vectorNumbers)
    vectorNumbers(n)=n;
end
disp(vectorNumbers);

Predict the outcome of the second example and then try it on your Matlab 😄

While

A while loop statement in Matlab programming language repeatedly executes a target statement as long as a given condition is true. It is very similar to the forcycle, but it can be more useful in some situations, for example when you don't know the number of iterations.

a = 10;
% Print numbers from 10 to 20 
while( a < 20 )
   fprintf('value of a: %d\n', a);
   a = a + 1;
end

%Fill elements in a vector
vectorNumbers=zeros(1,10);
n=1
while(n<=length(vectorNumbers))
    vectorNumbers(n)=n;
    n = n + 1;
end
disp(vectorNumbers);

Predict the outcome of the second example and then try it on your Matlab 😄

Nested Loops

Matlab allows to use one loop inside another loop. Following section shows few examples to illustrate the concept.

%Print prime numbers from 2 to 100
for i=2:100
   for j=2:100
      if(~mod(i,j)) 
         break; % if factor found, not prime
      end 
   end
   if(j > (i/j))
      fprintf('%d is prime\n', i);
   end
end

%Matrix Filler
rows=3;
columns=3;
matrix=zeros(3,3);
counter=1;
for i=1:rows
    for j=1:columns
        matrix(i,j)=counter;
        counter=counter+1;
    end
end

Predict the outcome of the second example and then try it on your Matlab 😄