

% ----------------------------------------------------------------
% Controlflow in MATLAB
% ----------------------------------------------------------------


% Trainer:
% Md. Jalal Uddin
% PhD candidate - Atmospheric Physics and Atmospheric Environment (NUIST)
% M.Sc. in Applied Meteorology (NUIST)
% B.Sc. in Disaster Management (PSTU)
% Founder and Director of Research Society
% Email: dmjalal90@gmail.com,  20205103002@nuist.edu.cn  
% Website: https://researchsociety20.org/founder-and-director/ 
% Researchgate: https://www.researchgate.net/profile/Md_Uddin125 


clc;clear;close all

a = 1:2:10  %  start :increment :end


for i = 1:10    %  index = start:increment:end
    disp(i)     % similar to "print" in Python, NCL etc.
end


for n = 1:10           %  index = start:increment:end
    x = cos(n*pi/10);  % statement
end

% -----------------------
% save output
% -----------------------

% method 1
for n    = 1:10     %  index = start:increment:end
    x(n) = cos(n*pi/10);  % statement
end

% method 2
y = zeros(1,10);     % preallocated memory for y

% It is similar to Python. We often create an empty list, for example, 
% y = [], and append it. 

for i  = 1:10 
 y(i)  = cos(i*pi/10);
end


% ----------------------
% count loop iterations
% ----------------------

i=0;           
for n = 1:10 
        i = i+1;  % increment
     x(n) = cos(n*pi/10);
end


% ------------------
% Nested "for" loop 
% ------------------

for i = 1:10 
    for j =1:10
        A(i,j) = i^2 + j^2;     % similar to c = a square + b square
    end
end


%------------------------
% "while" loop 
% while loop to repeat when condition is true
%-----------------------

i = 1;
while i < 10 
   fprintf('value of i: %d\n', i);
   i = i + 1;  % increment
end


% ------------------------------
% conditional statement
% "if", "elseif", "else"
%-------------------------------

% Test for positive or negative number
n = 10;

if n<0
    fprintf('The number is negative:%\n',n);
elseif n>0
    fprintf('The number is positive:%\n',n);
else
    fprintf('The number is zero:%\n',n);
end


% Test for even or odd number
number    = 5;
remainder = 5/2;

if remainder ==0
    disp('The number is even')
else
    disp('The number is odd')
end


% "if" for scaler expression
a = 10;
b = 20;

if a>5
    a = a*b;
end


apples = 10;
cost   = apples*20;  % 200 dollars

if apples>5
    cost = (1-20/100)*cost;  % give 20% discount for larger purchases
end
                       
 
k = 1:20;

for i = 1:length(k)
    if k(i)>10
       k(i) = k(i)*10;
    elseif k(i)<10
       k(i) = k(i)-10;
    else
       k(i) = k(i);
    end
end
 
disp(k)


% --------------------------------------
% "break" and "continue"
% --------------------------------------

for n = 1:10 
     x(n) = cos(n*pi/10);
    if n == 5  
        break
    end
end


i = 1;
while i < 10 
   fprintf('value of i: %d\n', i);
   i = i + 1;   % increment
   if i == 5
       break
   end
end


   
for n = 1:10 
    if n == 5  
      % skip the current iteration   
        continue
    end
    x(n) = cos(n*pi/10);
end


i = 1;
while i < 10 
   i = i + 1;   % increment
   if i == 5
       % skip the current iteration 
       continue
   end
    fprintf('value of i: %d\n', i);
end


% -------------------------------------------------------------------------
% "switch, case, otherwise"
% evaluates an expression and chooses to execute one of several groups of statements. 
% Each choice is a case.
% ----------
% Cautions:
% A case_expression cannot include relational operators
% If the first case statement is true, MATLAB does not execute the other case statements.
% Do not use a break statement within a switch block. break is not defined outside a for or while loop.
% Adapted from MATLAB
%--------------------------------------------------------------------------

% compare single values

n = input('Enter a number: ');

switch n
    case -1
        disp('negative one')
    case 0
        disp('zero')
    case 1
        disp('positive one')
    otherwise
        disp('other value')
end


% compare against multiple values

x        = [12 64 24];
plottype = 'pie';

switch plottype
    case 'bar' 
        bar(x)
        title('Bar Graph')
    case {'pie','pie3'}
        pie3(x)
        title('Pie Chart')
    otherwise
        warning('Unexpected plot type. No plot created.')
end



% -------------------------------------------------------
% "try, catch"
% Execute statements and catch resulting errors
% -------------------------------------------------------


try
    a = notaFunction(5,6);
catch
    warning('Problem using function.  Assigning a value of 0.');
    a = 0;
end


x = ones(4,2);
y = 4*eye(3);       % wrong size                Note: eye is the N-by-N identity matrix.

try   
      z = x*y; 
catch
      z = nan; 
      disp('X and Y are not conformable.')
end


