CSIT881 Programming and Data Structures Decision Making Statements Decision Making Statements Objectives: ● Understanding the usage of Decision Making Statements ● Solving problems using: ○ Single-selection statement if ○ Double-selection statement if-else ○ Multiple-selection statement if-elif-…-else 2 Hello World example 3 # ask user to choose a language print(“Choose a language: (I)talian (W)elsh (Z)ulu”) language_option = input(“Enter language selection: “) # display Hello World, How are you, in different languages if (language_option == “I”): print(“Ciao mondo.”) print(“Come stai “) elif (language_option == “W”): print(“Helo Byd.”) print(“Sut wyt ti “) elif (language_option == “Z”): print(“Sawubona Mhlaba.”) print(“Unjani “) else: print(“Hello World.”) print(“How are you “) Choose a language: (I)talian (W)elsh (Z)ulu Enter language selection: W Helo Byd. Sut wyt ti Hello World example 4 if (language_option == “I”): #{ print(“Ciao mondo.”) print(“Come stai “) #} elif (language_option == “W”): #{ print(“Helo Byd.”) print(“Sut wyt ti “) #} elif (language_option == “Z”): #{ print(“Sawubona Mhlaba.”) print(“Unjani “) #} else: #{ print(“Hello World.”) print(“How are you “) #} If you are coming from C and Java background, perhaps using the comments with curly brackets like this may help you to see the beginning and the end of your code block better! if – else 5 Double-selection statement if-else is the most common decision making statement. We use this control structure to specify two different actions in our program. We will need to provide a condition. If this condition is true then our program will perform a certain action. And if this condition is false then another action will be taken. if – else if (some condition): block 1 statements … else: block 2 statements … 6 Example 1 Number of items Cost 1-50 $3 per item Postage: $10 More than 50 $2 per item Postage: free 7 Example 1 Number of items Cost 1-50 $3 per item Postage: $10 More than 50 $2 per item Postage: free If the user buys 10 item: Item cost = $3 x 10 = $30 Postage: $10 Total cost: $40 8 Example 1 Number of items Cost 1-50 $3 per item Postage: $10 More than 50 $2 per item Postage: free If the user buys 100 item: Item cost = $2 x 100 = $200 Postage: free Total cost: $200 9 # get the number of items from the user item_input = input(“Enter the quantity: “) item_count = int(item_input) # calculate the cost if (item_count <= 50): else: Example 1 10 Example 1 # get the number of items from the user item_input = input("Enter the quantity: ") item_count = int(item_input) # calculate the cost if (item_count <= 50): unit_price = 3 postage = 10 total_cost = unit_price * item_count + postage print("Total cost: ${0}".format(total_cost)) else: 11 Example 1 # get the number of items from the user item_input = input("Enter the quantity: ") item_count = int(item_input) # calculate the cost if (item_count <= 50): unit_price = 3 postage = 10 total_cost = unit_price * item_count + postage print("Total cost: ${0}".format(total_cost)) else: unit_price = 2 total_cost = unit_price * item_count print("Total cost: ${0}".format(total_cost)) 12 # get the number of items from the user item_input = input("Enter the quantity: ") item_count = int(item_input) # calculate the cost if (item_count <= 50): #{ unit_price = 3 postage = 10 total_cost = unit_price * item_count + postage print("Total cost: ${0}".format(total_cost)) #} else: #{ unit_price = 2 total_cost = unit_price * item_count print("Total cost: ${0}".format(total_cost)) #} Example 1 13 if - elif - elif - … - else 14 Multiple-selection statement if-elif-...-else is to be used when we have different actions in our code to handle different conditions. In each of the if and elif control structures, we will need to provide a condition. The last control structure else is optional. If else is used then the block of codes that belong to else will be executed if all the previous conditions (of if and elif) are false. if - elif - elif - … - else if (condition1): # condition1 is true statement statement ... elif (condition2): # condition1 is false and condition2 is true statement statement ... elif (condition3): # condition1 is false, condition2 is false, and condition3 is true statement statement ... else: # all the conditions are false statement statement ... 15 Example 2 Number of items Cost 1-50 $3 per item Postage: Standard post: $10 Registered post: $15 Express post: $20 More than 50 $2 per item Postage: Standard post: free Registered post: $15 Express post: $20 10 items + Registered Post Item cost = $3 x 10 = $30 Postage: $15 Total cost: $45 16 Example 2 Number of items Cost 1-50 $3 per item Postage: Standard post: $10 Registered post: $15 Express post: $20 More than 50 $2 per item Postage: Standard post: free Registered post: $15 Express post: $20 100 items + Registered Post Item cost = $2 x 100 = $200 Postage: $15 Total cost: $215 17 Example 2 Number of items Cost 1-50 $3 per item Postage: Standard post: $10 Registered post: $15 Express post: $20 More than 50 $2 per item Postage: Standard post: free Registered post: $15 Express post: $20 100 items + Standard Post Item cost = $2 x 100 = $200 Postage: free Total cost: $200 18 # get the number of items from the user item_input = input("Enter the quantity: ") item_count = int(item_input) # get the shipping method Standard/Registered/Express shipping = input("Shipping method (s/r/e): ") # calculate the cost Example 2 19 # calculate the cost # determine the unit price # determine the postage # determine the total cost Example 2 20 # determine the unit price if (item_count <= 50): unit_price = 3 else: unit_price = 2 Example 2 21 # determine the postage if (shipping == "s"): # standard post elif (shipping == "r"): # registered post else: # express post Example 2 22 # determine the postage if (shipping == "s"): # standard post $10 for 1-50 items, free for > 50 items if (item_count <= 50): postage = 10 else: postage = 0 elif (shipping == "r"): # registered post else: # express post Example 2 23 # determine the postage if (shipping == "s"): # standard post $10 for 1-50 items, free for > 50 items if (item_count <= 50): postage = 10 else: postage = 0 elif (shipping == "r"): # registered post $15 postage = 15 else: # express post Example 2 24 # determine the postage if (shipping == "s"): # standard post $10 for 1-50 items, free for > 50 items if (item_count <= 50): postage = 10 else: postage = 0 elif (shipping == "r"): # registered post $15 postage = 15 else: # express post $20 postage = 20 Example 2 25 # determine the total cost total_cost = unit_price * item_count + postage print("Total cost: ${0}".format(total_cost)) Example 2 26 # grade A: 100-80, B: 79-60, C: 59-40, D: 39-0 # ask user to enter the mark # determine the grade based on mark # display the mark and grade Example 3 27 # grade A: 100-80, B: 79-60, C: 59-40, D: 39-0 # ask user to enter the mark mark_input = input("Please enter mark: ") mark = int(mark_input) # determine the grade based on mark # display the mark and grade Examp
le 3 28 # grade A: 100-80, B: 79-60, C: 59-40, D: 39-0 # determine the grade based on mark if (mark >= 80): grade = “A” elif (mark >= 60): grade = “B” elif (mark >= 40): grade = “C” else: grade = “D” Example 3 29 # display the mark and grade print(“Mark {0}, Grade {1}”.format(mark, grade)) Example 3 30 #grade A: 100-80, B: 79-60, C: 59-40, D: 39-0 mark_input = input(“Please enter mark: “) mark = int(mark_input) if (mark >= 80): grade = “A” elif (mark >= 60): grade = “B” elif (mark >= 40): grade = “C” else: grade = “D” print(“Mark {0}, Grade {1}”.format(mark, grade)) Please enter mark: 90 Mark 90, Grade A mark is greater than or equal to 80 31 #grade A: 100-80, B: 79-60, C: 59-40, D: 39-0 mark_input = input(“Please enter mark: “) mark = int(mark_input) if (mark >= 80): grade = “A” elif (mark >= 60): grade = “B” elif (mark >= 40): grade = “C” else: grade = “D” print(“Mark {0}, Grade {1}”.format(mark, grade)) Please enter mark: 62 Mark 62, Grade B 32 #grade A: 100-80, B: 79-60, C: 59-40, D: 39-0 mark_input = input(“Please enter mark: “) mark = int(mark_input) if (mark >= 80): grade = “A” elif (mark >= 60): grade = “B” elif (mark >= 40): grade = “C” else: grade = “D” print(“Mark {0}, Grade {1}”.format(mark, grade)) Please enter mark: 45 Mark 45, Grade C 33 #grade A: 100-80, B: 79-60, C: 59-40, D: 39-0 mark_input = input(“Please enter mark: “) mark = int(mark_input) if (mark >= 80): grade = “A” elif (mark >= 60): grade = “B” elif (mark >= 40): grade = “C” else: grade = “D” print(“Mark {0}, Grade {1}”.format(mark, grade)) Please enter mark: 15 Mark 15, Grade D 34 if (alone) if (some condition): statements … 35 The single-selection statement if is to be used when we want to perform an action if a condition is true or skip the action when the condition is false. 36 Example 4 Finding the maximum number among 3 given numbers user_input = input(“Enter the 1st integer: “) number1 = int(user_input) user_input = input(“Enter the 2nd integer: “) number2 = int(user_input) user_input = input(“Enter the 3rd integer: “) number3 = int(user_input) number_max = number1 if (number2 > number_max): number_max = number2 if (number3 > number_max): number_max = number3 print(“Max of {0}, {1}, {2} is {3}” .format(number1, number2, number3, number_max)) 37 Enter the 1st integer: 12 Enter the 2nd integer: 3 Enter the 3rd integer: 5 Max of 12, 3, 5 is 12 number1 12 number2 3 senario 1 38 number3 5 number_max 12 user_input = input(“Enter the 1st integer: “) number1 = int(user_input) user_input = input(“Enter the 2nd integer: “) number2 = int(user_input) user_input = input(“Enter the 3rd integer: “) number3 = int(user_input) number_max = number1 if (number2 > number_max): number_max = number2 if (number3 > number_max): number_max = number3 print(“Max of {0}, {1}, {2} is {3}” .format(number1, number2, number3, number_max)) Enter the 1st integer: 5 Enter the 2nd integer: 12 Enter the 3rd integer: 3 Max of 5, 12, 3 is 12 senario 2 39 number1 5 number2 12 number3 3 number_max 5 number_max 12 user_input = input(“Enter the 1st integer: “) number1 = int(user_input) user_input = input(“Enter the 2nd integer: “) number2 = int(user_input) user_input = input(“Enter the 3rd integer: “) number3 = int(user_input) number_max = number1 if (number2 > number_max): number_max = number2 if (number3 > number_max): number_max = number3 print(“Max of {0}, {1}, {2} is {3}” .format(number1, number2, number3, number_max)) senario 3 40 number1 5 number2 3 number3 12 number_max 5 number_max 12 Enter the 1st integer: 5 Enter the 2nd integer: 3 Enter the 3rd integer: 12 Max of 5, 3, 12 is 12 user_input = input(“Enter the 1st integer: “) number1 = int(user_input) user_input = input(“Enter the 2nd integer: “) number2 = int(user_input) user_input = input(“Enter the 3rd integer: “) number3 = int(user_input) number_max = number1 if (number2 > number_max): number_max = number2 if (number3 > number_max): number_max = number3 print(“Max of {0}, {1}, {2} is {3}” .format(number1, number2, number3, number_max)) senario 4 41 number1 3 number2 5 number3 12 number_max 3 number_max 5 number_max 12 Enter the 1st integer: 3 Enter the 2nd integer: 5 Enter the 3rd integer: 12 Max of 3, 5, 12 is 12 user_input = input(“Enter the 1st integer: “) number1 = int(user_input) user_input = input(“Enter the 2nd integer: “) number2 = int(user_input) user_input = input(“Enter the 3rd integer: “) number3 = int(user_input) number_max = number1 if (number2 > number_max): number_max = number2 if (number3 > number_max): number_max = number3 print(“Max of {0}, {1}, {2} is {3}” .format(number1, number2, number3, number_max)) Equality if (number1 == 5): # number1 is equal to 5 if (number1 == number2): # number1 is equal to number2 if (your_answer == “Y”): # your_answer is equal to “Y” if (student_name == “John”): # student_name is equal to “John” Remember the double equal sign == 42 Inequality if (number1 != 5): # number1 is not equal to 5 if (number1 != number2): # number1 is not equal to number2 if (your_answer != “Y”): # your_answer is not equal to “Y” if (student_name != “John”): # student_name is not equal to “John” 43 Comparison if (number1 < 5): # number1 is less than 5 if (number1 <= 5): # number1 is less than or equal to 5 if (number1 > 5): # number1 is greater than 5 if (number1 >= 5): # number1 is greater than or equal to 5 44 Logical And if ((number1 > 5) and (number1 < 10)): # number1 is greater than 5 AND less than 10 if ((age > 40) and (student_type == “Domestic”)): # age is greater than 40 # AND student_type is equal to “Domestic” 45 Logical Or if ((number1 < 1000) or (number1 > 5000)): # number1 is less than 1000 # OR greater than 5000 if ((student_type == “Exchange”) or (student_type == “Domestic”)): # student_type is equal to “Exchange” # OR is equal to “Domestic” 46 Logical Negation if (not (number1 == 1000)): # number1 is not equal to 1000 if (condition): this is a block of codes that is indented by the same amount of spaces else: usually we use 2, 3 or 4 spaces for indentation In Python, all the continuous lines indented with same number of spaces form a block. All statements within the block must be indented the same amount. We usually use 2, 3 or 4 spaces for indentation. 47 Block and indentation if (item_count <= 50): #{ unit_price = 3 postage = 10 total_cost = unit_price * item_count + postage print("Total cost: ${0}".format(total_cost)) #} else: #{ unit_price = 2 total_cost = unit_price * item_count print("Total cost: ${0}".format(total_cost)) #} 48 Block and indentation If you are coming from C, C++, Java, etc... background, perhaps using the above coding style will help you! if (condition): this is a block of codes that is indented by the same amount of spaces else: usually we use 2, 3 or 4 spaces for indentation Common mistakes Forget the colon : Wrong indentation, mix-up between spaces and tabs Make your choice of indentation and use it consistently! 49