### Thread: Iteration count in a while loop

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. 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.
3. 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```
4. 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.
5. 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```