% if_demo
% This script demonstrates the use of if-, elseif- and switch statements in 
% Matlab.
% It is part of the Matlab course for PhD Students 2010-2011:
% http://www.sinnesphysiologie.uni-oldenburg.de/50246.html
% written by Jutta Kretzberg, 11.12.2010

%% simple if statement

a=input('your number: ')			% user input
if a>0                              % condition
	b=a^2;                          % b will only be set if the condition 
                                    % a>0 is true. 
    disp('your number is positive.')	% output on command line depends on 
                                    % user input.
end                                 % key word to end conditional part
c=-2*a;                             % commands after key word ?end? will be 
                                    % performed independently of the
                                    % condition. 


%% if-else-statement

a=input('your number: ')			% user input
if a>0                              % condition
	b=a^2;                          % b will only be set if the condition 
                                    % a>0 is true. 
    disp('your number is positive.')	% output on command line depends on 
                                    % user input.
else                                % key word to start the other block of 
                                    % commands
    disp('your number is not positive.')	% output on command line 
                                    % depends on user input.
end                                 % key word to end conditional part
c=-2*a;                             % commands after key word ?end? will be 
                                    % performed independently of the
                                    % condition. 
                                
 %% elseif-statements
 
a=input('your number: ')			% user input
if a>0                              % condition
	b=a^2;                          % b will only be set if the condition 
                                    % a>0 is true. 
    disp('your number is positive.')	% output on command line depends on 
                                    % user input.
elseif a==0 						% condition2
    disp('your number is 0. ')      % output on command line depends on 
                                    % user input.                                   
else                                % key word to start the other block of 
                                    % commands
    d=a/2;                          % d will only be set if a<0                                
    disp('your number is negative.')	% output on command line 
                                    % depends on user input.
end                                 % key word to end conditional part
c=-2*a;                             % commands after key word ?end? will be 
                                    % performed independently of the
                                    % condition. 
 
%% nested if-statements
% if-statements in if-statements...
  
a=input('your number: ')                % user input
if isnumeric(a)                         % outer condition
    if a>0                              % inner condition
        b=a^2;                          % b will only be set if both  
                                        % conditions are true. 
        disp('your number is positive.')	% output on command line 
                                        % depends on user input.
    else                                % else for inner if condition
        disp('your number is not positive.')	% output on command line 
                                        % depends on user input.
    end                                 % key word to end inner if 
                                        %statement
    c=-2*a;                             % commands after inner ?end? will
                                        % be performed independently of the
                                        % inner condition, but only if the
                                        % outer condition is true.
else                                    % else for outer condition
    warning('This was not a number!')   % warning will be issued if outer
                                        % condition is false
end                                     % end of outer if statement
v=zeros(1,10);                          % Following statements are computed
                                        % independent of conditions. 
                                        
                                        
%% Combined conditions
% the same result as above, but done differently...

a=input('your number: ')			% user input
if isnumeric(a) && a>0              % combination of conditions
	b=a^2;                          % b will only be set if the condition 
                                    % a>0 is true. 
    disp('your number is positive.')	% output on command line depends on 
                                    % user input.
    c=-2*a;                         % this command should be performed in 
                                    % any case when a is numeric.
elseif isnumeric(a) 				% one of the conditions in if is true 
                                    % (a is a number), the other is not 
                                    % (a<=0). 
    disp('your number is not positive.')	% output on command line 
                                        % depends on user input.  
    c=-2*a;                         % this command should be performed in 
                                    % any case when a is numeric.
else                                % all cases when a is not numeric
    warning('This was not a number!')   % warning will be issued if 
                                        % isnumeric(a) is false
end                                     % end of if statement
v=zeros(1,10);                          % Following statements are computed
                                        % independent of conditions. 
                                        
%% switch statements

a=input('enter letter A, B, or C:','s')   % user input of a character
a=upper(a);                         % make sure that a contains upper case 
                                    % letters
switch a                            % different possibilities for a
    case 'A'                        % first case
        disp('your letter is A')
        c=27;   
    case {'B','C'}                  % cases can be combined by using {}, 
                                    % meaning "B or C"     
        disp('your letter is B or C')
        c=108;
    otherwise                       % all other cases
        warning('you did not enter A, B, or C!')
end
        