Programs That Can Loop

Learn how to execute a block of code multiple times.

We have found a way to break the strictly sequential flow of a program, by pushing one or however many statements within an if block. This means that our lines of code, other than those in if-else blocks, will get executed line by line, in sequence. But some lines of code will only get executed if a condition is True, otherwise, another set of lines will become a part of the execution. Sequential or selective, each line of code only gets executed once, and then our computer fetches the next one, and then the next.

The while loop

Wouldn’t it be great for the programmers if they could make the computer execute a block of code repeatedly while a condition is True?

Looping over the code
Looping over the code

For instance, if you are required to print 1 three times, what would be your code?

Python 3.10.4
print(1)
print(1)
print(1)

Let’s imagine we are being asked by our boss to print 1 ten times. Of course, we’d have to write 10 lines of code. We’ll drag our feet a little but we’ll still be willing to write those 10 lines of code. But now, our boss is ordering us to print it a 1000 times! Would we protest, resign, or start writing long and tedious code where every line is doing the same thing over and over again?

Even if we are okay with calling the print() function again and again, we need to be able to keep a count of how many times we have repeated the task. Let’s use a variable and name it appropriately to keep track of it. We initialize counter with 1:

counter = 1

We know there are comparison operators, such as < or less than equal to, <= that we can use to keep track of the counter. But this time, instead of checking to see if the condition is true or not, we need a mechanism to keep printing while the condition is true:

counter = 1
while counter <= 10:
print(1)
A loop to execute statement multiple times

Thankfully, Python gives us this new keyword while. This makes our code so neat. Let’s impress the boss: 3 lines of code is much more efficient than other programmers who are still writing their 10 lines.

Python 3.10.4
counter = 1
while counter <= 10:
print(1)

Oh no! This keeps printing until the Python program has had enough!

Why don’t you seek the help of Educative’s AI mentor? Don't hesitate to press the new button!

Looks like we forgot to update the counter, in the absence of which the value of the counter remains <= 10 forever. This was our mistakeThis is an example of a logical error., not the computer’s. Let’s fix that.

There, this might work now.

Python 3.10.4
counter = 1
while counter <= 10:
print(1)
counter = counter + 1

Oh no! That didn’t work either! Should we give up? No, we think, we always think, instead of giving up. We always think of why things don’t work the way we expect them to. We do this persistently, like being in a loop, till we succeed.

What is the difference between the two pieces of code here?

Code A

counter = 1
while counter <= 10:
    print(1)
counter = counter + 1

Code B

counter = 1
while counter <= 10:
    print(1)
    counter = counter + 1

We’ve already tried code A, and it kept printing 1 forever. Why does code B work, we’ve only made a tiny change in the last line, and that too, only in the last statement’s indentation. Run code B first, but then think about why it stopped afterYou might have to scroll to see all the 1s it printed in the output. printing the 10th 1.

Python 3.10.4
counter = 1
while counter <= 10:
print(1)
counter = counter + 1

Printing the counter

Instead of print(1), let’s print(counter) to know how code A and code B keep track of the counter. Remember, our algorithmic idea was to initialize a counter to 1, and right after printing the number, increase the counter by 1 so that the while loop only works 10 times.

Python 3.10.4
counter = 1
while counter <= 10:
print(counter)
counter = counter + 1

Life without while()

Do you see what our computer is doing for us? If we unroll this efficient loop-based program, this is what we did.

Python 3.10.4
counter = 1
print(counter)
counter = counter + 1
print(counter)
counter = counter + 1
print(counter)
counter = counter + 1
print(counter)
counter = counter + 1
print(counter)
counter = counter + 1
print(counter)
counter = counter + 1
print(counter)
counter = counter + 1
print(counter)
counter = counter + 1
print(counter)
counter = counter + 1
print(counter)
counter = counter + 1

Imagine writing the above code manually when the boss asked us to print 100 numbers. Well, now we don’t have to resign because we know that whenever there is a repeating pattern in our code or whenever something needs to be done again and again, we can use a while loop.

Challenge

Can you prove to your boss that you can write a program that prints 1000 numbers on the screen? And that you can do that in just a few seconds?

Python
counter = 1
while counter <= 10:
print(counter)
counter = counter + 1
Show Hint

The while loop in our project

1.

What are some of the repeating patterns in your client’s project where a loop can be applied?

A.

Asking the user for their name.

B.

Printing the user’s final score

C.

Posing a question, taking the user’s answer, checking if the learner’s answer is correct, and updating the score.


1 / 1