0% found this document useful (0 votes)
3 views18 pages

day08_logic_if_else_statements_StudentVersion_2023_v2

The document covers the topic of selection statements in MATLAB, specifically focusing on 'if', 'if-else', and 'if-elseif' statements. It provides examples of how to use these statements to control the flow of execution based on logical conditions. Additionally, it includes functions that demonstrate practical applications of these selection statements.

Uploaded by

tatopla20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

day08_logic_if_else_statements_StudentVersion_2023_v2

The document covers the topic of selection statements in MATLAB, specifically focusing on 'if', 'if-else', and 'if-elseif' statements. It provides examples of how to use these statements to control the flow of execution based on logical conditions. Additionally, it includes functions that demonstrate practical applications of these selection statements.

Uploaded by

tatopla20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

9/19/23

EECS 1011 – Session 8

Logic: Selection Statements

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

Lab E: Shapes & “If Statements”

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

• Many MATLAB scripts are made up of


statements that are executed in Is it true?
sequence No
• Sometimes you need change directions
• a selection statement uses a logical value
or values to select which statements are
Yes
executed

10

10

Selection statements

• Many MATLAB scripts are made up of


statements that are executed in Is it true?
sequence
• Sometimes you need change directions
• a selection statement uses a logical value
or values to select which statements are
executed

11

Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University

11

3
9/19/23

Selection statements

• Many MATLAB scripts are made up of


statements that are executed in Is it true?
sequence
• Sometimes you need change directions
• a selection statement uses a logical value
or values to select which statements are
Yes
executed

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

• Many MATLAB scripts are made up of


statements that are executed in Is it true?
sequence
• Sometimes you need change directions
• a selection statement uses a logical value
or values to select which statements are
Yes
executed

13

13

4
9/19/23

Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University

Selection statements

} Many MATLAB scripts are made Is it true?


up of statements that are No
executed in sequence
} Sometimes you need change
directions
} a selection statement uses a
logical value or values to select
which statements are executed

14

14
Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University

Selection statements

} Many MATLAB scripts are made Is it true?


up of statements that are No
executed in sequence
} Sometimes you need change
directions
} a selection statement uses a
logical value or values to select
which statements are executed

15

15

5
9/19/23

Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University

Selection statements

• Many MATLAB scripts are made up of


statements that are executed in Is it true?
sequence No
• Sometimes you need change directions
• a selection statement uses a logical value
or values to select which statements are
Yes
executed

16

16

if statement

• the if statement allows you to conditionally execute a single block of


MATLAB statements

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

logical condition if the logical condition is true then

sequence of MATLAB statements do the sequence of MATLAB


statements and then
end
more MATLAB statements do more MATLAB statements

18

Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University

18

[Fill in here]
if statement

if logical condition if the logical condition is false then

sequence of MATLAB statements

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

• find the absolute value of a number x

if (x < 0)
x = -x
end

• don't do this, use abs instead

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

• change a row vector to a column vector

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

• create a function that returns true if a resistor value is high enough to


prevent an LED from failing
• http://en.wikipedia.org/wiki/LED_circuit
• the values for forward voltage drop and suggested operating current come
from the data sheet for the LED; e.g.,
• https://www.sparkfun.com/products/9590

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

EECS 1011 – Session 8

Logic: Selection Statements

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

• the if-else statement allows you to conditionally execute one of two


blocks of MATLAB statements

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

logical condition if the logical condition is true then


if
sequence of MATLAB statements do the first sequence of MATLAB
statements and then
else

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

logical condition if the logical condition is false then


if
sequence of MATLAB statements

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

• write a function that returns true if an integer number is even, and


false if it is odd

function [tf] = is_even(x)


%IS_EVEN True for an even number
% TF = IS_EVEN(X) returns 1 if X is an even integer number
% and 0 otherwise.

if (rem(x, 2) == 0) rem(x, 2) is the remainder


of x / 2
else
tf = false;
end

30

Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University

30

[Fill in here]
if-else statement

• write a function that returns a sorted vector of two numbers

function [sorted] = sort2(x, y)


%SORT2 Sort two numbers in ascending order
% SORTED = SORT2(X, Y) returns a row vector containing
% the numbers X and Y in ascending order

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

EECS 1011 – Session 8

Logic: Selection Statements

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

• the if-elseif statement allows you to conditionally execute one of


multiple blocks of MATLAB statements

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

elseif if logical condition 2 is true then

sequence of MATLAB statements do the second sequence of MATLAB


statements and then
else
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

elseif if logical condition 2 is false then

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

• write a function that returns the smallest of three numbers

function [s] = smallest(x, y, z)


%SMALLEST Find the smallest of 3 numbers
% S = SMALLEST(X, Y, Z) returns the minimum of X, Y, and Z

if (x <= y & x <= z)


s = x;

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

• hypertension or high blood pressure categories defined by the


American Heart Association

Category Systolic pressure Diastolic pressure


mmHg mmHg
normal < 120 AND < 80

prehypertension 120-139 OR 80-89

high blood pressure >= 140 OR >= 90

39

39

17
9/19/23

function [category] = catpressure(systolic, diastolic)


[Fill in here]
%CATPRESSURE Categorize blood pressure measurement
% C = CATPRESSURE(SYSTOLIC, DIASTOLIC) returns
% the blood pressure category defined by the American
% Heart Association for the given SYSTOLIC and DIASTOLIC
% pressure measurements in mmHg. The return value C is:
%
% 0 for NORMAL
% 1 for PREHYPERTENSION
% 2 for HIGH BLOOD PRESSURE

if (systolic >= 140 | diastolic >= 90)


category = 2;
elseif
category = 1;

category = 0;

40

Copyright James Andrew Smith & others 2015 - 2023. Do not share or upload outside of York University

40

18

You might also like