# Control Structures

### Overview

We have no looked at if/elif/else statements, while loops and for loops. These are all types of control statements, and change what statements get executed.

Below are some "basic", or non-control Python statements:


print("Hello")
name = input("What's your name? ")
day = "Monday"


These all cause some action to happen, but don't affect the control flow of the program. Control flow means what statements are going to run next. With these, Python will always just move down to the next one.

Control statements change this. If statements let you choose between different options of what code to run, and loops let you run some code more than one time.

Most programs are a combination of control statements and basic ones.

### Flowcharts

One way of visualizing control flow statements is with a flowchart. For example, this code:


temperature = float(input("What is the temperature? "))

if temperature < 15:
print("Warning: It's very cold!")

print("Done")


Can be depicted like this:

The flowchart shows how the control of the program changes based on the condition of the if statement. If the answer is True, it prints the warning message, otherwise it just moves on. When the warning is printed, we move on as well.

Drawing flowcharts can help us think about what control statements we need and how they should work.

A flowchart for an if/else statement is similar, except that it creates two different paths before joining them. For instance, the following code has an if/else:


temperature = float(input("What is the temperature? "))

if temperature < 32:
print("It's below freezing!")
else:
print("It's above freezing.")

print("Done")


We can also extend these flowcharts to include elif statements. In this case, they will include more than two options. For example we could have the following code:


temperature = float(input("What is the temperature? "))

if temperature < 20:
print("It's very cold!")
elif temperature < 50:
print("It's pretty cold.")
elif temperature < 75:
print("It's quite nice.")
elif temperature < 90:
print("It's hot.")
else:
print("It's really hot!.")

print("Done")


Which would give us a flowchart like this:

### Flowcharts for Loops

We can also make flowcharts for loop control structures. The difference is that, with loops, we will have arrows which go back to previous instructions.

For example, with our input checking loop which we saw previously:


age = int(input("How old are you? "))

while age < 0:
print("Hey, your age can't be negative!")
age = int(input("How old are you for real? "))

print("You are", age, "years old.")


We could have a flowchart which captures this loop:

We can also make a flowchart from a for loop. The following code just prints each number from 1 to 10:


for i in range(1, 11):
print(i)

print("Done!")


A flowchart for this loop might look like this:

Flowcharts can be helpful for a few reasons. First, they can help us to understand how the control structures work. Secondly, when writing our own programs, we can try drawing a flowchart first. Then, when we understand how the flow of the program is supposed to go, we can work on coding it.

### Nested Control Structures

To form more complex programs, we can nest any of the control structures together. For example, we can have an if statement inside of a while loop, a for loop inside an if statement, or any other combination.

For example, the program below loops through the numbers 1 through 10. Then, inside of the loop, it checks if the number is even or odd and prints that out. Recall that % checks the remainder of a division. If the remainder when dividing by 2 is 0, it means there is no remainder.


for i in range(1, 11):
if i % 2 == 0:
print(i, "is even.")
else:
print(i, "is odd.")


Because the if statement is under the for loop, it will get executed each time the loop executes.

Below is a flowchart showing this:

Notice there are elements of the for loop and the if statement here. The if/else decision happens inside of the for loop repetition.

### Nested Loops

We can also put one loop inside of another. For instance, the following program has one for loop nested under another:


for i in range(1, 6):
for j in range(1, 6):
print(i, "*", j, "=", i*j)


Each time the first loop executes, the second loop will execute 5 times. How many lines will this program print?

We can also make a flowchart for this program:

Now there is one loop inside of another.

There is no limit to how many times we can nest loops and if statements. The more nesting that is done, the more complicated the program is to write and understand though!