User Controls

For loop or while loop for small number of iterations?

  1. #1
    Sophie Pedophile Tech Support
    Consider this code:


    pages = 5

    i = 0
    while i < pages:
    do_stuff()
    i += 1



    pages = 5

    for i in pages:
    do_stuff()


    Which would be better?

  2. #2
    iamlight Yung Blood
    Which one do you prefer? Simply pragmatics

    If you want to discuss performance, how is your assembly?
  3. #3
    aldra JIDF Controlled Opposition
    I prefer the form of for(x;x<y;x++)

    generally for if you're able to know the number of iterations before starting, while if you aren't
    The following users say it would be alright if the author of this post didn't die in a fire!
  4. #4
    Sophie Pedophile Tech Support
    Originally posted by iamlight Which one do you prefer? Simply pragmatics

    If you want to discuss performance, how is your assembly?

    Not that good. As for the subject of loops, i prefer a `for` loop, it looks and feels more elegant.
  5. #5
    Lanny Bird of Courage
    Second piece of code won't run. "for ... in" iterates over a collection type (list, dictionary, stream, etc) while `i` is an int. You can do `for i in range(5):` but not `for i in 5:`. Both `for index in range(N)` and `for item in collection` are prefered to a while loop if you want to iterate over an item or do something finite N times. This is more convention/readability than anything.

    If you want to do something N times for large values of N the while construct is more space efficient since `range(n)` takes roughly linear space while the index approach takes constant space. There is, however, `xrange()` which returns a generator and requires constant space to operate.
    The following users say it would be alright if the author of this post didn't die in a fire!
  6. #6
    Sophie Pedophile Tech Support
    Originally posted by Lanny Second piece of code won't run. "for … in" iterates over a collection type (list, dictionary, stream, etc) while `i` is an int. You can do `for i in range(5):` but not `for i in 5:`. Both `for index in range(N)` and `for item in collection` are prefered to a while loop if you want to iterate over an item or do something finite N times. This is more convention/readability than anything.

    If you want to do something N times for large values of N the while construct is more space efficient since `range(n)` takes roughly linear space while the index approach takes constant space. There is, however, `xrange()` which returns a generator and requires constant space to operate.

    Ya ya, sorry i forgot to put `range` there. I was about to say REEEEE LIAR


    >>> page = 5
    >>> for i in range(page):
    ... print i
    ...
    0
    1
    2
    3
    4


    Then i saw i did range in the interpreter and not in my code example, lel.
  7. #7
    Merlin Houston
    Originally posted by aldra I prefer the form of for(x;x<y;x++)

    Same pisses me off that python doesn't have this. I usually only use while if it's an infinite while(true) {...}
  8. #8
    Lanny Bird of Courage
    ^ `for x in range(y)` is the exact same behavior, the only difference is syntax
  9. #9
    Merlin Houston
    Originally posted by Lanny ^ `for x in range(y)` is the exact same behavior, the only difference is syntax

    Yeah I know it just doesn't feel right and I can never remember the syntax, if it will start at 0 or 1, will it use the final number or just stop there, etc. I'm sure once you know it that it is cleaner and more readable.
  10. #10
    Sophie Pedophile Tech Support
    Originally posted by Merlin Yeah I know it just doesn't feel right and I can never remember the syntax, if it will start at 0 or 1, will it use the final number or just stop there, etc. I'm sure once you know it that it is cleaner and more readable.

    "It is not the Python's fault that it occasionally bites"
    - Cyber Sun Tzu
Jump to Top