Newbie question: Explain this behavior

D

David Smith

Why does code snippet one work correctly, but not two. The only
difference is the placement of the "else". I know that indentation
affects execution, but how does it change behavior in the following
examples? Thank you.

1. for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
# loop fell through without finding a factor
print n, 'is a prime number'


Output:

2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3



2. for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
# loop fell through without finding a factor
print n, 'is a prime number'

Output:

3 is a prime number
4 equals 2 * 2
5 is a prime number
5 is a prime number
5 is a prime number
6 equals 2 * 3
7 is a prime number
7 is a prime number
7 is a prime number
7 is a prime number
7 is a prime number
8 equals 2 * 4
9 is a prime number
9 equals 3 * 3
 
G

George Sakkis

David Smith said:
Why does code snippet one work correctly, but not two. The only
difference is the placement of the "else". I know that indentation
affects execution, but how does it change behavior in the following
examples? Thank you.

[snipped]

In the second example, "else" corresponds to "if", while in the first one it corresponds to "for".
The tutorial example on "for ... else" loops
(http://docs.python.org/tut/node6.html#SECTION006400000000000000000) uses the prime number
computation you posted, so read it again if it's not clear what the "else" clause does with loops.

George
 
R

Ross Wilson

Why does code snippet one work correctly, but not two. The only
difference is the placement of the "else". I know that indentation
affects execution, but how does it change behavior in the following
examples? Thank you.

1. for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
# loop fell through without finding a factor
print n, 'is a prime number'
<snip>

A quote from "Python: Essential Reference (2/e)" (p56) says it best:

"The 'else' clause of a loop executes only if the loop runs to completion.
This either occurs immediately (if the loop wouldn't execute at all) or
after the last iteration. On the other hand, if the loop is terminated
early using the 'break' statement, the 'else' clause is skipped."

Ross
 
D

David Smith

First, thanks to those who offered answers. They didn't really answer
my question, only because I had not worked through the example
sufficiently well. Doing this, I believe I understand what is
happening, and, if my understanding is correct, have discovered that for
other beginning and ending values for the two range statements, the
example doesn't work.

Given that the beginning and ending values for the inner range statement
are the same, the inner range statement will never be executed for its
first iternation; the else will be. This is not correct. Simply make
the beginning value a non-prime number, and the program still prints out
that that number is prime. Changing both beginning and ending values on
the two statements, the ouput is differentially buggy.
 
M

max

range statements, the example doesn't work.

Given that the beginning and ending values for the inner range
statement are the same, the inner range statement will never be

Is your question about the semantics of for else blocks or about the
suitability of the algorithm given in the example? The for else block
is behaving exactly as expected...
range(1,1) []
range(500,500) []

see
http://groups-
beta.google.com/group/comp.lang.python/browse_frm/thread/d6c084e791a00
2f4?q=for+else&hl=en&

for a good explanation of when the else part of the loop is executed.
Basically, whenever the loop is exited normally, which is what happens
when you iterate over an empty list like the one returned by
range(1,1)


max
 
D

David Smith

max said:
Is your question about the semantics of for else blocks or about the
suitability of the algorithm given in the example? The for else block
is behaving exactly as expected...

Good question. The question was directed at the latter, the suitability
of algorithm for determining prime numbers.

[]


see
http://groups-
beta.google.com/group/comp.lang.python/browse_frm/thread/d6c084e791a00
2f4?q=for+else&hl=en&

for a good explanation of when the else part of the loop is executed.
Basically, whenever the loop is exited normally, which is what happens
when you iterate over an empty list like the one returned by
range(1,1)


max
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top