%% If Statements
% A piece of code that executes only when a boolean expression is true


%Syntax:
%if boolean_expression
% statements that will execute if boolean_expression is true
%end



%If Statement example: Prompt user for a number and display the absolute value of
%it (make a negative number positive)
x = input('Enter a number:\n');
if x < 0
  x = -1*x;
end
disp(x)



%%Else Statements
%Want to execute a piece of code when the boolean expression in the if
%statement is false.



%Syntax:
%if boolean_expression
% statements to execute if boolean_expression is true
%else
% statements to execute if boolean_expression is false
%end

%Else Statement example: Prompt user for a number. If it's negative, make
%it positive. Otherwise, add 10 to it.
x = input('Enter a number:\n');
if x < 0
  x = -1*x;
else
  x = x + 10;
end
disp(x)

%%Elseif Statements
%Each elseif statement has its own boolean expression
%Execute code if the boolean expression is true and all previous
%if and elseif boolean expressions are false


%Syntax:
%if boolean_expression1
% statements which execute if boolean_expression1 is true
%elseif boolean_expression2
% statements which execute if boolean_expression2 is true AND
% boolean_expression1 is false
%elseif boolean_expression3
% statements which execute if boolean_expression3 is true AND
% boolean_expression1 and boolean_expression2 are false
%else
% statements which execute if none of the above boolean expressions
% are true
%end

%MATLAB trick for indentation: highlight the code, right click and choose
%Smart Indent (or you can use Ctrl+I)

%Elseif example: Prompt user for a number. If it's negative, make
%it positive. If it is between 0 and 5 (inclusive) add 5. If it is between
%10 and 20 (inclusive), multiply it by 2
x = input('Enter a number:\n');
if x < 0
  x = -1*x;
elseif x <= 5
  x = x + 5;
elseif x >= 10 && x <= 20
  x = 2*x;
end
disp(x)

%Elseif example 2: Prompt user for a number. If it's negative, make
%it positive. If it is between 0 and 5 (inclusive) add 5. If it is between
%10 and 20 (inclusive), multiply it by 2. Otherwise, set it to 0.
x = input('Enter a number:\n');
if x < 0
  x = -1*x;
elseif x <= 5
  x = x + 5;
elseif x >= 10 && x <= 20
  x = 2*x;
else
  x = 0;
end
disp(x)


