Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming...

24
Decision Structures and Boolean Variables

Transcript of Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming...

Page 1: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Decision Structures and Boolean Variables

Page 2: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Sequence Structures

• Thus far, we’ve been programming “sequence structures”

• That just means that the program will execute the statements in the order in which they appear in the source code

• We would now like for programs to deviate from the linear structure to adapt according to conditions being met, or not met

Page 3: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Calculating Overtime

• For instance, if an employee works more than 40 hours in a week, he/she is entitled to overtime pay

• Overtime pay is calculated at the rate of 1.5 times the normal hourly rate

• The additional rate is only applied to the hours worked above 40 hours

Page 4: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Calculating Overtime

• Input: hourly rate of pay

• Input: number of hours worked in a week

• If hours worked is less than 40, simply multiply hours worked by hourly rate

• If hours worked is greater than 40: • Multiply hourly rate by 40

• Subtract 40 from hours worked

• Multiply overtime hours by 1.5 times hourly rate

• Add overtime pay to base pay

Page 5: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Calculating Overtime

• Currently, we don’t know enough on Python to deviate from a linear sequence structure

• We cannot account for when the user inputs a total amount of hours worked that is above 40 to pay for overtime

Page 6: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

The Selection Statement

• Allows program to “ask a question” and respond accordingly

• Simplest form: perform an action only if a certain condition is met

• If the condition is not met, then the action is not performed (we will learn how to accommodate for non-met conditions with a separate action later on)

Page 7: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

The Selection Statement

• In this program, we start by asking a question, “is it cold outside?”

• If the answer is yes (aka “True”) then we execute an alternate set of commands

• Otherwise, we continue with the program as-is

Page 8: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

The Selection Statement

• Python will read your inputs of “yes” and “no” as holding the values of “True” or “False”

Page 9: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

The Selection Statement

• In Python, we use the keyword “if” to start a selection statement

• We must also use colons to end the selection statement

• The block of code reserved to execute if and only if the condition is met, must be indented within the selection statement (nested structure)

Page 10: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Page 11: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Boolean Expressions

WRITING A CONDITION

Page 12: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Writing a condition

• Python doesn’t read the way you would, so you can’t write your conditions as questions like, “Is it cold outside?”

• The key is to write a selection statement in a way Python might understand and that matches the question you are trying to ask

• All selection statements must have a condition to be “tested” as either True or False

• Think about asking questions that can be answered using “yes” or “no”

Page 13: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Boolean Expressions

Page 14: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Boolean Expressions• Named after George Boole, a 19th

century English philosopher and mathematician

• Boole developed a system of mathematics that allows us to work with the concepts of “True” or “False”

• Boole is considered one of the founders of modern computer science, as his work is reflected in the way computers process binary data

Page 15: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Boolean Expressions

• Boolean expressions can be used as the condition of an “if” selection statement

• They are generally formed using “relational operators” which allow you to test whether a specific relationship exists between two (or more) values

Page 16: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Relational Operators

•A > B # A is greater than B

•A < B # A is less than B

•A == B # A is equal to B

•A >= B # A is greater than OR equal to B

•A <= B # A is less than OR equal to B

Page 17: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Boolean Expressions

• All Boolean expressions in Python, we say “evaluate” to either “True” or “False”

Page 18: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Writing a Boolean Expression

sticks_stones = 10

words = 7

if sticks_stones > words: # sticks_stones > words

print( “Sticks and stones # 10 > 7

may break my bones, but# True, condition met

words will never hurt me” )

Page 19: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Practice

# given these variables # evaluate

A = 99 A > B

B = 7 B < C

C = -5 B >= C

D = 92 C <= D

A == B + D

D <= A + C

C != B

Page 20: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Practice

# given these variables # evaluate

A = 99 A > B True

B = 7 B < C False

C = -5 B >= C True

D = 92 C <= D True

A == B + D True

D <= A + C True

C != B True

Page 21: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

More Boolean Operators

•Don’t confuse “==“ with “=“: • “=“ is used to assign variables • “==“ is used to test if two values are equivalent

•We use “!=“ to test if two values are different (“not equal to”)

•“<=“ and “>=“ test for more than one relationship at a time

Page 22: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Challenge: Guess the Magic Number

•Write a program that sets a magic number, anywhere from 1-10.

• Then, ask the user to guess a number.

• If they guess correctly, then print out “You guessed correctly, the number was __!”

• If not, tell them, “Sorry, the magic number was __.”

Page 23: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Challenge: Overtime Pay

•Write a program that asks the user for an hourly rate of pay and the number of hours they’ve worked

• If the number of hours worked is over 40, then calculate for overtime pay at 1.5 the original rate

• Then show the user how much money they made for the week

Page 24: Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Challenge: Overtime Pay

•HINT: you may want to set a variable for the overtime pay and set it equal to zero, before the selection statement

• This way, the overtime pay is only changed if the condition is met

• You may want to do the same with the base