Indentation in statement "Else"

Asked

Viewed 318 times

9

What is the difference in the indentation of else "out of" the if. In this case it is to return the prime numbers up to the nth 'n' value.

First case:

for i in range(2, n):
    for j in range(2, i):
        if i % j == 0 and i != j:
            break
    else:
        print(i)

Second case:

for i in range(2, n//2):
    for j in range(2, i):
        if i % j == 0 and i != j:
            break
        else:
            print(i)

3 answers

6


In fact, the indentation serves to define which structure the else belongs. In the first case, the else belongs to the loop for (yes, that exists in Python), while in the second the else would belong to the if.

for i in range(2, n):
    for j in range(2, i):
        if i % j == 0 and i != j:
            break
    else:
        print(i)

In this case, the value of i shall vary from 2 to n-1 and the value of j shall vary from 2 to i-1. If a value of j that is divisive of i, the tie of j will be stopped. The else will be executed whenever the loop ends without interruption, that is, if the value of j come to i-1, which is its upper limit, without one of its values being a i. If the break, the block else will not be executed. It is worth mentioning that the condition i != j here is unnecessary because j will not reach the value i, as its upper limit is i-1.

As for the second case:

for i in range(2, n//2):
    for j in range(2, i):
        if i % j == 0 and i != j:
            break
        else:
            print(i)

The block else shall be executed at each loop on j where the value of j is not a divisor of i which will probably generate a result far from the expected.

In short, the structure else in a repeat loop will serve when your loop is a search structure and the desired value has not been found. Finishing the iteration on the object in the loop without interrupting it with break, the else will be executed.

  • 1

    In the books I read, I do not study anything just for a hobby, they did not comment on ELSE together with FOR, but this way generates the expected result. So this structure can be used is "pythonica"?

  • 1

    Yes, in a way, because you will be using the structure for exactly what it proposes to do.

5

In the first case the else is not from if is from for. If the for not interrupted then falls into the else. So in this case if the i be less than 2 o else will be executed, which seems to me to make no sense. This is very strange and makes the resource less useful, so despite the name but, is executed whenever the execution is normal and all steps of the loop are executed without forcibly exiting (break).

Here’s a simpler example of seeing the difference:

i = 1
for j in range(0, i):
    if j % 2 == 1:
        break
    print(j)
else:
    print("ok")
i = 3
for j in range(0, i):
    if j % 2 == 1:
        break
    print(j)
else:
    print("ok")

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

3

Tests to perform Else behavior


I performed the following tests, I thought the for with else could be affected by if, by internal variables and by break, to test I ran the following tests.


Test 1


  • unconditional
  • break-free

Code

for i in range(2, 10):
    print("Dentro" , i)
else:
    print("Fora" , i)

Upshot

Dentro 2
Dentro 3
Dentro 4
Dentro 5
Dentro 6
Dentro 7
Dentro 8
Dentro 9
Fora 9

Executed at the last index


Test 2


  • unconditional
  • break-hearted

Code

for i in range(2, 10):
  print("Dentro" , i)
  break
else:
  print("Fora" , i)

Upshot

Dentro 2

Lse is not executed


Test 3


  • paroled
  • break-free
  • external variable

Code

variavel_externa = 1
for i in range(2, 10):
  print("Dentro" , i)
  if(variavel_externa == 1):
    print('achou')
else:
  print("Fora" , i)

Upshot

Dentro 2
achou
Dentro 3
achou
Dentro 4
achou
Dentro 5
achou
Dentro 6
achou
Dentro 7
achou
Dentro 8
achou
Dentro 9
achou
Fora 9

Executed at the last index


Test 4


  • paroled
  • break-hearted
  • external variable

Code

variavel_externa = 1
for i in range(2, 10):
  print("Dentro" , i)
  if(variavel_externa == 1):
    print('achou')
    break
else:
  print("Fora" , i)

Upshot

Dentro 2
achou

Lse is not executed


Test 5


  • paroled
  • break-free
  • internal variable

Code

for i in range(2, 10):
  print("Dentro" , i)
  if(i == 2):
    print('achou')
else:
  print("Fora" , i)

Upshot

Dentro 2
achou
Dentro 3
Dentro 4
Dentro 5
Dentro 6
Dentro 7
Dentro 8
Dentro 9
Fora 9

Executed at the last index


Test 6


  • paroled
  • break-hearted
  • internal variable

Code

for i in range(2, 10):
  print("Dentro" , i)
  if(i == 2):
    print('achou')
    break
else:
  print("Fora" , i)

Upshot

Dentro 2
achou

Lse is not executed


Test with the question code


  • paroled
  • break-hearted
  • internal variable

Code

for i in range(2, 10):
    for j in range(2, i):
        if i % j == 0 and i != j:
            print('break', i)
            break
    else:
        print(i)

Upshot

2
3
break 4
5
break 6
7
break 8
break 9

Else is not executed when the break is not executed


Completion


It just doesn’t run Else when "break" runs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.