Skip to Content
Learn
Loops
For / else

Just like with while, for loops may have an else associated with them.

In this case, the else statement is executed after the for, but only if the for ends normally—that is, not with a break. This code will break when it hits 'tomato', so the else block won’t be executed.

Instructions

1.

Click Run to see how for and else work together.

Folder Icon

Take this course for free

Already have an account?