-2
for n in range(3,10):
    for m in range (3,n):
        if n%m==0:
            print(n, "not prime")
            break
   else :
       print(n,"prime")

for n in range(3,10):
    for m in range (3,n):
        if n%m==0:
            print(n, "not prime")
       else :
          print(n,"prime")

I want to know difference between use of else in both code

user2782906
  • 117
  • 1
  • 8

3 Answers3

0

Code Example 1 : This code breaks from the loop as soon as we identify that n cannot be a prime number. The else statement is associated with the 'for m' loop, and executes whenever the for loop exits normally (i.e. that is it gets to the end of the range without a break)

for n in range(3,10):
    for m in range (3,n):
        if n%m==0:
            print(n, "not prime")
            break
    else :
       print(n,"prime")

This use of else is not a hack (as has been suggested elsewhere - a hack being something that shouldn't really work but does) - it is a deliberately designed part of the language, and is designed for exactly cases like these.

Code Example 2 : Here the code does not break when we know that n is a not prime and the else here is associated with the if and executes whenever the conditional on the if statement is false (i.e. when n%m !=0). This version is buggy, as potentially you will get told that a given number is both prime and not prime multiple times.

for n in range(3,10):
    for m in range (3,n):
        if n%m==0:
            print(n, "not prime")
        else :
            print(n,"prime")

In Summary - Code Example 1 is correct, in that it correctly reports for each number between 3 and 9 whether it is a prime or not. Strictly speaking the inner loop could be :

 for m in range(3, n/2):

with no loss of functionality.

Tony Suffolk 66
  • 9,358
  • 3
  • 30
  • 33
0
for n in range(3,10):
    for m in range (3,n):
        if n%m==0:
            print(n, "not prime")
            break
    else:
       print(n,"prime")

output:-

(3, 'prime')
(4, 'prime')
(5, 'prime')
(6, 'not prime')
(7, 'prime')
(8, 'not prime')
(9, 'not prime')

here break break the inner for loop and execute else statement. while in 2nd

for n in range(3,10):
    for m in range (3,n):
        if n%m==0:
            print(n, "not prime")
        else :
          print(n,"prime")

ouput:-

(4, 'prime')
(5, 'prime')
(5, 'prime')
(6, 'not prime')
(6, 'prime')
(6, 'prime')
(7, 'prime')
(7, 'prime')
(7, 'prime')
(7, 'prime')
(8, 'prime')
(8, 'not prime')
(8, 'prime')
(8, 'prime')
(8, 'prime')
(9, 'not prime')
(9, 'prime')
(9, 'prime')
(9, 'prime')
(9, 'prime')
(9, 'prime')

it continues with in the inner loop. check out this for for else clauses.

Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24
-1

This is a hack for early break the outer loop, currently not a syntax error. For example,

https://stackoverflow.com/a/654002/921082

But I wouldn't suggest you to do it since it often breaks understanding. You can always use True/False flag to stop the outer loop if inner loop stops.

Community
  • 1
  • 1
tomriddle_1234
  • 3,145
  • 6
  • 41
  • 71
  • This is... bizarre. I had no idea. So it goes into the else only if we don't break? That's actually sort of cool; you can code a clean up clause to the loop using else, in other words. – Stephen Lazaro Oct 08 '14 at 06:45
  • 1
    -1: 1) it is not a hack (it really is a standard python feature, albeit weird) and 2) breaking out of the loop is what the break statement does, you don't explain at all what the 'else' does, which actually was the question. – KillianDS Oct 08 '14 at 06:47
  • Definitely not a hack ! – Tony Suffolk 66 Oct 08 '14 at 07:10
  • It is a hack, if your team has non-python programmer, image how this would confuse him. – tomriddle_1234 Oct 08 '14 at 08:14
  • 1
    Languages differ, it's not because feature x is only in language Y it's a hack. That way, you can see the whole of java as a hack because it uses features haskell doesn't. – KillianDS Oct 08 '14 at 08:45