-2

I am new to programming and I came across these two similar problems:

Example 1:

for i in range(10):
  if i == 5:
    break
  else:
    print(i)
else:
    print("Here")

Output:

0 1 2 3 4

Example 2:

for i in range(5):
    if i == 5:
        break
    else:
        print(i)
else:
    print("Here")

Output:

0 1 2 3 4 Here

Question:

The second example is executing the else statement also. Why?

3
  • 1
    @righteousness_wealth In Python, else doesn't always pair with if. It can also go with a for or while loop, or a try...except construction. Commented Aug 12, 2023 at 10:08
  • A simple rule to remember: else: in a for loop means if no break. They just did not want to introduce a new keyword. Commented Aug 12, 2023 at 12:42
  • Does this answer your question? Why does python use 'else' after for and while loops? Commented Oct 14, 2023 at 12:10

2 Answers 2

4

Explanation

From this link it explains that for loops can have an else statement.

From the link:

The else clause executes after the loop completes normally. This means that the loop did not encounter a break statement.

So in your first example the loop hits the break point and so doesn't execute the else clause print("Here"). In the second example the last value of i in the loop is 4 so the break is never hit.

Your examples

# Example 1
for i in range(10):
    if i == 5:
        print("Break")
        break
    else:
        print(i)
else:
    print("Here")
# Output: 0 1 2 3 4 Break
    
    
# Example 2 
for i in range(5):
    if i == 5:
        print("Break")
        break
    else:
        print(i)
else:
    print("Here")
    
# Output: 0 1 2 3 4 Here

# Another example that doesn't cause break
for i in range(3):
    if i == 5:
        print("Break")
        break
    else:
        print(i)
else:
    print("Here")
    
# Output: 0 1 2 Here
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks... Didn't know a for loop could have an else block in python.
0

In python, we can use any loop and else. If the loop breaks, then else statement will not be executed. Else will be executed when the loop doesn't break.

For more ref: https://www.geeksforgeeks.org/using-else-conditional-statement-with-for-loop-in-python/

Correcting post the comment from @Slothrop

3 Comments

"If the loop is breaks then else statement will be executed or else no." The other way round. If the loop is not broken, the else statement will be executed. If the loop is broken, the else statement is not executed.
Yup, the other way round, thanks for correcting.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.