Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 7th, 2012, 06:36 AM
 keshk
Contributing User

Join Date: Nov 2012
Posts: 49
Time spent in forums: 19 h 5 m 33 sec
Reputation 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
November 7th, 2012, 08:06 AM
 steveeshair
Registered User

Join Date: Nov 2012
Posts: 3
Time spent in forums: 34 m
Reputation 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
November 7th, 2012, 09:37 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,211
Time spent in forums: 1 Month 3 Weeks 2 Days 17 h 32 m 42 sec
Reputation Power: 455
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!

#4
November 7th, 2012, 11:49 AM
 keshk
Contributing User

Join Date: Nov 2012
Posts: 49
Time spent in forums: 19 h 5 m 33 sec
Reputation 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
November 7th, 2012, 02:26 PM
 Dietrich
Contributing User

Join Date: Feb 2005
Posts: 576
Time spent in forums: 5 Days 4 h 53 m 16 sec
Reputation Power: 63
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```
b49P23TIvg agrees: Nice! Extract the common statement to simplify the code.
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

Last edited by Dietrich : November 7th, 2012 at 02:52 PM.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Iteration count in a while loop