Control Flow (Conditionals)
CS 110
1
Reminders and Announcements
Next Week
3
Top-Down
When we first started our class, the programs we wrote went from top to bottom.
Function Definitions
When we started defining our own functions, we began to teach the computer how to do tasks which gave us the ability to first DEFINE then later CALL those functions in our programs.
Conditionals
With conditionals, we can learn how to run parts of our programs only if certain conditions were met.
Loops
Loops allow us to repeat bits of our programs many times without having to know in advanced how many pieces of data we'd have to deal with..
Events
Events allow us to tell the computer to LISTEN for particular events or inputs and when the computer HEARS that input, they run part of our program.
Control Flow in Python
conditionals
4
First…we need a new data type!
5
Boolean values only have one of two values:
True
False
my_first_boolean = True
my_second_bool = False
Revisiting Basic Data Types
Type Examples
Integer (int) 1 -5555
Float (float) 2.567 3.14159
String (string) "Hello world!"
None None
Boolean (bool) True False
6
Comparison Operators (compare two things and get back a boolean!)
Comparison operators compare two operands according to a comparison rule and evaluate to either True or False (boolean)
7
Operator | Description |
== | If the values of two operands are equal, then the condition becomes True. |
!= | If values of two operands are not equal, then condition becomes True. |
> | If the value of left operand is greater than the value of right operand, then condition becomes True. |
< | If the value of left operand is less than the value of right operand, then condition becomes True. |
>= | If the value of left operand is greater than or equal to the value of right operand, then condition becomes True. |
<= | If the value of left operand is less than or equal to the value of right operand, then condition becomes True. |
Logical Operators
Logical operators provide additional ways to determine whether something is true or false:
8
Operator | Description |
and | If both operands are True then the expression evaluates to True. Otherwise, the expression evaluates to False |
or | If either or both operands are True then the expression evaluates to True. If both operands are false, the expression evaluates to False |
not | If the operand is False than the expression evaluates to True (and vice versa) |
in | If the left operand is an element of the right operand, then the expression evaluates to True. Otherwise, the expression evaluates to False |
Conditional Statements
Conditional statements are like a choose your own adventure story:
Computer programs work the same way. Depending on the data that’s passed into the program, one part of your program will execute while another part gets skipped over
9
Conditional Statements
Conditional statements enable you to skip over some statements and execute others, depending on whether a condition is True or False.
EXAMPLE
10
Is the course full?
Enroll student
Add to waitlist
No (False)
Yes (True)
Everything needs to be put in terms of yes or no
Is the course full?
|── No (False)
| |── Is there a prerequisite?
| |── No (False)
| | └── Enroll student
| |── Yes (True)
| |── Has student satisfied prereqs?
| |── ...
|── Yes (True)
|── Does student want to be added to waitlist?
|── No (False)
| |── Start Over
|── Yes (True)
|── Add name to waitlist
11
12
Is the course full?
Is there a prerequisite?
Does student want to be added to waitlist?
Add name to waitlist
Start Over �
Has student satisfied prereqs?
Enroll student
No (False)
No (False)
No (False)
Yes (True)
Yes (True)
Yes (True)
Enroll student
Start Over �
No (False)
Yes (True)
13
Is the course full?
Is there a prerequisite?�
Does student want to be added to waitlist?
Add name to waitlist
Start Over �
Has student satisfied prereqs?
Enroll student
No (False)
No (False)
No (False)
Yes (True)
Yes (True)
Yes (True)
Enroll student
Start Over �
No (False)
Yes (True)
Data (use case):
14
Is the course full?
Is there a prerequisite?�
Does student want to be added to waitlist?
Add name to waitlist
Start Over �
Has student satisfied prereqs?
Enroll student
No (False)
No (False)
No (False)
Yes (True)
Yes (True)
Yes (True)
Enroll student
Start Over �
No (False)
Yes (True)
Data (use case):
15
Is the course full?
Is there a prerequisite?�
Does student want to be added to waitlist?
Add name to waitlist
Start Over �
Has student satisfied prereqs?
Enroll student
No (False)
No (False)
No (False)
Yes (True)
Yes (True)
Yes (True)
Enroll student
Start Over �
No (False)
Yes (True)
Data (use case):
16
Is the course full?
Is there a prerequisite?�
Does student want to be added to waitlist?
Add name to waitlist
Start Over �
Has student satisfied prereqs?
Enroll student
No (False)
No (False)
No (False)
Yes (True)
Yes (True)
Yes (True)
Enroll student
Start Over �
No (False)
Yes (True)
Data (use case):
If Statement - Guessing Game
17
# Ask the user for a number
# print "you win!" if it's 15
# print "game over."
If Statement - Guessing Game
18
Ask the user to input a number
If they guessed correctly
Game Over
Tell them they won
Yes (True)
No (False)
Blocked If Statement (if you want to do multiple things)
19
if condition:
statement 1
statement 2
...
CONDITION�Expression that evaluates to True or False.
Indented BLOCK�If the condition evaluates to True, the block executes. Otherwise, the block is skipped.
If-Else Statement - Guessing Game
20
# Ask the user for a number
# print "you win!" if it's 15 else print "uh oh."
# print "game over."
If Statement - Guessing Game
21
my_num = int(input("Input a number…"))
if my_num == 15:
print("You win!")
print("Game over.")
my_num = int(input("Input a number…"))
if my_num == 15:
print("You win!")
print("Game over.")
If Statement - Guessing Game
22
Thing to do if True
if keyword tells Python the next part is the conditional
Conditional
The thing to Python uses to determine what to do. It's a yes (True) or no (False) question.
If-Else Statement - Guessing Game
23
Ask the user to input a number
Is their guess correct?
Tell them uh oh.
Tell them they won
Game over
Yes (True)
No (False)
Blocked If Statement (if you want to do multiple things)
24
if condition:
statement 1
statement 2
...
CONDITION�Expression that evaluates to True or False.
Indented BLOCK�If the condition evaluates to True, the block executes. Otherwise, the block is skipped.
else:
statement 1
statement 2
...
If-Else Statement
25
my_num = int(input("Input a number…"))
if my_num == 15:
print("You win!")
else:
print("Uh oh.")
Thing to do if False
else keyword tells Python the "we're a continuation of the previous conditional
Using Comparison Operators
year = 2023
...�if year % 4 == 0:
print('February 29')
26
When this expression is evaluated, you can imagine substituting in the computed value.
Comparison Operators and the Substitution Model
year = 2023
...�if 2 == 0:
print('February 29')
27
Evaluates to False (so the code block does not execute)
Comparison Operators and the Substitution Model
year = 2023
...�if False:
print('February 29')
28