% page 40
% relational operators
clc, clear;
a = 2; b = 4;
aIsSmaller = a < b
bIsSmaller = b < a

clc, clear;
x = 1:5; y = 5:-1:1;
z = x > y

% page 41
% logical operators
% Operator    Meaning
% -------------------
%    &          and
%    |           or
%    -          not
% -------------------
clc, clear;
a = 2; b = 4;
aIsSmaller = a < b;
bIsSmaller = b < a;
bothTrue = aIsSmaller & bIsSmaller
eitherTrue = aIsSmaller | bIsSmaller

% page 44
% if
clc, clear;
a = -1;
if a < 0 
    disp('a is negative');
end
% or one line format
if a < 0, disp('a is negative'); end

% page 45
% if ... else
clc, clear;
x = -1;
if x < 0 
    error('x is negative; sqrt(x) is imaginary');
else
    r = sqrt(x);
end

% page 46
% if ... elseif
clc, clear;
x = -1;
if x > 0 
    disp('x is positive');
elseif x < 0
    disp('x is negative');
else
    disp('x is exactly zero');
end

% page 47
% switch
clc, clear;
color = 'red';
switch color
    case 'red'
        disp('color is red');
    case 'blue'
        disp('color is blue');
    case 'green'
        disp('color is green');
    otherwise
        disp('color is not red, blue, or green');
end

% page 49
% for loops
clc, clear;
x = 1:5;
sumx = 0;
for k = 1:length(x)
    sumx = sumx + x(k);
end

% page 50
% for loops with non-integer increments
clc, clear;
for k = 0:pi/15:pi
    fprintf('%8.2f  %8.5f\n', k, sin(k));
end

% page 54
% break command
clc, clear;
n = 10;
k = breakDemo(n);

% page 55
% return command
clc, clear;
n = 10;
k = returnDemo(n);

% page 58
% replace loops with vector operations
clc, clear;
x = rand(1,10);
for k = 1:length(x)
    y(k) = sin(x(k));
end

% vectorization
clear y;
y = sin(x);

% page 60
% array indexing
clc, clear;
x = sqrt(0:4:20)
i = [1 2 5];
y = x(i)

% page 61
% logical indexing
clc, clear;
x = sqrt(0:4:20)
j = find(rem(x,2) == 0)
y = x(j)

% page 63
% vectorized copy
clc, clear;
A = rand(1,10);
for i = 1:length(A)
    B(i) = A(i);
end

% vectorization
clear B;
B = A;
