day08_logic_if_else_statements_StudentVersion_2023_v2
day08_logic_if_else_statements_StudentVersion_2023_v2
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
Part 1: Intro to
“Selection” Statements
2
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
1
9/19/23
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
Reference
• You should be following along in Chapter 4 of the Attaway book.
Ch 4: 4.1 – 4.3
8
https://bit.ly/3kYngw8 https://tinyurl.com/y5qd8a4e
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
2
9/19/23
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
Selection statements
10
10
Selection statements
11
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
11
3
9/19/23
Selection statements
12
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
12
Selection statements Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
13
13
4
9/19/23
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
Selection statements
14
14
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
Selection statements
15
15
5
9/19/23
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
Selection statements
16
16
if statement
17
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
17
6
9/19/23
[Fill in here]
if statement
18
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
18
[Fill in here]
if statement
end
do more MATLAB statements
19
Copyright James Andrew Smith 2023. Do not share or upload outside of York University
19
7
9/19/23
if-statement
if (x < 0)
x = -x
end
20
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
20
[Fill in here]
if-statement
if (degKelvin < 0)
error('impossible temperature!');
end
21
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
21
8
9/19/23
[Fill in here]
if-statement
if (size(x, 1) > 1)
end
if (size(x, 2) > 1)
x = x';
end
22
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
22
if-statement
23
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
23
9
9/19/23
[Fill in here]
function [tf] = rled(rSeries, vSupply, vLed, iLed)
%RLED Validate LED resistance
% TF = RLED(RSERIES, VSUPPLY, VLED, ILED) returns TRUE
% if the resistor RSERIES is large enough to prevent
% the LED from failing given a supply voltage
% VSUPPLY, forward voltage drop VLED, and operating
% current ILED.
tf = false;
if ( > (vSupply - vLed) / iLed)
tf = true;
24
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
24
25
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
25
10
9/19/23
Part 2:
If + Else
26
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
26
if-else statement
27
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
27
11
9/19/23
[Fill in here]
if-else statement
end
more MATLAB statements do more MATLAB statements
28
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
28
[Fill in here]
if-else statement
else
do the second sequence of
MATLAB statements and then
end
more MATLAB statements do more MATLAB statements
29
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
29
12
9/19/23
[Fill in here]
if-else statement
30
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
30
[Fill in here]
if-else statement
if (
sorted = [x y];
sorted = [y x];
end
Alternatively, use sort() instead
31
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
31
13
9/19/23
32
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
32
Part 3:
If + (Else + If)
33
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
33
14
9/19/23
if-elseif statement
34
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
34
[Fill in here]
if-elseif statement
logical condition 1 if the logical condition 1 is true then
if
sequence of MATLAB statements do the first sequence of MATLAB
statements and then
elseif
sequence of MATLAB statements
else
sequence of MATLAB statements
end
more MATLAB statements do more MATLAB statements
35
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
35
15
9/19/23
[Fill in here]
if-elseif statement
logical condition 1 if logical condition 1 is false then
if
sequence of MATLAB statements
end
more MATLAB statements do more MATLAB statements
36
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
36
[Fill in here]
if-elseif statement
logical condition 1 if logical condition 1 is false then
if
sequence of MATLAB statements
else
do the third sequence of MATLAB
sequence of MATLAB statements statements and then
end
more MATLAB statements do more MATLAB statements
37
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
37
16
9/19/23
[Fill in here]
if-elseif statement
s = y;
else
s = z;
Alternatively, use min() instead
end
38
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
38
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
if-elseif statement
39
39
17
9/19/23
category = 0;
40
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University
40
18