Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS005 Introduction to Programming

Similar presentations


Presentation on theme: "CS005 Introduction to Programming"— Presentation transcript:

1 CS005 Introduction to Programming
Matlab Eamonn Keogh

2 Quick Review of Functions
Let us write a function that gets the user age as an integer (nothing) We need a good name for the function… We need to make sure that even if the use types in a real number, we return an integer. The built-in function round will help. GetAge Say the user types in 10.5 10

3

4 If the person entering their age is under 18, I want to display a warning “You cannot vote” on the screen. This is called conditional branching

5 Conditional Branching
Get the Users Age Check if age less than 18 Yes No Display Warning Message Return rounded version of Age

6 Conditional Branching
We do conditional Branching with the if statement function UsersAge = GetAge() UsersAge = input('Please Enter your age in years : '); if UsersAge < 18 disp('You cannot vote') end UsersAge = round(UsersAge);

7 We do conditional Branching with the if statement
if UsersAge < 18 disp('You cannot vote') end if some logical expression is true do these statements

8 We do conditional Branching with the if statement
if UsersAge < 18 disp('You cannot vote') end (Minor note: You can write this on a single line, so long as you put commas after each statement) if UsersAge < 18, disp('You cannot vote') , end

9 We do conditional Branching with the if statement
if some logical expression is true do these statements end Matlab uses ‘0’ to mean false Matlab uses ‘1’ to mean true (actually any non-zero)

10 EDU>> 1 + 2 ans = 3 EDU>> 10 < 20 1
When Matlab sees an arithmetic expression such as 1 + 2, it evaluates it: EDU>> 1 + 2 ans = 3 When you evaluate a arithmetic expression, the answer is always a single number. When Matlab sees a logical (or relational )expression such as 10 < 20, it evaluates it: EDU>> < 20 1 When you evaluate a logical expression, the answer is always true or false, which is matlab are represented as 1 and 0

11

12

13 We can practice these skills on the Matlab command line.
Here we as saying.. “Matlab, please tell us if two is less than four?” And Matlab will respond by returning a ‘1’ if it is true (as it happens to be) but a ‘0’ if it is false

14 EDU>> 2 < -7 ans = EDU>> 2 < abs(-7) 1 EDU>> 2 < max(1,0) EDU>> 2 < max(1,17) EDU>> 2 < 2.3 ans = 1 EDU>> 2 < round(2.3) EDU>> 2 < 1+2 EDU>> 2 < sqrt(9)

15 EDU>> HisAge = 17; EDU>> 2 < HisAge ans = 1 EDU>> 2 < min(1,HisAge) EDU>> 2 < HisAge - 500 EDU>> HisAge < 21 EDU>> HisAge = 17; EDU>> HerAge = 34; EDU>> HisAge < HerAge ans = 1 EDU>> 20 < max(HerAge,HisAge)

16 The relational operators
Math Matlab  ==  <  >  >=  <=  ~= It is important to differentiate between assignment and comparison for equality A = B % A is assigned to B A == B % A equal to B? ~ is the tilde (TILL-duh or TILL-day) It is usually to the left of ‘1’ key on keyboard

17 EDU>> <= 50 ans = 1 EDU>> <= 2 EDU>> < 2 EDU>> 2 == 4 ans = EDU>> 2 == 2 1 EDU>> 2 ~= 2 EDU>> 2 ~= 7

18 A Golden Rule ( For all computer languages, not just Matlab)
We should never test for equality or inequality with real numbers EDU>> == % Do not do this! ans = 1 EDU>> HisAge = GetAge(); EDU>> HisAge == 18 % Do not do this! EDU>> round(HisAge) == 18 % But this is OK! We will see why later….

19 Let us return to this example…
if UsersAge < 18 disp('You cannot vote') end (Minor note: You can write this on a single line, so long as you put commas after each statement) if UsersAge < 18, disp('You cannot vote') , end

20 Let us return to this example…
EDU>> UsersAge = 14; EDU>> if UsersAge < 18, disp('You cannot vote') , end You cannot vote EDU>> UsersAge = 44; EDU>>

21 More examples… EDU>> UsersAge = 14;
EDU>> if UsersAge < 21, disp('You cannot drink') , end You cannot drink EDU>> EDU>> UsersAge = 55; EDU>> if UsersAge < 21, disp('You cannot drink') , end EDU>> if UsersAge >= 55, disp(' senior discount!') , end senior discount!

22 Boolean Variables (sort of)
Just as we can have variables to hold numbers, for example: EDU>> HisAge = 12; EDU>> HerBMI = 23.23; We can have variables to hold truth values, for example EDU>> IsMarried = 1; % I have assigned this TRUE EDU>> IsIrish = 0; % I have assigned this FALSE It is a great idea to name these variables IsSomething. We may call these variables, Boolean variables (But, at least in Matlab, they are just ordinary numbers)

23 We can have variables to hold truth values, for example
EDU>> IsMarried = 1; % I have assigned this TRUE EDU>> IsIrish = 0; % I have assigned this FALSE EDU>> if IsMarried, disp('Hard Luck') , end Hard Luck EDU>> if IsIrish, disp('You lucky dog!') , end EDU>>

24 Only return a 0 or 1, a true or false
Let us write a function that asks a user their age, and returns their voting status. Their voting status is binary (or Boolean), you can either legally vote or not. Here is how we might use it.. EDU>> IsAbleToVote = GetVotingStatus(); EDU>> if IsAbleToVote, disp(‘Please Pull Lever Now:') , end (nothing) GetVotingStatus Only return a 0 or 1, a true or false

25 function VoteStatus = GetVotingStatus()
VoteStatus = 0; % Let us assume she cannot vote UsersAge = input('Please Enter your age in years : '); if UsersAge >= 18 VoteStatus = 1; % Let us change our assumption end

26 Our function seems to work…
EDU>> GetVotingStatus Please Enter your age in years : 1 ans = Please Enter your age in years : 33 1

27 Lab assignment will be online during lab Quiz on Monday?
Review Session: Tomorrow 10 to 12, meet here If you are late, you will miss it (late means, one second after 10:00am)


Download ppt "CS005 Introduction to Programming"

Similar presentations


Ads by Google