% this is a script file to demonstrate control structures
%Exa = 1;  % choose the example to run

switch Exa
  case 1                 % for loops
    for k = 1:10
      sprintf('the square of %i is %i\n',k,k^2)
    end%for               % with Matlab use "end" only
    for k = 2:0.5:6.1    % different step size
      [k,k^2]
    end%for
  case 2                 % while loops
    fib = ones (1, 10);
    i = 3;
    while (i <= 10)
      fib (i) = fib (i-1) + fib (i-2);
      i = i+1; % for Octave you may use  i++;
    end%while
    fib
  case 3                 % do ... until (Octave only)
    fib = ones (1, 12);
    i = 2;
    do   % not available in Matlab
      i = i+1;
      fib (i) = fib (i-1) + fib (i-2);
    until (i == 12)
    fib
  case 4                 % if ... then ... else
    vec = round(rand(1,10)*100);  % generate random numbers between 0 and 100
    vec = randi(100,1,10)
    for k = 1:10
      if (vec(k) >= 50)       % subtract 50, if number larger than 50
	       vec(k) = vec(k) -50; % with Octave you may use vec(k)+= -50;
      end%if
    end%for 
    vec
end%switch
