What is wrong with this code snippet?

Can anyone tell me what's wrong with this code snippet? How will you improve it?

a = 1

while a:

    a += 23

    print a
Write your answer…

3 answers

Assuming that's python, the fact that it would take an unknown amount of time to exit since you'd have to wait for a number limit rollover to stumble upon zero for the while statement to finish... You have no planned exit condition unless your intent is LITERALLY to see whatever the max_integer size (assuming integer) does and how many times you can add 23 to one until the rollover zero's... which could be one HELL of a long time.

It's a bit like the US involvement in Afghanistan -- there is no exit strategy.

LOL over the US/Afghanistan comment!

Reply to this…

Share your programming knowledge and learn from the best developers on Hashnode

Get started

infinite loop.

you should add a condition to break

a = 1

while a:

    a += 23

    print a

    # assuming you do not want it to run forever
    if a > 100: 
        break

Reply to this…

It would result in a potential infinite loop.

You are evaluating a as boolean, but treating it as numeric. That's never a good idea.

Reply to this…