1

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)

6 Answers 6

3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This has explained my broken logic. Much appreciated.
2

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++

1 Comment

Yes there is obviously a lapse in my understanding here. I would have expected: while True: False ...to have broken the while loop
0

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)

Comments

0

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.