Control flow : if statements

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

This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Python Programming Language
Control Flow Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Conditional Statements Introduction to Computing Science and Programming I.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 7: Program flow control.
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
General Programming Introduction to Computing Science and Programming I.
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.
If statements while loop for loop
Topics: IF If statements Else clauses. IF Statement For the conditional expression, evaluating to True or False, the simple IF statement is if : x = 7.
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.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Python Conditionals chapter 5
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits:
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
Introduction to Computing Concepts Note Set 14. What if… You had to print “I love Java” to the screen 125 times. How? 125 lines of ▫ System.out.println(“I.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Control flow Ruth Anderson UW CSE 160 Spring
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control flow Ruth Anderson UW CSE 160 Winter
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
 Type Called bool  Bool has only two possible values: True and False.
Structured Programming The Basics
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Control Flow (Python) Dr. José M. Reyes Álamo.
Python Review 1.
Introduction to Computing Science and Programming I
COMP 14 Introduction to Programming
Introduction to Python
Making Choices with if Statements
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
CS1371 Introduction to Computing for Engineers
If, else, elif.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Conditional Execution
Ruth Anderson UW CSE 160 Spring 2018
Ruth Anderson UW CSE 160 Winter 2017
Conditionals (if-then-else)
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Selection CIS 40 – Introduction to Programming in Python
Python - Conditional Execution
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Conditional Execution
Types, Truth, and Expressions (Part 2)
Ruth Anderson UW CSE 140 Winter 2014
CSE 1020:Control Structure
Michael Ernst UW CSE 140 Winter 2013
Conditional Execution
Introduction to Programming Using Python PART 2
CS 1111 Introduction to Programming Spring 2019
Conditional Logic Presentation Name Course Name
15-110: Principles of Computing
Python Programming Language
By Ryan Christen Errors and Exceptions.
Control Flow statements
Michael Ernst UW CSE 190p Summer 2012
EE 194/BIO 196: Modeling biological systems
Introduction to Python
Control 9 / 25 / 19.
Lecture 6 - Recursion.
Presentation transcript:

Control flow : if statements Ruth Anderson UW CSE 160 Spring 2018

Making decisions How do we compute absolute value? Absolute value of 5 is Absolute value of 0 is Absolute value of -22 is If the value is negative, negate it. Otherwise, use the original value.

Absolute value solution See in python tutor If the value is negative, negate it. Otherwise, use the original value. Condition must be a Boolean expression val = -10 # calculate absolute value of val if val < 0: result = -val else: result = val print result Indentation is significant else is not required In this example, result will always be assigned a value.

Absolute value solution See in python tutor If the value is negative, negate it. Otherwise, use the original value. Another approach that does the same thing without using result: val = -10 # calculate absolute value of val if val < 0: result = -val else: result = val print result val = -10 if val < 0: print -val else: print val In this example, result will always be assigned a value.

Absolute value solution See in python tutor As with loops, a sequence of statements could be used in place of a single statement: val = -10 # calculate absolute value of val if val < 0: result = -val print "val is negative!" print "I had to do extra work!" else: result = val print "val is positive" print result

Absolute value solution with zero See in python tutor val = 0 # calculate absolute value of val if val < 0: print "val is negative" print val result = -val elif val == 0: print "val is zero" result = val else: print "val is positive" print result

Another absolute value solution See in python tutor What happens here? val = 5 # calculate absolute value of val if val < 0: result = -val print "val is negative!" else: for i in range(val): print "val is positive!" result = val print result Answer: >>> val is positive! 5

Another if It is not required that anything happens… See in python tutor It is not required that anything happens… val = -10 if val < 0: print "negative value!" Answer: nothing! What happens when val = 5?

The if body can be any statements # height is in km if height > 100: print "space" else: if height > 50: print "mesosphere" if height > 20: print "stratosphere" print "troposphere" # height is in km if height > 100: print "space" elif height > 50: print "mesosphere" elif height > 20: print "stratosphere" else: print "troposphere" # height is in km if height > 50: if height > 100: print "space" else: print "mesosphere" if height > 20: print "stratosphere" print "troposphere" then clause Execution gets here only if “height > 100” is false Execution gets here only if “height > 100” is false AND “height > 50” is true t else clause Question: why don’t we ever accidentally print “mesosphere” when height is 220? “height > 50” is true in that case. t e e troposphere stratosphere mesosphere space km above earth 10 20 30 40 50 60 70 80 90 100

Version 1 # height is in km if height > 100: print "space" else: if height > 50: print "mesosphere" if height > 20: print "stratosphere" print "troposphere" then clause Execution gets here only if “height <= 100” is true Execution gets here only if “height <= 100” is true AND “height > 50” is true t else clause Question: why don’t we ever accidentally print “mesosphere” when height is 220? “height > 50” is true in that case. t e e troposphere stratosphere mesosphere space km above earth 10 20 30 40 50 60 70 80 90 100

Version 1 See in python tutor # height is in km if height > 100: print "space" else: if height > 50: print "mesosphere" if height > 20: print "stratosphere" print "troposphere" All three versions (1, 2 and 3) are equivalent. troposphere stratosphere mesosphere space km above earth 10 20 30 40 50 60 70 80 90 100

Version 2 if height > 50: if height > 100: print "space" else: See in python tutor if height > 50: if height > 100: print "space" else: print "mesosphere" if height > 20: print "stratosphere" print "troposphere" All three versions (1, 2 and 3) are equivalent. troposphere stratosphere mesosphere space km above earth 10 20 30 40 50 60 70 80 90 100

Version 3 (Best) See in python tutor if height > 100: print "space" elif height > 50: print "mesosphere" elif height > 20: print "stratosphere" else: print "troposphere" All three versions (1, 2 and 3) are equivalent. ONE of the print statements is guaranteed to execute: whichever condition it encounters first that is true troposphere stratosphere mesosphere space km above earth 10 20 30 40 50 60 70 80 90 100

Order Matters Try height = 72 on both versions, what happens? See in python tutor # version 3 if height > 100: print "space" elif height > 50: print "mesosphere" elif height > 20: print "stratosphere" else: print "troposphere" # broken version 3 if height > 20: print "stratosphere" elif height > 50: print "mesosphere" elif height > 100: print "space" else: print "troposphere" Version 3: mesosphere, broken version 3: stratosphere Try height = 72 on both versions, what happens? troposphere stratosphere mesosphere space km above earth 10 20 30 40 50 60 70 80 90 100

Incomplete Version 3 See in python tutor # incomplete version 3 if height > 100: print "space" elif height > 50: print "mesosphere" elif height > 20: print "stratosphere" A: when height is <= 20 nothing is printed (troposphere is never printed). In this case it is possible that nothing is printed at all, when? troposphere stratosphere mesosphere space km above earth 10 20 30 40 50 60 70 80 90 100

What Happens Here? Try height = 72 See in python tutor # height is in km if height > 100: print "space" if height > 50: print "mesosphere" if height > 20: print "stratosphere" else: print "troposphere" Answer: space, mesosphere, and stratosphere are all printed (each on its own line). Try height = 72 troposphere stratosphere mesosphere space km above earth 10 20 30 40 50 60 70 80 90 100

The then clause or the else clause is executed See in python tutor speed = 54 limit = 55 if speed <= limit: print "Good job!" else: print "You owe $", speed/fine Try this code! First problem: if speed is > limit, then: You owe $ Traceback (most recent call last): File "Z:/rea-python-examples/L03-7.py", line 6, in <module> print "You owe $", speed/fine NameError: name 'fine' is not defined If speed is > limit, and fine = 0, then: File "Z:/rea-python-examples/L03-7.py", line 7, in <module> ZeroDivisionError: integer division or modulo by zero What if we change speed to 64?