Within a for loop, does every if statement need an else?


#1

Question

In the context of this exercise, within a Python for loop, does, or should, every if statement have an else statement?

Answer

No, for if statements in general, whether or not they are part of a for loop, they do not always need to be followed by an else statement. (This may be helpful to keep in mind for this exercise!)

If nothing else should happen if the condition is False, then it might make sense to exclude the else entirely. This not only saves space, by reducing the code by a few lines, but can also potentially speed up a program, by moving to the next iteration of a loop immediately instead of running additional code per iteration.