[Tutor] Else vs. Continue
Alan Gauld
alan.gauld at btinternet.com
Sun Nov 24 23:53:29 CET 2013
On 24/11/13 22:38, Alan Gauld wrote:
Responding to my own post, never a good sign :-(
> primes = []
> for n in range(1000)
> if n%2 == 0: even numbers aren't prime
> continue
> else:
> if isPrime(n):
> primes.append(n)
>
> Now the continue means the isPrime test never gets executed,
> saving a lot of processing. Without continue you would need
> to execute the test and the only way to save the time would
> be to move the n%2 test inside the isPrime() function
As soon as I posted it I realized that wasn't true, you could
do this instead...
for n in range(1000):
if n%2 == 1:
if isPrime(n):
primes.append(n)
So a bad example, sorry.
DeMorgan wins again.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list