Decision Making Upsorn Praphamontripong CS 1110 Introduction to

  • Slides: 18
Download presentation
Decision Making Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016

Decision Making Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016

What is a Decision Statement? • A statement that evaluates some conditions to true

What is a Decision Statement? • A statement that evaluates some conditions to true or false. if condition: statement … • A condition (or expression) • Must always evaluate to true or false, i. e. , “Boolean expression” • Use relational operators: >, <, >=, <=, ==, != age 19 1 Part of a Mc. Donald’s commercial from 1998 CS 1110 2

Calculations that Evaluate to Boolean Values • < ≤ > ≥ all evaluate to

Calculations that Evaluate to Boolean Values • < ≤ > ≥ all evaluate to true or false 3 < 2 is false • ==, != also evaluate to true or false 3 == 3 is true 3 == 4 is false “jello” != “blue” is true CS 1110 age > 21 True False “Boolean value” 3

Decision Structure Simple structure if condition: statements 1 condition False CS 1110 True statements

Decision Structure Simple structure if condition: statements 1 condition False CS 1110 True statements 1 Dual Structure Indent the block of statement if condition: statements 1 else: statements 2 False statements 2 condition True statements 1 4

Two Types of Decisions Sequence Decision if condition: block of statements . . .

Two Types of Decisions Sequence Decision if condition: block of statements . . . else: block of statements . . . • Independent decisions do not remember the results of decisions made before them CS 1110 Nested Decision if condition: block of statements . . . elif: block of statements . . . else: block of statements . . . • Nested decisions remember the results of decisions made before them (in the same nesting) 5

if Statements in Python operation “subtraction” • if is a keyword, the if statement

if Statements in Python operation “subtraction” • if is a keyword, the if statement must end in a colon • What belongs to a particular if statement is indented CS 1110 6

elif Statements in Python operation “multiplication” • elif is a keyword; it stands for

elif Statements in Python operation “multiplication” • elif is a keyword; it stands for else if • elif is attached to an if or elif before it, and indicates this elif is nested • You cannot have a standalone elif CS 1110 7

if versus elif result operation “addition” • The on the left returns 6 •

if versus elif result operation “addition” • The on the left returns 6 • if-elif statements are nested, linked, and mutually exclusive. CS 1110 result 6 5 4 6 operation “addition” • The on the right returns 4 • The plain if statements are not mutually exclusive, don’t know about each other, and thus all if statements get 8

else statements int(input("Enter number 1: ")) int(input("Enter number 2: ")) • else is a

else statements int(input("Enter number 1: ")) int(input("Enter number 2: ")) • else is a keyword, linked to an if or elif, and get executed if the if/elif above it is false CS 1110 9

else statements (2) operation result “foo” operation undefined • else only gets executed if

else statements (2) operation result “foo” operation undefined • else only gets executed if none of the if or elif before it are true CS 1110 10

Indentation Matters num 1 01 num 2 13 result ( ) CS 1110 “num

Indentation Matters num 1 01 num 2 13 result ( ) CS 1110 “num 1 is 1 num 2 <= 3 finished num 1” “” “num 1 is 1 num 2 <= 3” “num 1 is 0” This is another type of “nesting”, and is usually referred to as “nested if-else statements” 11

Indentation Matters num 1 1 num 2 2 result ( ) CS 1110 “num

Indentation Matters num 1 1 num 2 2 result ( ) CS 1110 “num 1 is 1 num 2 <= 3 finished num 1" “” “num 1 is 1 num 2 <= 3" “num 1 is 1 " This is another type of “nesting”, and is usually referred to as “nested if-else statements” 12

Indentation Matters num 1 2 num 2 1 result ( ) CS 1110 “”

Indentation Matters num 1 2 num 2 1 result ( ) CS 1110 “” “num 1 is not 0 or 1” • Indentation – group things • if, elif, and else are mutually exclusive This is another type of “nesting”, and is usually referred to as “nested if-else statements” 13

Unreachable statements num 1 1 num 2 5 result “num 1 is 1 num

Unreachable statements num 1 1 num 2 5 result “num 1 is 1 num 2 > 3 finished num 1” “” “num 1 is 1 num 2 > 3” “num 1 is 1 ” print template(1, 5) CS 1110 14

Programming TRAP • Assignment statement x = “CS 1110” x CS 1110 • Boolean

Programming TRAP • Assignment statement x = “CS 1110” x CS 1110 • Boolean expression x == “CS 1110” CS 1110 15

Boolean Types • True and False are both keywords and types in Python •

Boolean Types • True and False are both keywords and types in Python • Capitalization !! • not is a keyword that negates a boolean value • The code above returns True CS 1110 16

Boolean Values and Calculations x y x and y x or y False False

Boolean Values and Calculations x y x and y x or y False False True False True False True True • A boolean value must evaluate to true or false • Two boolean values can be compared with and or or • Use parentheses if you want to combine and or or to disambiguate; e. g. , (x and y) or z or x and (y or z) x=F, y=T, z=T F T T F • You can use any logical operators: and or or or not CS 1110 17

Exercise • Write a program that accepts six project scores. • All scores are

Exercise • Write a program that accepts six project scores. • All scores are out of 100 points, except the first (which is out of 30), and the fifth (which is out of 50). • The program provides the highest average grade for all the projects by dropping the one with the lowest percentage. • For example, if a student got a 65% on the first project (out of 30) and a 55% on the fifth project (out of 50), that fifth project would be dropped from the average calculation. • Do not use built-in functions/methods such as max() or sort() CS 1110 18