
Understanding Decision Structures in Programming
Explore the concept of decision structures in programming, allowing programs to execute different sequences of instructions based on specific conditions. Learn about one-way and two-way decisions, boolean operators, and how to enhance programs with temperature warnings using if statements and conditions.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
15-110: Principles of Computing Decision Structures- Part I Lecture 5, September 16, 2018 Mohammad Hammoud Carnegie Mellon University in Qatar
Today Last Session: Functions- Part II Today s Session: Decision Structures- Part I: One- and Two-way Decisions Booleans Relational, Logical, Membership, and Identity Operators
Decision Structures So far, we have mostly viewed computer programs as sequences of instructions that are interpreted one after the other Sequencing is a fundamental concept of programming, but alone it is not sufficient to solve every problem Often it is necessary to alter the sequential flow of a program to suit the needs of a particular situation We will study decision structures, which are statements that allow a program to execute different sequences of instructions for different cases
Example: Temperature Warnings Let us consider again our Celsius to Fahrenheit temperature program def main(): celsius = eval(input("What is the Celsius temperature? ")) fahrenheit = 9/5 * celsius + 32 print("The temperature is", fahrenheit, "degrees Fahrenheit") main() How can we enhance this program to print a suitable warning when the temperature is extreme (say, over 90 degrees F, it deserves a heat warning, and under 30 it deserves a cold warning)?
Example: Temperature Warnings Enhanced Celsius to Fahrenheit temperature program def main(): celsius = eval(input("What is the Celsius temperature? ")) fahrenheit = 9/5 * celsius + 32 print("The temperature is", fahrenheit, "degrees Fahrenheit") if fahrenheit > 90: print( Itis really hot outside. Be careful! ) if fahrenehit < 30: print( Brrrrr. Be sure to dress warmly! ) main()
One-Way Decisions We applied the Python if statement to implement a one-way decision, using the following form: One-way Condition Yes if <condition>: <body> <condition> true? No <body> is just a sequence of one or more statements indented under the if heading <statement> <statement> Simple <condition> compares values of two expressions using a comparison operator as follows: <statement> <expr> <comp> <expr>
Comparison Operators There are six comparison (or relational) operators in Python Python Mathematics Meaning < < Less than <= Less than or equal to == = Equal to >= Greater than or equal to > > Greater than != Not equal to
Conditions and Booleans Conditions may compare either numbers or strings Strings are compared lexicographically, meaning that they are compared character by character according to their Unicode values E.g., Bbbb is less than aaaa since B precedes a (all uppercase Latin letters come before lowercase equivalents) A condition is actually a Boolean expression, which produces a value of either True (the condition holds) or False (the condition does not hold)
Conditions and Booleans Here are few interactive examples: >>> "a" < "b" True >>> "aa" < "a" False >>> "a" < "A" False >>> "Hello" < "hello" True >>> >>> "a" == "a" True >>> type("a" == "a") <class 'bool'> >>> 3 < 4 True >>> 3 * 4 < 3 + 4 False >>>
Chaining Comparison Operators Comparison operators can be chained >>> 1 < 2 < 3 True >>> x = 2 >>> 1 < x < 2 False >>> 1 < x < 2 < 3 < 4 < 5 False >>> 1 < x <= 2 True >>> 3 < x < 2 False >>> 2 == x < 4 True >>> y = 4 >>> 1 < x <= 2 < y True >> 1 != 2 True
Logical Operators We can also use logical operators with our conditions Operator Operator Operator Description Description Description not x not x not x Evaluates to the opposite- i.e., if x is False, the result will be True and vice versa will be True and vice versa will be True and vice versa Evaluates to the opposite- i.e., if x is False, the result Evaluates to the opposite- i.e., if x is False, the result x and y x and y x and y Evaluates to True if both, x and y are True Evaluates to True if both, x and y are True Evaluates to True if both, x and y are True x or y x or y x or y Evaluates to True if either x is True or y is True Evaluates to True if either x is True or y is True Evaluates to True if either x is True or y is True
Logical Operators True or False? Statement Statement Statement Statement Statement Result Result Result Result Result (3*4 > 10) and (3+4 < 10) (3*4 > 10) and (3+4 < 10) (3*4 > 10) and (3+4 < 10) (3*4 > 10) and (3+4 < 10) (3*4 > 10) and (3+4 < 10) True True True True (3*4 < 10) or (3+4 < 10) (3*4 < 10) or (3+4 < 10) (3*4 < 10) or (3+4 < 10) (3*4 < 10) or (3+4 < 10) (3*4 < 10) or (3+4 < 10) True True True not ((3*4 < 10) or (3+4 < 10)) not ((3*4 < 10) or (3+4 < 10)) not ((3*4 < 10) or (3+4 < 10)) not ((3*4 < 10) or (3+4 < 10)) not ((3*4 < 10) or (3+4 < 10)) False False (3 * 4 > 10) or (5 + 5 > 10) and (4 * 4 > 15) or (5 + 4 > 10) (5 + 4 > 10) (5 + 4 > 10) (5 + 4 > 10) (5 + 4 > 10) (3 * 4 > 10) or (5 + 5 > 10) and (4 * 4 > 15) or (3 * 4 > 10) or (5 + 5 > 10) and (4 * 4 > 15) or (3 * 4 > 10) or (5 + 5 > 10) and (4 * 4 > 15) or (3 * 4 > 10) or (5 + 5 > 10) and (4 * 4 > 15) or True
Membership Operators We can also use membership operators with conditions Operator Operator Description Description x in sequence x in sequence Evaluates to True if x is found in the given sequence (e.g., string) (e.g., string) Evaluates to True if x is found in the given sequence x not in sequence sequence Evaluates to True if x is NOT found in the given sequence (e.g., string) sequence (e.g., string) x not in Evaluates to True if x is NOT found in the given
Membership Operators True or False? Statement Statement Statement Statement Statement Result Result Result Result Result 15-110 in 15-110 is a lot of fun! 15-110 in 15-110 is a lot of fun! 15-110 in 15-110 is a lot of fun! 15-110 in 15-110 is a lot of fun! 15-110 in 15-110 is a lot of fun! True True True True Java not in 15-110 uses Python to illustrate computing principles computing principles computing principles computing principles computing principles Java not in 15-110 uses Python to illustrate Java not in 15-110 uses Python to illustrate Java not in 15-110 uses Python to illustrate Java not in 15-110 uses Python to illustrate True True True 1 in 15-110 1 in 15-110 1 in 15-110 1 in 15-110 1 in 15-110 ERROR ERROR 1 in 15-110 1 in 15-110 1 in 15-110 1 in 15-110 1 in 15-110 True
Identity Operators We can also use identity operators with conditions Operator Operator Description Description x is y x is y Evaluates to True if x and y point to the same object Evaluates to True if x and y point to the same object x isnot y x isnot y Evaluates to True if x and y do not point to the same object Evaluates to True if x and y do not point to the same object
Membership Operators True or False? >>> x = "cmu" >>> y = "cmu" >>> x is y True >>> z = x >>> z is y True >>>
Membership Operators True or False? >>> x = "cmu" >>> y = "cmu" >>> x is y True >>> z = x >>> z is y True >>>
Membership Operators True or False? >>> x = "cmu" >>> y = "cmu" >>> x is y True >>> z = x >>> z is y True >>> >>> a = 5 >>> if (type(a) is int): ... print("true") ... else: ... print("false") ... true >>>
Membership Operators True or False? >>> x = "cmu" >>> y = "cmu" >>> x is y True >>> z = x >>> z is y True >>> >>> a = 5 >>> if (type(a) is int): ... print("true") ... else: ... print("false") ... true >>> >>> b = 5.4 >>> if (type(b) is not int): ... print("true") ... else: ... print("false") ... true >>>
Membership Operators True or False? >>> x = "cmu" >>> y = "cmu" >>> x is y True >>> z = x >>> z is y True >>> >>> a = 5 >>> if (type(a) is int): ... print("true") ... else: ... print("false") ... true >>> >>> b = 5.4 >>> if (type(b) is not int): ... print("true") ... else: ... print("false") ... true >>>
Two-Way Decisions Notice how we attached an else clause onto an if clause to come up with what we refer to as a two-way decision Yes No <condition> true? if <condition>: <statements> else: <statements> <statement> <statement> <statement> <statement> <statement> <statement>
Revisiting Our Quadratic Equation Solver #The following line will make the math library in Python available for us. import math def rootsQEq(): print("This program finds the real solutions to a quadratic.") print() a = eval(input("Enter the value of coefficient a: ")) b = eval(input("Enter the value of coefficient b: ")) c = eval(input("Enter the value of coefficient c: "))
Revisiting Our Quadratic Equation Solver #To call a function from the math library, we can use the #dot operator as follows: s_root_val = math.sqrt(b*b - 4 * a * c) root1 = (-b + s_root_val)/(2*a) root2 = (-b - s_root_val)/(2*a) print() print("The solutions are: ", root1, root2) #Call the function rootsQEq() rootsQEq()
Revisiting Our Quadratic Equation Solver A sample run: This program finds the real solutions to a quadratic. Enter the value of coefficient a: 1 Enter the value of coefficient b: 2 Enter the value of coefficient c: 3 Traceback (most recent call last): File "/Users/mhhammou/Desktop/CMU-Q/Courses/15- 110/Programs/Lecture4/RootsQE.py", line 22, in <module> rootsQEq() File "/Users/mhhammou/Desktop/CMU-Q/Courses/15- 110/Programs/Lecture4/RootsQE.py", line 14, in rootsQEq s_root_val = math.sqrt(b*b - 4 * a * c) ValueError: math domain error What is the problem?
Revisiting Our Quadratic Equation Solver The problem is that ?2 4?? < 0 The sqrt function is unable to compute the square root of a negative number How can we avoid this problem? We can first compute the discriminant ?2 4?? < 0 Then, we can check if it is negative (via using the if statement) If it is negative, we can print out that the equation has no real roots Otherwise (via using the else clause), we can compute the solutions and print them out
A Refined Quadratic Equation Solver import math def rootsQEq(): print("This program finds the real solutions to a quadratic.") print() a = eval(input("Enter the value of coefficient a: ")) b = eval(input("Enter the value of coefficient b: ")) c = eval(input("Enter the value of coefficient c: ")) discriminant = b * b 4 * a * c
A Refined Quadratic Equation Solver if discriminant < 0: print( The equation has no real roots! ) else: s_root_val = math.sqrt(discriminant) root1 = (-b + s_root_val)/(2*a) root2 = (-b - s_root_val)/(2*a) print( \nThe solutions are: ", root1, root2) rootsQEq()
Next Lecture Decision Structures- Part II