# Newbie question: Explain this behavior

Discussion in 'Python' started by David Smith, Jul 14, 2005.

1. ### David SmithGuest

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

David Smith, Jul 14, 2005

2. ### George SakkisGuest

"David Smith" <> wrote in message news:db6tfq\$1kbh\$...
> 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

George Sakkis, Jul 15, 2005

3. ### Ross WilsonGuest

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

Ross Wilson, Jul 15, 2005
4. ### David SmithGuest

Re: Newbie question: Explain this behavior - a followup

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.

David Smith, Jul 15, 2005
5. ### maxGuest

Re: Newbie question: Explain this behavior - a followup

David Smith <> wrote in
news:db99ec\$2epm\$:

> 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

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

max, Jul 16, 2005
6. ### David SmithGuest

Re: Newbie question: Explain this behavior - a followup

max wrote:
> David Smith <> wrote in
> news:db99ec\$2epm\$:
>
>
>>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

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

David Smith, Jul 20, 2005