We talked about recursion and iteration in the last tutorial.

Recursion is the term used when you call the same function inside a function, which causes repetition since you’re repeating the function.

Iteration is the term used when you repeat steps without using recursion.

This video will be about making iteration or repeating steps easier!

In tutorial #2, we talked about most recent assignment because we talked about how you can assign a variable multiple times.

A variable takes the value of its most recent assignment.

When we’re repeating the assignment of a variable, we are iterating.

When you hear iteration, think repetition.

Let’s say that we set:

Here, we set b = a, and since a’s most recent assignment is 2.

b is equal to 2.

Even though b is equal to a, b is unaffected when we add 1 to a.

b is completely separate from a.

When we add 2 to b’s most recent assignment, we get 2 + 2.

In that example, we were iterating the assignment of a variable.

Instead of writing each repetitive step 1 by 1, Python has statements that make writing iterations or repetitions easy.

We’ll start with the while statement or also called while loop.

The while statement works like a conditional.

When the condition is true, the statements underneath are run, but the difference is that the while statement is checked again, and if the condition is still true, then the statements underneath are run again.

As long as the condition stays true, the statements underneath are repeated.

We use the term while loop because the while statement loops in a circle, repeating its statements.

We can write the same function that did the countdown to New Year’s like we did last video.

The while statement is like an if statements that continually checks the condition after running all the statements underneath.

We start at 10.

num must be greater than 0.

The condition is True, so all the statements underneath are run.

num is equal to 10 – 1.

Now, num is 9.

All the statements tabbed once to the right have been completed.

The while statement checks its condition again.

9 is greater than 0, so the condition stays True.

Repeat all the statements underneath.

Keep on repeating until the condition is false.

The condition is false, when num is 0 because 0 is not greater than 0.

The while statement is similar to recursion, but it doesn’t reuse a function.

There are two main parts.

1. The condition

A while statement is triggered by a True condition like an if statement.

2. Progress

You need to make sure that the condition is eventually False or else, you’ll have an infinite repetition or loop.

For the while loop that we have written, we are constantly decreasing num by 1.

num is decreasing every time we repeat the contents of the while loop.

10 becomes 9, 9 becomes 8, and so on until num is 0. When num is 0, it is not greater than 0, so the while statement ends there.

Then, print(“New Year’s”) happens and afterwards, we’re at the end of the function.

Using iteration with the while statement makes repeating steps much easier than recursion in most cases.

Instead of manually writing print(10) print(9) and so on 1 by 1, the while loop allows us to repeat a series of steps until the condition is False.

In the last video, I told you that if iteration seems easier, then you should use iteration.

But when iteration seems complex, try to think out the solution with recursion to see if recursion will make solving the problem easier.

Let’s do another example,

Let’s make a while loop that prints a number of lines.

First, the function should take in a parameter for the number of lines that we want.

Next, we want to use the while loop. We want to think of a base case. The moment when we are done with the while loop.

We can use num being equal to 0 as a means of ending the while loop.

Step 1 is figuring out our condition. Our condition should be (num != 0) because when num is 0, we are done.

while loops always have the keyword, while, followed by a condition and a colon after the condition.

We want the while loop to print out a line when num is not 0. num starts at 3.

Step 2 is progress. We want num to be decreasing at each step or iteration.

We can subtract num by 1 with each step to make sure that we’re making progress to make the condition eventually false.

The condition will be false when num is equal to 0, so decreasing num by 1 each step makes sense!

We want to print out an empty line at each step because that’s the purpose of our function.

Let’s call out function with the argument 3.

When we run the function, 3 lines are printed out.

Let’s take a closer look at our function.

The argument is 3, so num = 3.

while loop always looks at the while statement and checks if it’s true.

Step 1) the condition. The condition is true because 3 != 0. Do the statements underneath.

3 – 1 = 2

Check condition

2 != 0 is True

2 – 1 = 1

Check condition

1 != 0 is True

1 – 1 = 0

Check condition

0 != 0 is False, so while loop ends here.

While loops make repetitive steps much easier. If you are using repetition, think while loops.

There’s something called infinite while loops. Let’s talk about infinite while loops.

If you forget step 2, progress, then you’ll come out with a while loop that never stops.

If we delete the line, num = num – 1, the while loop will never stop because the condition will always be true.

3 != 0 is always true. num != 0 is always true because num always stays the same. There’s no progress.

The while loops continues forever! On a Windows computer, you can use CTRL-C to end the program.

We want the while loop to end, so always remember that you need progress that will make your condition be false eventually.

 

Video Demonstration