Google News
logo
Python Program to Else in for loop
In the following example of Python program that demonstrates how to use the `else` statement in a `for` loop :
Program :
# Else in for loop
fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
    print(fruit)
else:
    print("No more fruits left.")

print("Done.")
Output :
apple
banana
cherry
No more fruits left.
Done.
In the above program, we use a `for` loop to iterate over a list of fruits. Inside the loop, we use the `print()` function to print each fruit.

After the loop completes, the program executes the `else` statement, which prints the message "No more fruits left."

When the program exits, it continues to execute the `print()` statement outside the loop, which prints the message "Done."

You can use the `else` statement in a `for` loop to execute a block of code after the loop completes. The `else` block will only execute if the loop completes all iterations without encountering a `break` statement.