-1

Just wanted to confirm whether the following is an infinite loop or not. I think it's not because of how memory works, I faintly remember something about how the ints loop back to the opposite limit once a certain lower or upper limit is reached, based on how ints are stored in a computer's memory. But I'm not certain.

i = 0
while i < 1:
    #do stuff
    i -= 1

EDIT: my god a stupid mistake in the syntax, my bad, I've edited the code now. Thanks for the patience. :)

  • If you're asking about a specific language or machine, please specify. – Josh Lee Apr 20 '17 at 12:42
  • 2
    Yes, because the condition `i < 1` never becomes `true`, unless in the `#do stuff` part that you didn't show you are modifying the variable `i` so that it might become less than 1. Note: you are decrementing a different variable `j`. Did you mean to do this or did you really mean `i`? – Jesper Apr 20 '17 at 12:42
  • @Jesper yeah my bad, I meant `i`. – Pranjal Verma Apr 20 '17 at 13:03
  • @JoshLee um, not really. Just in general. – Pranjal Verma Apr 20 '17 at 13:05

1 Answers1

1

Yes, it is an infinite loop.

Your condition is while i < 1 and i never changes

Dazak
  • 1,011
  • 2
  • 9
  • 17
  • 1
    just in case, it's a typo on his side. – Andrii Plotnikov Apr 20 '17 at 12:44
  • In that case is still and infinite loop... I guess that depending of the programming language (and the type of `i`) it could reach the minimum possible value for `i` and throw an error. – Dazak Apr 20 '17 at 12:48
  • @Dazak hm okay! Considering python 3.x and `i` thus being an `int`? – Pranjal Verma Apr 20 '17 at 13:06
  • 1
    According with [this](http://stackoverflow.com/a/4581847/5602069), _Python has arbitrary precision integers so there is no true fixed maximum. You're only limited by available memory_ so it still being an infinite loop, but when the available memory get fill, it will throw an `OverflowError` (in this part I'm guessing since I am not familiar with python) – Dazak Apr 20 '17 at 13:27