#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    2

    Iteration count in a while loop


    Hi,

    I'm trying to figure out the iteration happening in the background while the following program works.
    number = 21
    while number < 43:
    if number % 3 != 0:
    number += 1
    continue
    print number
    number += 1

    #the output of the above codes:

    21
    24
    27
    30
    33
    36
    39
    42
    #end

    From my understanding it goes as the following. Even though it is not printed, all numbers are iterated between 21-43. at the background and assessed true or false:

    Value: -----number<43
    21---------True
    22--------- False
    23--------- False
    24--------- True
    ..--------- ..
    42--------- True
    43--------- False # Last line of Iteration

    Is it correct or have I confused myself and it should be just:

    Value: -----number<43
    21--------- True
    24--------- True
    27--------- True
    ..--------- ..
    42--------- True
    43--------- False # Last line of iteration

    Very new to python. Sorry if it sounds silly. Thanks for help.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    That is what I am doing. I just thought that with everything else it keeps track of, without being told too, it would track this for some reason I am not thinking of.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    You can learn these things for yourself! Tell python to show you the happenings.
    Code:
    number = 21
    while number < 43:
        print('%d <--- number at start of while block'%number)
        if number % 3:
            number += 1 # increments number
            continue     # program flow goes back to the while test
        print('%d <-- is a multiple of 3'%number)
        number += 1 # increments number
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    2
    Thank you. %d and %number is new to me. Based on the print out,
    my understanding is - Every 3 numbers is true
    and get printed(21,24,27 and so on till 42). The rest are false thus not printed.

    Hope my interpretation is correct.
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    605
    Rep Power
    65
    Test print it:
    Code:
    number = 21
    while number < 43:
        print(number, (number %3 ))
        number += 1
    Conclusion:
    Code:
    number = 21
    while number < 43:
        if number % 3 == 0:
            print(number)
        number += 1

    Comments on this post

    • b49P23TIvg agrees : Nice! Extract the common statement to simplify the code.
    Last edited by Dietrich; November 7th, 2012 at 01:52 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo