Need help with for loop

Hi I need help with understanding following code . the output is 3 .

others = 0
for i in range(2):
for j in range(2):
if i != j:
others = others + 1
else:
others = others + 1
print(others)

I think the output should be 2 as the range is (0, 1)
but it increments 3 times giving 3 .why?

Which language is this? It has the vague look of Python code but your indentation disappeared in your post. You need to use a code fence (surround it with ```) to make the code readable. If this is Python, the indentation is critical since it changes how the code blocks run.

it’s python. sorry I will write it again

others = 0
for i in range(2):
for j in range(2):
if i != j:
other = others + 1
else:
others = others + 1
print(others)

it gives 3 as an out put. I don’t understand ,I think it should be 2

similarly there is another example like this

others = -1
for i in range(1,3):
for j in range (1,2):
if i == j:
others = others + 1
else:
others = others + 1
print(others)

In this example output is 2 whereas I think it should be 1. Please explain

sorry I am trying to write it with indentations but the interface removes all indentations once I hit reply button

You need to wrap it in a code fence like this:

```
def your_python_code():
  print('like this')
```
others = 0
for i in range(2):
      for j in range(2):
            if i != j:
                 other = others + 1
else:
others = others + 1
print(others)

it gives 3 as an out put. I don’t understand ,I think it should be 2 because loop goes to 'else' block two times as the condition in the 'if' block is not met.I don't understand how the variable 'others' is incremented the third time.


similarly there is another example like this

others = -1
for i in range(1,3):
       for j in range (1,2):
             if i == j:
                 others = others + 1
      else:
                others = others + 1
print(others)

In this example output is 2 whereas I think it should be 1. Please can you explain 

OK, so I think I know what is going on, here is what your code looks like:

others = 0
for i in range(2):
  for j in range(2):
    if i != j:
      others = others + 1
else:
  others = others + 1
print(others)

The execution of that code will produce 3 because of how the else clause works with a for loop. Basically the execution proceeds normally (i iterates through 0 and 1 with the inner loop going through j iterating through 0 and 1). When i is 0 and j is 1, we increment once and when i is 1 and j is 0 we increment again. So far so good, the current value of others is 2. However, the else clause is tied to the whole for loop and executes after the loop completes normally causing the value of others to be incremented one final time resulting in a value of 3 when others is printed.

By the way, as you wrote it is invalid (bad indentation). Also your code would result in a value of 1 since you are setting the other variable (not others with an s) which is not defined or used outside of the loop. In that case the only time others is updated is at the conclusion of the loop when it gets incremented once resulting in a value of 1 getting printed.

Fundamentally, you need to understand that the else clause on a for loop executes when the loop executes normally (in other words, unless you use a break statement to exit early). In both cases, the for loop is exiting normally and then the else clause is executing.

2 Likes