% Jake Bobowski
% July 27, 2017
% Created using MATLAB R2014a

% This tutorial will introduce simple implementations of conditional if
% statements in MATLAB.

clearvars

% Here's a simple if statement.
ans = 'y';
if ans == 'y'
    'yes'
end;

% In the example above, ans was set equal to 'y' and the if condition was
% true such that 'yes' was printed.  In the example below, ans is not equal
% to 'y' and the if condition is false.  As a result, no output is printed.
ans = 'n';
if ans == 'y'
    'yes'
end;

% We can enhance the capability of our if statement by adding an else
% statement.  Anytime the if condition is false, the else statement will
% execute.

% True, so 'yes' output.
ans = 'y';
if ans == 'y'
    'yes'
else
    'no'
end;

% False, so 'no' output.
ans = 'n';
if ans == 'y'
    'yes'
else
    'no'
end;

% False, so 'no' output.
ans = 'x';
if ans == 'y'
    'yes'
else
    'no'
end;

% In the above if statement the only "y" produces a "yes", any other entry
% results in "no".  Suppose that you only want to accept "y" and "n" as
% valid responses.  Including an elseif condtion as done below produces
% the desired results.

% If condition true, so output 'yes'.
ans = 'y';
if ans == 'y'
    'yes'
elseif ans == 'n'
    'no'
else
    'invalid response'
end;

% Elseif condition true, so output 'no'.
ans = 'n';
if ans == 'y'
    'yes'
elseif ans == 'n'
    'no'
else
    'invalid response'
end;

% If and elseif conditions false, so output 'invalid response'.
ans = 'x';
if ans == 'y'
    'yes'
elseif ans == 'n'
    'no'
else
    'invalid response'
end;
ans =

yes


ans =

yes


ans =

no


ans =

no


ans =

yes


ans =

no


ans =

invalid response