Difference between if: else: and if: continue

12,031

Solution 1

Usually we don't put statements while using continue which means when a specific condition then simply take cursor again to for loop's next round of iteration(without doing anything in case no actions are mentioned).

Where a normal if, else condition is used for simply true and false conditions, condition in if satisfies then execute its statements else go to else block and execute its statements then.

Solution 2

I believe it should depend on what you are doing to the balls. If you are doing something similar, lets say changing the size of the balls and you want blue ones to be a certain size and all the other ones to be another size I would use if/else. If what you are doing is conceptually different I would use the continue example.

If your goal is to only do something to the blue ball you should simply write:

for ball in balls:
    if not ball.blue:
        # do what you want to balls that aren't blue.

Solution 3

To me there is no difference in what can be achieved with these two structures. Is there an intended difference? Are there cases where one is quicker, more readable, etc?

There's no difference in how the code works. continue is basically the same as putting everything below in else.

But if that else part is long or uses a lot of indentation levels, and maybe some loops by itself, continue and break save a lot of space and allow for easier flow management!

E.g. imagine having a lot of cases you must skip the entry. That calls for if/else or continue. E.g. check whether some property exist - if not, skip the rest for that element. Then check its value - if it's something strange, again skip... That could make a lot of indentation levels or scopes that is hard to manage. contunue saves some headache.

Also, if you already wrote some code and only later realised you have some border cases, you can add a similar 'skip' thingy to the beginning of the loop and don't indent the rest. Indentation changes show up in git (unless you configure otherwise). In languages where indentation matter, those "changes" that do nothing may be confusing when you later go through your or someone else's commits.

Share:
12,031
leafystar
Author by

leafystar

Updated on June 15, 2022

Comments

  • leafystar
    leafystar almost 2 years

    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? Are there cases where one is quicker, more readable, etc?

  • Chris_Rands
    Chris_Rands almost 5 years
    disagree, no reason that continue should not be proceeded by other things. anyway, please don't respond until you've finished driving!