%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Statement_demo.m
%
% purpose: demo ifesle, switch, and loop in matlab
%
% written by tancy 05/08/2017
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% boolean expressions
% boolean expressions always return 
% 1: True
% 0: False
% >, <, >=, ==, ~=, &, | 

%% operators
% check how the matlab return the result
% 
9>8 
8<9
'a'=='b'
8== (9-1)
1==1
(5+5) < 10
(1==1) & (2==3)
(1==1) |(2==3)
5 + (5<10)



% think the following expressions answers before check
'e' == 'd' + 1

%% simple if condition
% if condition
%    action
% end 
%

num = -4; 
if num <0 
    num = abs(num)
end

disp(num)


%% if else conditions
% if condition
%    action1
% else
%    action2
% end 
%

if rand < 0.5
     disp('It was less than .5!')
   else
     disp('It was not less than .5!')
end

%% nest if else conditions
% if condition
%    action1
% elseif
%    action2
% else
%    action3
% end 
%

if quiz >= 90
        score = 'A';
    elseif quiz >= 60 & quiz < 90
        score = 'B';
    else
        score = 'C';
end

%% if else
%
% This function calculates y based on x: 
% y = 1 for x < ?1
% y = x2 for ?1 ? x ? 2
% y = 4 for x > 2


if x < -1
    y = 1;
elseif (x >= -1) && (x <=2)
    y = x^2; 
else
    y = 4; 
end

%% 

num1 = 3
num2 = pi

if ( (round(num1)==num1) & (round(num2)==num2) ) % if true then disp; if false then no disp   
     disp('num1 and num2 are integers')
end 


if ( (round(num1)==num1) & (round(num2)==num2) )    
     disp('num1 and num2 are integers')
elseif(round(num1)==num1)
	disp('only num1 is an integer')
elseif(round(num2)==num2)
	disp('only num2 is an integer')
else
	disp('neither number is an integer')
end


%% switch example1
% switch switch_expression
% case case_exp1
%    action1
% case case_exp2
%    action2
% case case_exp3
%    action3
% end

lunch_choice = input('Enter 1,2,3, or 4: ');

switch lunch_choice
    case 1
        disp('MM is my today lunch')
    case 2
        disp('KFC is my today lunch')
    case 3
        disp('maybe dumplings......')
    otherwise
        disp('On diet today!!')
end

%% switch example2
x = [12 64 24];
plottype = 'pie3';

%plottype = input('choice plot type (bar or pie):'); % you can input by
%urself

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


%% meaure time
% tic starts a timer 
% toc tells you the number of seconds since when you ran toc


%% loop
% for index = values
%     statements
% end

% values could be 
% initVal:endVal
% initVal:step:endVal
% valArray

tic
for v = 1.0:-0.2:0.0

    disp(v)

end

toc

%% loop for valArray
for v = [3 ,6, 12, 'a']
    disp(v)
end


%% multiple loops
s = 2;
H = zeros(s); % generate 10 x 10 matrix

for c = 1:s
    disp(c) % for debug
    for r = 1:s
        disp(r) % for debug
        H(r,c) = 1/(r+c-1);
    end
end


%% while
% while is a type of loop that runs while a condition is true
% while(boolean statement)
     %do something many times
% end
% you would stop repeating the loop when the
% boolean statement is false OR if you use
% break

a = 10;
% while loop execution 
tic
while( a < 20 )
    disp(a)
    a = a + 1;
end
toc

%% 
tic
loops = 0;
while(toc<5)
    loops = loops+1;
    disp(['number of loops = ', num2str(loops)]);
    disp(['number of seconds = ', num2str(toc)]);
end

%% While program stopping after 10 loops
tic
loops = 0;
while(toc<5)
    loops = loops+1;
    disp(['number of loops = ', num2str(loops)]);
    disp(['number of seconds = ', num2str(toc)]);
    
    if(loops>=10)
        break;
    end
end


%% function


try 
    
end

