# Need help with for loop

**URL:** <https://forum.codewithmosh.com/t/need-help-with-for-loop/15008>\
**Category:** Uncategorized\
**Created:** [September 8, 2022, 11:02am UTC](https://forum.codewithmosh.com/t/need-help-with-for-loop/15008 "2022-09-08T11:02:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MariaK](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@MariaK](https://forum.codewithmosh.com/u/MariaK)\
**Post date:** [September 8, 2022, 11:02am UTC](https://forum.codewithmosh.com/t/need-help-with-for-loop/15008/1 "2022-09-08T11:02:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [September 8, 2022, 2:18pm UTC](https://forum.codewithmosh.com/t/need-help-with-for-loop/15008/2 "2022-09-08T14:18:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MariaK](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@MariaK](https://forum.codewithmosh.com/u/MariaK)\
**Post date:** [September 8, 2022, 3:50pm UTC](https://forum.codewithmosh.com/t/need-help-with-for-loop/15008/3 "2022-09-08T15:50:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MariaK](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@MariaK](https://forum.codewithmosh.com/u/MariaK)\
**Post date:** [September 8, 2022, 3:51pm UTC](https://forum.codewithmosh.com/t/need-help-with-for-loop/15008/4 "2022-09-08T15:51:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [September 8, 2022, 8:31pm UTC](https://forum.codewithmosh.com/t/need-help-with-for-loop/15008/5 "2022-09-08T20:31:02Z")

</div>

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

````auto
```
def your_python_code():
  print('like this')
```

````

---

<div class="post-metadata">

**Author:** ![MariaK](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@MariaK](https://forum.codewithmosh.com/u/MariaK)\
**Post date:** [September 8, 2022, 10:12pm UTC](https://forum.codewithmosh.com/t/need-help-with-for-loop/15008/7 "2022-09-08T22:12:45Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [September 8, 2022, 10:55pm UTC](https://forum.codewithmosh.com/t/need-help-with-for-loop/15008/8 "2022-09-08T22:55:12Z")

</div>

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

```python
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.
