%%Lecture 9, Part 1 - Boolean Expressions
% CS 368 (MATLAB) Spring 2020

% Boolean expression - logical expression, evaluates true or false

% Later video for this lecture - if statements, while loops. Execute code
% depending on the result of a boolean expression

% MATLAB has a logical type, which is the result of a boolean expression
% (logical 1 is true, logical 0 is false)

% MATLAB uses numerical values as logicals as well. Any non-zero value is
% true, 0 is false. Other languages only use 1 for true and zero for false.

% Suggestion (for good coding practice): Use 1 for true and zero for false
% in general.


%%Relational operators - comparison of numerical values that evaluates true
%%or false

%%Relational Operators Examples
a = 3;
%b is a random integer between 1 and 6
b = randi(6);
fprintf('a = %d, b = %d\n', a, b);
%a greater than b
gtresult = a > b;
fprintf('a greater than b result: %d\n', gtresult)
%A greater than or equal to b
geqresult = a >= b;
fprintf('a greater than or equal to b result: %d\n', geqresult)
%a less than b
ltresult = a < b;
fprintf('a less than b result: %d\n', ltresult)
%a less than or equal to b
leqresult = a <= b;
fprintf('a less than or equal to b result: %d\n', leqresult)
%a equal to b
eqresult = a == b;
fprintf('a equal to b result: %d\n', eqresult)
%a not equal to b
neqresult = a ~= b;
fprintf('a not equal to b result: %d\n', neqresult)

% Relation operators work for matrix and vector input elementwise
% Need to use matrices of the same size when comparing
A = [1, 2; 3, 4];
B = eye(2);
disp(A);
disp(B);
disp(A > B);

% Can use a single scalar to compare against a matrix.
disp(A <= 2);


%%Logical operators
% Used to compare two boolean values (true or false values) and return
% another boolean value.

%Logical operators examples
a = 3; b = randi(6); c = 2;
fprintf('a = %d, b = %d, c = %d\n', a, b, c);
%AND - true if both expressions are true
andresult = (a >= b) & (b ~= c);
fprintf('Result of AND statment: %d\n', andresult);
%OR - true if at least one of the expressions is true
orresult = (a >= b) | (b ~= c);
fprintf('Result of OR statement: %d\n', orresult);
%NOT/NEGATION
notresult = ~(a >= b);
fprintf('Result of NOT statement: %d\n', notresult);

% Upside: &, |, ~ all work on vector and matrix input
% Downside: &, | do not "short curcuit"

% Matrix example for logical operators
A = [1, 2; 3, 0];
B = eye(2);
disp(A);
disp(B);
disp(A & B);
disp((A > B) & (A ~= 3));

% && - short curcuiting AND
% || - short curcuiting OR

% Upside: Faster evaluation and runtime due to short curcuiting.
% Downside: Only works on scalar input
% If you have scalar inputs, && and || are preferred for speed.

%% Short Curcuiting
% Stop evaluating a boolean expression as soon as possible
% A & B -> evaluate A, evaluate B, then AND to get result.
% A && B -> evaluate A,
%   A is false, result is false
%   A is true, evaluate B and the result is the value of B
% A || B -> evaluate A,
%   A is true, result is true
%   A is false, evaluate B and the result is the value of B


