45
COMP1405 Dr. Andrew Runka Carleton University 1 Branches and Decision Making

Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

COMP1405Dr. Andrew RunkaCarleton University

1

Branches and Decision Making

Page 2: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Flow of Execution

▪ Computers operate by performing sequences of instructions

▪ Order of instructions is important

Linear Flow

▪ Step-by-step, top-to-bottom

▪ Predictable

2

Start

Measure

Mix

Bake

Cake!

Page 3: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Branching Flow▪ Some instructions only executed

under certain conditions

▪ A condition is a True or False expression▪ Like a yes/no question

▪ Requires a conditional statement:▪ if statements

▪ if-else statements

▪ if-elif-else statements

3

Page 4: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

It is raining

Bring an umbrella

4

Page 5: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

5

Grade is at least 50

Pass the class

Page 6: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

6

There is a traffic jam

Stop driving

Page 7: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

7

<some condition>

<some action>

True or False,

aka, a Boolean expression

Page 8: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Compare numbers (or text) to get True/Falsea < b Less thana > b Greater thana <= b Less than or equala >= b Greater than or equala == b Equalitya != b Inequality

Important: == vs. =

8

Page 9: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

if weather == "rainy":

print("Bring an umbrella")

if grade >= 50:

print("Congratulations!")

9

Page 10: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Here is the syntax for if-statements in Python:

10

if <condition>:

<body>

Page 11: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

if <condition>:

<body>

11

Keyword if

A Boolean expression

Any instructions to run if the condition is true

Page 12: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

if x:

print('A')

print('B')

print('C')

12

A

B

X?True

False

Indentation is important!

C

Page 13: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

if x:

print('A')

print('B')

print('C')

13

A

B

X?True

False

Indentation is important!

C

Page 14: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

14

a = int(input("A. Enter a number: "))

b = int(input("B. Enter a number: "))

if a > b:

print("A is greater")

print("Have a nice day!")

A is greater

Have a nice day!

a>bTrue

Inputs

False

Page 15: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

15

It is a weekend

I will watch cartoons

I will go to work

Page 16: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

It is before noon

I will eat pancakes

I will eat pizza

16

Page 17: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

if today != weekday:

print("Watch cartoons")

else:

print("Go to work")

if time < noon:

print("Pancakes!")

else:

print("Pizza!")

17

Page 18: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Here is the syntax for if-else statements in Python:

if <condition>:

<then-body>

else:

<else-body>

18

Page 19: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

19

a = int(input("A. Enter a number: "))

b = int(input("B. Enter a number: "))

if a > b:

print("A is greater")

else:

print("B is greater")

print("Have a nice day!")

B is greater A is greater

Have a nice day!

a>b TrueFalse

Page 20: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Write a program that asks the user for their age. If they enter a number of 19 or

over, it says they can enter the bar, otherwise, it says they cannot.

20

Page 21: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

if <condition>:

<then-body>

else:

<else-body>

21

Page 22: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

if <condition1>:

if <condition2>:

<body1>

else:

<body2>

else:

<else-body>

22

then-body

Page 23: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

if <condition1>:

if <condition2>:

<body1>

else:

<body2>

else:

if <condition3>:

<body3>

else:

<body4>23

else-body

Page 24: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

24

a = int(input("A. Enter a number: "))

b = int(input("B. Enter a number: "))

if a > b:

print("A is greater")

else:

if a < b:

print("B is greater")

else:

print("They are equal")

print("Have a nice day!")

They are equal B is greater

A is greater

Have a nice day!

a>b

a<b

True

True

False

False

Page 25: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Write a program that asks the user for their age. If they enter a number of 19 or

over, ask them for ID.

If they have ID, let them in. Otherwise, they cannot enter.

25

Page 26: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Write a program that asks the user for their age. If they enter a number of 19 or

over, ask them for ID.

If they have ID, let them in. Otherwise, they cannot enter.

If the user is under 19, they can try to sneak in, with a 35% chance of success!

26

Page 27: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Write a program that asks the user for their grade.

If it is above or equal to 80, they get an A.

If it is between 70 and 79, they get a B.

If it is between to 60 and 69, they get a C.

If it is between 50 and 59, they get a D.

Otherwise they get an F.

27

Page 28: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

if <condition1>:

<body1>

elif <condition2>:

<body2>

else:

<body3>

28

Page 29: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

29

a = int(input("A. Enter a number: "))

b = int(input("B. Enter a number: "))

if a > b:

print("A is greater")

elif a < b:

print("B is greater")

else:

print("They are equal")

print("Have a nice day!")

They are equal B is greater

A is greater

Have a nice day!

a>b

a<b

True

True

False

False

Page 30: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Write a program that asks the user for a number and, using an elif statement,

outputs whether the number is positive, negative, or zero.

30

Page 31: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Combine Boolean expressions

Build more precise conditions

Three operators:

▪ not

▪ and

▪ or

31

http://sydneypadua.com/

Page 32: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Negation (not)

▪ Unary operator (one operand)

▪ Result has the opposite Boolean value

Truth Table:

32

x

T

F

not x

F

T

Page 33: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

33

I am NOT sleepy

I will study

Page 34: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Conjunction (and)

▪ Binary operator (two operands)

▪ Result is True only if both inputs are True

Truth Table:

34

T F

T T F

F F F

Page 35: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

35

I am hungry AND

I have $20

I will buy pizza

Page 36: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

36

I am buying a movie ticket

AND I am a student

I will get a discount on

the price

Page 37: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Disjunction (or)

▪ Binary operator (two operands)

▪ Result is True if either input is True

Truth Table:

37

T F

T T T

F T F

Page 38: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

38

The car battery is dead

OR there is no gas

The car will not start

Page 39: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

39

I am hungry OR

I am bored

I will check the fridge

Page 40: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

What is the value of this expression?

(True and (not False)) and (True or (False and True))

(True and True) and (True or False)

True and True

True

40

Page 41: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

What is the value of this expression?

(not False) or (False and (not True)) and not True

(not False) or (False and False) and not True

True or False and not True

41

Page 42: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

What is the value of this expression?

(not False) or (False and (not True)) and not True

(not False) or (False and False) and not True

True or False and not True

True or False and False

True or False

True

42

Page 43: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

43

Symbol Meaning

() Brackets

** Exponentiation

*, /, //, %, Multiplication, etc.

+,- Addition, Subtraction

<,>,<=,>=,==,!= Comparison

not Negation

and Conjunction

or Disjunction

= Assignment

Pre

cedence

Page 44: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Write a program that asks the user for a number between 1 and 10 (inclusive). If

the user enters a number inside the range, it thanks them, if it's outside of the

range it displays an error message.

44

Page 45: Branches and Decision Making - Carleton Universitypeople.scs.carleton.ca/~arunka/courses/comp1405/lectures/... · 2020-01-31 · Write a program that asks the user for their age

Write a program that asks the user for a month of the year, and responds with the

number of days in that month.

45