Velocity Reviews > Newbie question: Explain this behavior

# Newbie question: Explain this behavior

David Smith
Guest
Posts: n/a

 07-14-2005
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

George Sakkis
Guest
Posts: n/a

 07-15-2005
"David Smith" <(E-Mail Removed)> wrote in message news:db6tfq\$1kbh\$(E-Mail Removed)...
> 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.htm...00000000000000) uses the prime number
computation you posted, so read it again if it's not clear what the "else" clause does with loops.

George

Ross Wilson
Guest
Posts: n/a

 07-15-2005
On Thu, 14 Jul 2005 15:46:40 -0700, David Smith wrote:

> 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

David Smith
Guest
Posts: n/a

 07-15-2005
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.

max
Guest
Posts: n/a

 07-16-2005
David Smith <(E-Mail Removed)> wrote in
news:db99ec\$2epm\$(E-Mail Removed):

> 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-
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

David Smith
Guest
Posts: n/a

 07-20-2005
max wrote:
> David Smith <(E-Mail Removed)> wrote in
> news:db99ec\$2epm\$(E-Mail Removed):
>
>
>>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...
>
>

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

>>>>range(1,1)

>
> []
>
>>>>range(500,500)

>
> []
>
>
> see
> http://groups-
> 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
>
>