COMP1405Dr. Andrew RunkaCarleton University
1
Branches and Decision Making
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!
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
It is raining
Bring an umbrella
4
5
Grade is at least 50
Pass the class
6
There is a traffic jam
Stop driving
7
<some condition>
<some action>
True or False,
aka, a Boolean expression
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
if weather == "rainy":
print("Bring an umbrella")
if grade >= 50:
print("Congratulations!")
9
Here is the syntax for if-statements in Python:
10
if <condition>:
<body>
if <condition>:
<body>
11
Keyword if
A Boolean expression
Any instructions to run if the condition is true
if x:
print('A')
print('B')
print('C')
12
A
B
X?True
False
Indentation is important!
C
if x:
print('A')
print('B')
print('C')
13
A
B
X?True
False
Indentation is important!
C
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
15
It is a weekend
I will watch cartoons
I will go to work
It is before noon
I will eat pancakes
I will eat pizza
16
if today != weekday:
print("Watch cartoons")
else:
print("Go to work")
if time < noon:
print("Pancakes!")
else:
print("Pizza!")
17
Here is the syntax for if-else statements in Python:
if <condition>:
<then-body>
else:
<else-body>
18
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
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
if <condition>:
<then-body>
else:
<else-body>
21
if <condition1>:
if <condition2>:
<body1>
else:
<body2>
else:
<else-body>
22
then-body
if <condition1>:
if <condition2>:
<body1>
else:
<body2>
else:
if <condition3>:
<body3>
else:
<body4>23
else-body
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
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
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
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
if <condition1>:
<body1>
elif <condition2>:
<body2>
else:
<body3>
28
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
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
Combine Boolean expressions
Build more precise conditions
Three operators:
▪ not
▪ and
▪ or
31
http://sydneypadua.com/
Negation (not)
▪ Unary operator (one operand)
▪ Result has the opposite Boolean value
Truth Table:
32
x
T
F
not x
F
T
33
I am NOT sleepy
I will study
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
35
I am hungry AND
I have $20
I will buy pizza
36
I am buying a movie ticket
AND I am a student
I will get a discount on
the price
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
38
The car battery is dead
OR there is no gas
The car will not start
39
I am hungry OR
I am bored
I will check the fridge
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
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
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
43
Symbol Meaning
() Brackets
** Exponentiation
*, /, //, %, Multiplication, etc.
+,- Addition, Subtraction
<,>,<=,>=,==,!= Comparison
not Negation
and Conjunction
or Disjunction
= Assignment
Pre
cedence
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
Write a program that asks the user for a month of the year, and responds with the
number of days in that month.
45