Skip to content

Control Flow

Dave Luk edited this page Jan 2, 2018 · 7 revisions

Overview

In this lesson, we will learn about ways to control how a program behave.

New Concepts

Boolean Values

Boolean is a kind of datatype. It have only two values: True and False. Now you can think of your true-false questions on your tests as booleans!

Comparison Operators

Comparison operators are symbols that will give us results in boolean. Let's first introduce them:

Equals ==

This is used to check if 2 variable/values are same. For example:

5 == 5  # This give us True
5 == 0 # This give us False
Not equals !=

Just like equals, this checks for exact opposite of equals:

5 != 5  # This give us False
5 != 0 # This give us True
Greater than >

This checks for if a value/variable is greater than another:

5 > 5  # This give us False
5 > 0 # This give us True
Less than <

This checks for if a value/variable is less than another:

5 < 5  # This give us False
0 < 5 # This give us True
Greater than or equals >=

This checks for if a value/variable is greater than or equals another:

5 >= 5  # This give us True
0 >= 5 # This give us False
Less than or equals <=

This checks for if a value/variable is less than or equals another:

5 <= 5  # This give us True
0 <= 5 # This give us True

Conditions

Conditions are formulas written with boolean values or operators. That means all the previous examples are conditions! All of them used a boolean operator, and they give back a boolean value as result.

Activity 1

Now, we will show some more complicated conditions (boolean statements): Try to calculate their value!

1. True == True
2. True != False
3. 42 == '42'
4. 0 == 0.0

Boolean Operators

What if we want something more complicated? So far we have only 1 condition. What if we want multiple conditions or at least 1 condition to happen?

We will need to use Boolean Operators!

Let's introduce them.

and

Conditions on both sides of this operator must be True for this to be True. That is, both things must happen for this condition to pass. For example:

5 == 5 and 5 > 0 # True
5 == 5 and 5 > 9 # False
or

At least 1 condition on both sides of this operator must be True for this to be True. That is, one thing must happen for this condition to pass. For example:

True == False or 5 > 0 # True
True == False or 5 > 9 # False
not

Using this keyword, we can flip boolean values. That is, True to False and False to True

not True # False
not False # True
not 0 == 6 # True
not 10 = 10.0 # False 

Activity 2: Complex Conditions

Now, with these operators, we can write more complex conditions. Try to calculate their boolean value.

1. True != False and False != False
2. not 9 > 0 or not 0 > 9
3. False or True and False 

Bonus Questions :3

These are harder ;3c

1. 3 > 2 > 1 < 10
2. 1 and True
3. not not True or not not False

Flow of Control

Now, we can use these conditions to control programs!

if statement

The if statement executes commands base on the boolean value of the condition. As an Example:

condition = True #False
if condition:
    print('condition is True!')
else:
    print('Condition is False.')

Here, we have the condition to be True, so then we will print out 'condition is True!'

Now, let's amp it up!

while statement

The while statement executes commands until the condition is no longer True. Lets look at an example:

condition = 0
while condition < 5:
    print(cond)
    cond = cond + 1

This code will print out the following:

0
1
2
3
4

Last but not least, let's look at the 'for' statement.

'for' statement

The for statement executes commands a set number of times. Precisely, it goes over a list of things, and stop when it runs out.

Let's solidify this concept with an example:

for i in range(4):
    print(2*i)

The output of this program would be:

0
2
4
6

The range(n) function returns a list of numbers in order from 0 to n-1. so range(4) give us 0, 1, 2, 3. So the for loop goes from 0 to 3.

import statement

The import statements are useful for bringing in other programs and functions. There are many packages that can be used so we do not have to write them ourselves!

import math
print(math.pi)

The output:

3.141592653589793

Activity Answers

A1

1. True 2. True 3. False 4. True

A2

1. False 2. True 3. False

[Master Activity TBA]

Summary

Practice Problems