while loop and less than or equal to sign (Python)
Solution 1
There is nothing odd about <=
; your loop condition allows for numbers up to and including 5
. But you increment count
and then print it, so you will print 6
last.
That's because count = 5
satisfies your loop condition, then you add one to make it 6
and print. The next time through the loop count <= 5
is no longer true and only then loop ends.
So your code does this:
count = 0
,count <= 5
->True
,count += 1
makescount = 1
, print1
.count = 1
,count <= 5
->True
,count += 1
makescount = 2
, print2
.count = 2
,count <= 5
->True
,count += 1
makescount = 3
, print3
.count = 3
,count <= 5
->True
,count += 1
makescount = 4
, print4
.count = 4
,count <= 5
->True
,count += 1
makescount = 5
, print5
.count = 5
,count <= 5
->True
,count += 1
makescount = 6
, print6
.count = 6
,count <= 5
->False
, end the loop.
You could increment the counter after printing:
while count <= 5:
print(count)
count += 1
or you could use <
to only allow numbers smaller than 5
:
while count < 5:
count += 1
print(count)
Solution 2
It is simple when count equals five you add 1 and it becomes 6 then it is printed and you exit the loop.
Solution 3
Your problem is not about how <=
works.
You are adding 1 to count
before printing it, so when count is equal to 5, you then add 1 and therefore print 6.
Solution 4
Let's walk through the code and see what's happening.
Note: If your code is doing something that you didn't expect it to do, this is a good practice to follow.
count = 0
while count <= 5:
count += 1
print(count)
The count
starts at 0
count = 0
while count <= 5: # count = 0. Is 0 <= 5? Yes. Run the code.
count += 1
print(count)
Increment the count so it now equals 1. Print it.
while count <= 5: # count = 1. Is 1 <= 5? Yes. Run the code.
count += 1
print(count)
Increment. Print. Repeat.
Let's move on to the interesting case that's causing the problem.
while count <= 5: # count = 5. Is 5 <= 5? Yes. Run the code.
count += 1
print(count)
In this case the count
still satisfies the inequality, so the code runs.
What happens?
The count
, which is 5, is incremented and thus prints out 6.
Now that I hope you understand why the problem exists, let's explore alternatives and their advantages, disadvantages, and outputs.
Let's start with your code.
count = 0
while count <= 5:
count += 1
print(count)
Advantages: Does not print out 0
Disadvantages: Prints out 6
What if we removed the =
sign?
count = 0
while count < 5:
count += 1
print(count)
Output:
1
2
3
4
5
Advantages: Does what you want
Disadvantages: You have to start at 0 instead of 1
What if we did as you suggested and replaced the <
sign with the !
sign?
count = 0
while count != 5:
count += 1
print(count)
Output:
1
2
3
4
5
Advantages: Does what you want
Disadvantages: Fragile. If you ever changed your increment so that it increased by a number like, say, 3, the end point would be skipped over accidentally and the code would continue to run forever. Better to use an inequality
What if we wanted where we start to be the first number that is displayed? Well, to do this we'd have to print out the current number before we change it, so that means we have to flip the order of the events.
count = 1 # Change the first number so it's what we want displayed first.
while count <= 5:
print(count)
count += 1
Output:
1
2
3
4
5
Advantages: Does what you want and starts on the first displayed number instead of below it.
Disadvantages: If you want to stick with while
loops, this is the way to go, but there is a better way in this case.
In situations like this, where you increment numbers and then perform operations with them, it's much more logical to just use a for
loop, which was designed for instances just like this one.
for count in range(1,5):
print(count)
Output:
1
2
3
4
5
Advantages: Does what you want, easier to read, easier to write, less likely to cause bugs based on placement.
Disadvantages: The upper boundary must be known, unlike in a while
loop.
Zion
Updated on August 31, 2020Comments
-
Zion over 3 years
So I was doing while loops and I noticed something strange.
count = 0 while count <= 5: count += 1 print(count)
output:
1 2 3 4 5 6
it's not that I don't understand while loops. It's that how come the count is printed up to six? when it's supposed to print
count
only ifcount
is less than or equal to 5?and well 6 is beyond 5. why is this?
I know I could do
count = 0 while count != 5: count += 1 print(count)
but I just want to understand why does putting
<=
behave in an odd way? -
Malik Brahimi over 8 yearsIn other words, you will get a different result if you were to print before the increment.
-
Zion over 8 yearsI got it now. it stills increments count even if count is 5 because I used
<=
thats why 6 was outputted also because i putcount += 1
first so when the count was 5 it got added one then it checked the loop again it turns false so it breaks. thanks for the visualization of what's happening! -
MuhsinFatih over 2 yearsThe output of ``` for count in range(1,5): print(count) ``` is not 1 2 3 4 5. It's 1 2 3 4