# While loops | BUS 104 – Python Exam

## While loops explained

Count from 0 to 3.

Congrats, you just accomplished the core functionality of a while loop.

First, we set a starting value of 0.

Then, we counted 1 by 1 until we reached 3.

Once we reached 3, we stopped counting.

So, how does this relate to while loops?

While loops, when given a starting value, continue looping while a certain condition is met.

You might be confused how this relates to while loops right now, so let's dive into an example so you can see.

## Don't be tempted to do this!

With your current Python experience, I'd expect you to hear "Make a Python program that counts from 0 to 3" and code the following:

print(0)
print(1)
print(2)
print(3)

While this would work, it's bad practice. What if I asked you to make a Python program that counts from 0 to 10 instead?

print(0)
print(1)
print(2)
print(3)
print(4)
print(5)
print(7)
print(8)
print(9)
print(10)

Ugh. Talk about a headache to type out! Not only is it annoying to type, but what if you mess up one of the numbers? I’m sure you didn’t even notice that I forgot to type a 6…

This can be cleaned up with a while loop. Let's learn how to write one!

## Coding a while loop

To teach you how to code while loops, I'm actually going to first reveal the answer to you, and then we'll break it down step-by-step.

Here's the answer to coding a while loop that counts from 0 to 3!

PAID CONTENT

This is the end of the preview. To unlock the rest, get the .