mercredi 7 août 2019

Difference between if: else: and if: continue

Let's say I have an iterable: balls. I want to do something to all the balls in that loop that are not blue. As far as I see it I have two options:

Using if: else:

for ball in balls:
    if ball.blue:
        # can do something with blue ball
    else:
        # now execute code for all balls that are not blue

Using if: continue

for ball in balls:
    if ball.blue:
        # can do something with blue ball
        continue
    # now execute code for all balls that are not blue

To me there is no difference in what can be achieved with these two structures. Is there an intended difference?

Aucun commentaire:

Enregistrer un commentaire