Lecture 12 Slides - Control Flow - Conditionals

1 of 28

Control Flow (Conditionals)

CS 110

1

2 of 28

Reminders and Announcements

  • Exercise 4 due this evening - Peer Review assignment.
  • Q1 grades will be available by next Wednesday at the latest
  • Back to a normal schedule next week

Next Week

  • Monday - Loops
  • Wednesday - (Pre-Recorded + MQ 8) Animation + Tutorial 4
  • Friday - Events/Listeners + Mini-Quiz 9
    • Exercise 5 due

3 of 28

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

4 of 28

conditionals

4

5 of 28

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

6 of 28

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

7 of 28

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.

8 of 28

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

9 of 28

Conditional Statements

Conditional statements are like a choose your own adventure story:

  • If you choose to open Door #1, one thing happens, otherwise something else happens
  • New choices can also be made as you step through new doors
  • After a series of choices, many different outcomes become possible
  • Each reader ends up in a different place: same book, different outcome

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

10 of 28

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)

11 of 28

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 of 28

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 of 28

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):

  • Is course full? True
  • Wants waitlist? True

14 of 28

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):

  • Is course full? True
  • Wants waitlist? True

15 of 28

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):

  • Is course full? False
  • Has prereq? True
  • Taken prereq? False

16 of 28

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):

  • Is course full? False
  • Has prereq? True
  • Taken prereq? False

17 of 28

If Statement - Guessing Game

17

# Ask the user for a number

# print "you win!" if it's 15

# print "game over."

18 of 28

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)

19 of 28

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.

20 of 28

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."

21 of 28

If Statement - Guessing Game

21

my_num = int(input("Input a number…"))

if my_num == 15:

print("You win!")

print("Game over.")

22 of 28

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.

23 of 28

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)

24 of 28

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

...

25 of 28

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

26 of 28

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.

27 of 28

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)

28 of 28

Comparison Operators and the Substitution Model

year = 2023

...�if False:

print('February 29')

28