If statement returning False in While True loop (Python)
Question:
I expected that in this If statement, the variable ‘i’ would increment until it eventually equals 10, and subsequently ‘if 10 < 10’ would return False, breaking my while loop. But this code seems print until 10 and then get stuck in an infinite loop unless I add an else: break. Why?
i=0
while True:
if i < 10:
i = i + 1
print(i)
Answers:
That is because there isn’t anything telling you to terminate the loop. So it will continue even after the if statement isn’t satisfied.
This is why it is generally not a great practice to use while True
You can achieve the same thing with a for loop when the break condition is built into the loop:
for i in range(0, 10):
print(i)
while True
will make the loop run forever because “true” always evaluates to true. You can exit the loop with a break.
To achieve what you want to do, I would use
while i < 10:
print (i)
i++
If you want to use while True then you can go for:
i=0
while True:
i = i + 1
print(i)
if i == 10:
break
while X
repeats when X
equals to True
so in while True
it’s always True
. It only breaks with break
statement.
In your code, you only check the value inside the while
loop with if so neither you break the while loop nor you change True
to False
in while True
.
If you want to use while
:
i = 0
while i < 10:
i += 1
print(i)
Or
i = 0
while True:
if i < 10:
i += 1
print(i)
else:
break
Without while
:
for i in range(10):
print(i)
I think you need to understand a few things here, as you have set while True
which means statement will never gets false
so there is never end to while loop
even if if condition
gets fail. So the while loop
will continue running till you interrupt.
The only way you can achieve this without break is like this, where you have a variable which will reset the condition of while loop
to false when if loop
fails
i=0
condition = True
while condition:
if i<10:
i=i+1
print(i)
else:
condition=False
The answers here might be too hard to understand for a newbie. The error is that you are telling the computer to keep adding 1 in every loop until it reaches 10, and then stop… adding. As people have already said, you need to explicitly tell it to stop looping by using break
. And even better, skip the while
loop and use a for
loop instead.
I expected that in this If statement, the variable ‘i’ would increment until it eventually equals 10, and subsequently ‘if 10 < 10’ would return False, breaking my while loop. But this code seems print until 10 and then get stuck in an infinite loop unless I add an else: break. Why?
i=0
while True:
if i < 10:
i = i + 1
print(i)
That is because there isn’t anything telling you to terminate the loop. So it will continue even after the if statement isn’t satisfied.
This is why it is generally not a great practice to use while True
You can achieve the same thing with a for loop when the break condition is built into the loop:
for i in range(0, 10):
print(i)
while True
will make the loop run forever because “true” always evaluates to true. You can exit the loop with a break.
To achieve what you want to do, I would use
while i < 10:
print (i)
i++
If you want to use while True then you can go for:
i=0
while True:
i = i + 1
print(i)
if i == 10:
break
while X
repeats when X
equals to True
so in while True
it’s always True
. It only breaks with break
statement.
In your code, you only check the value inside the while
loop with if so neither you break the while loop nor you change True
to False
in while True
.
If you want to use while
:
i = 0
while i < 10:
i += 1
print(i)
Or
i = 0
while True:
if i < 10:
i += 1
print(i)
else:
break
Without while
:
for i in range(10):
print(i)
I think you need to understand a few things here, as you have set while True
which means statement will never gets false
so there is never end to while loop
even if if condition
gets fail. So the while loop
will continue running till you interrupt.
The only way you can achieve this without break is like this, where you have a variable which will reset the condition of while loop
to false when if loop
fails
i=0
condition = True
while condition:
if i<10:
i=i+1
print(i)
else:
condition=False
The answers here might be too hard to understand for a newbie. The error is that you are telling the computer to keep adding 1 in every loop until it reaches 10, and then stop… adding. As people have already said, you need to explicitly tell it to stop looping by using break
. And even better, skip the while
loop and use a for
loop instead.