CS005 Introduction to Programming

Slides:



Advertisements
Similar presentations
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Advertisements

Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Selection. Computer Programming 2 Objectives Examine if statement in more detail Study use of switch statement to implement multialternative selections.
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Please CLOSE YOUR LAPTOPS, and turn off and put away your cell phones, and get out your note- taking materials.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Introduction to programming in java Lecture 11 Boolean Expressions and Assignment no. 2.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
JavaScript: Conditionals contd.
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Introduction to Decision Structures and Boolean Variables
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
The Ohio State University
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Computer Science 210 Computer Organization
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Variables Variables are used to store data or information.
CLOSE Please YOUR LAPTOPS, and get out your note-taking materials.
CMSC201 Computer Science I for Majors Lecture 04 – Expressions
INC 161 , CPE 100 Computer Programming
CS005 Introduction to Programming
CS005 Introduction to Programming
Chapter 2 - Introduction to C Programming
More important details More fun Part 3
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
EGR 2261 Unit 4 Control Structures I: Selection
IF statements.
Variables and Primative Types
ITM 352 Flow-Control: if and switch
CS 106 Computing Fundamentals II Chapter 33 “Conditional Statements”
Chapter 2 - Introduction to C Programming
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
CS005 Introduction to Programming
Conditional Statements
Data Types, Identifiers, and Expressions
Conditions and Ifs BIS1523 – Lecture 8.
Computers & Programming Languages
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Introduction to C++ Programming
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
CS005 Introduction to Programming
Types, Truth, and Expressions (Part 2)
Computer Science 210 Computer Organization
Programming in JavaScript
CS005 Introduction to Programming: Matlab
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
MATLAB Logical Expressions
Introduction to Decision Structures and Boolean Variables
Introduction To Robot Decision Making
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Programming in JavaScript
Chapter 3: Selection Structures: Making Decisions
Introduction to Primitives
Introduction to Primitives
Life is Full of Alternatives
Chapter 3: Selection Structures: Making Decisions
CPSC 121: Models of Computation
Selection Statements Chapter 3.
CPSC 121: Models of Computation
Decision Structures if, if/else conditions
Data Types and Maths Programming Guides.
OPERATORS in C Programming
The boolean type and boolean operators
OPERATORS in C Programming
Introduction to Python
Types, Truth, and Expressions
Presentation transcript:

CS005 Introduction to Programming Matlab Eamonn Keogh eamonn@cs.ucr.edu

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

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

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

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);

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

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

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)

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>> 10 < 20 1 When you evaluate a logical expression, the answer is always true or false, which is matlab are represented as 1 and 0

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

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)

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)

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

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

A Golden Rule ( For all computer languages, not just Matlab) We should never test for equality or inequality with real numbers EDU>> 2.1 == 2.1 % 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….

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

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>>

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!

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)

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>>

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

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

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

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)