19 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

15 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Making Decisions In Python
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Course A201: Introduction to Programming 09/16/2010.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
29 September 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Copyright © 2013 by John Wiley & Sons. All rights reserved. DECISIONS CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 15,
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
Input Validation 10/09/13. Input Validation with if Statements You, the C++ programmer, doing Quality Assurance (by hand!) C++ for Everyone by Cay Horstmann.
3 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
8 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
22 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
ITP © Ron Poet Lecture 5 1 Branching. ITP © Ron Poet Lecture 5 2 CookTime After Midnight  We want to improve our program so that we can cook meals after.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Decision Making CMSC 201 Chang (rev ).
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
26 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
4 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming Python Lab 8: Loops 26 February PythonLab8 lecture slides.ppt Ping Brennan
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Introduction to Programming
Control Structures I Chapter 3
Introduction to Computer Systems
Introduction to Programming
Introduction to Programming
Introduction to Programming
Making Choices with if Statements
Introduction to Programming
Introduction to Programming
Topics The if Statement The if-else Statement Comparing Strings
Introduction to Programming
Introduction to Programming
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Programming
DECISIONS.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
Introduction to Programming
Adding Intelligence Check for the presence or absence of a condition in the environment Take the appropriate actions Involves making a choice (to do or.
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Presentation transcript:

19 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems Spring 2016 Week 7: if Statement

Revision: Relational Operators  5 > 4 # True  5 >= 7 # False  "9032" < "0AB" # False  "ABC" <= "AB" # False  48 == # True  "AB" != "ABC" # True 19 February 2016PFE Section 3.22

Revision: Lexicographic Ordering 19 February 2016PFE Section 3.23 ali g n ed alig h t What happens if one of the strings s1, s2 is a prefix of the other?

Revision: Boolean Operators  The Boolean Operators in Python are and, or and not  and, or and not are used to make new Boolean Operators  5 == 0 and 6 == 0 # False  5 == 0 or 5 > 4 # True  not(5 == 0) # True 19 February 2016PFE Section 3.74

Revision: Short Circuit Evaluation  Boolean statements are evaluated left to right. Evaluation stops as soon as the truth value of the statement is determined.  Example quantity = 0 if(quantity == 0 or price/quantity < 10) : print("bargain") 19 February 2016PFE Section 3.75

if Statement  Example – there is no 13 th floor! actualFloor = 0 # define variable if floor > 13 : actualFloor = floor-1 else : actualFloor = floor 19 February 2016PFE Section 3.16

Alternative if Statement actualFloor = floor if floor > 13 : actualFloor = actualFloor-1 19 February 2016PFE Section 3.17

Flow Chart 19 February 2016PFE Section 3.18 floor > 13? actualFloor = floor-1 actualFloor = floor True False

Parts of an if Statement if floor > 13 : # The condition floor > 13 is True or False actualFloor = floor-1 # execute only if the condition is True else : actualFloor = floor # execute only if the condition is False # Align if and else # Indent the statements in each branch 19 February 2016PFE Section 3.19

Syntax of if Statements  Version 1 if condition : statements  Version 2 if condition : statements_1 else : statements_2 # The colons indicate compound statements 19 February 2016PFE Section 3.110

Compound Statement  A compound statement contains a header followed by a statement block. Example: if totalSales > : # Colon indicates the header discount = totalSales*0.05 # Block of statements totalSales = totalSales-discount print("You received a discount of", discount) # All the statements in the block have the same indentation 19 February 2016PFE Section 3.111

Avoid Duplication  Avoid if floor > 13 : actualFloor = floor-1 print("Actual floor:", actualFloor) else : actualFloor = floor print("Actual floor:", actualFloor)  and prefer if floor > 13 : actualFloor = floor-1 else : actualFloor = floor print("Actual floor:", actualFloor) 19 February 2016PFE Section 3.112

Example 1  The university bookstore has a Kilobyte Day sale every October 24, giving an 8% discount on all computer accessory purchases if the price is less than $128, and a 16 percent discount if the price is at least $128. Write a program that asks the cashier for the original price and then prints the discounted price. 19 February 2016PFE Section 3.213

Shipping Costs Example  Shipping costs are $5 inside the USA except that to Hawaii and Alaska they are $10. International shipping costs are $10.  First design: use a single if statement to distinguish between the $5 cost and the $10 cost if(country!="USA" or (country=="USA" and (state = "AK" or state = "HI"))) : ShippingCost = 10 else : ShippingCost = 5 19 February 2016PFE Section 3.214

Second Design  Shipping costs are $5 inside the USA except that to Hawaii and Alaska they are $10. International shipping costs are $10.  Separate the three branches: i) inside the USA and in Hawaii or Alaska; ii) inside the USA and not in Hawaii or Alaska; iii) outside the USA. if country == "USA" : if state == "AK" or state == "HI" shippingCost = 10 else : shippingCost = 5 else : shippingCost = February 2016PFE Section 3.515

Richter Scale 19 February 2016PFE Section Value Effect 8Most structures fall 7Many buildings destroyed 6 Many buildings considerably damaged, some collapse 4.5Damage to poorly constructed buildings

The elif Statement if richter >= 8.0 : print("Most structures fall") elif richter >= 7.0 : print("Many buildings destroyed") elif richter >= 6.0 : print("Many buildings considerably damaged, some collapse") elif richter >= 4.5 : print("Damage to poorly constructed buildings") else : print("No destruction of buildings") 19 February 2016PFE Section 3.417

Questions  What happens if the order of the tests is reversed?  What happens if each elif statement is replaced by an if statement? 19 February 2016PFE Section 3.418

Example  In a game program, the scores of players A and B are stored in variables scoreA and scoreB. Assuming that the player with the larger score wins, write an if/elif sequence that prints out "A won", "B won" or "Game tied". 19 February 2016PFE Section 3.419

Input Validation  Check user supplied input to see if it has the correct form.  Example: in the elevator simulation let maxFloor be the largest floor number. The following inputs are illegal. i) 13 ii) 0 or a negative number (in the USA) iii) any number > maxFloor iv) Any input not a sequence of digits 19 February 2016PFE Section 3.920

Error Messages if floor == 13 : print("Error: there is no 13th floor") if floor maxFloor print("Error: the floor must be between 1 and", maxFloor) floor = int(input("Floor: ")) # if the input is non digital then there is a run time exception # and the program terminates. 19 February 2016PFE Section 3.921

Example Code floor = int(input("Floor: ")) if floor == 13 : print("Error: there is no 13th floor") elif floor maxFloor print("Error: the floor must be between 0 and", maxFloor) else : actualFloor = floor if floor > 13 : actualFloor = actualFloor-1 19 February 2016PFE Section 3.922