8

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?

leafystar
  • 129
  • 1
  • 1
  • 7
  • why do you think that the usage of different keywords should necessarily lead to different outcome – mangusta Aug 07 '19 at 13:27
  • 3
    no difference for such a simple example- sometimes i prefer the `continue` idiom to prevent another layer of indentation, but it's quite subjective – Chris_Rands Aug 07 '19 at 13:29
  • I think it's a matter of preference. I'd add the option of if not ball.blue: without any else or continue. – mauve Aug 07 '19 at 13:31
  • 1
    it is subjective, if there was some PEP guidance or docs i'd say follow that, but otherwise it's a matter of taste for such trivial examples – Chris_Rands Aug 07 '19 at 13:39
  • `...do something to all...that are not blue...` You left another option out: `if not ball.blue: do stuff` - no need for `pass` or `continue`. – wwii Aug 07 '19 at 13:57

5 Answers5

2

In the case you've shown there is no difference as there is no logic after if-else statement. Generally continue is used to skip to another iteration of for loop, so in the following case you would't be able to translate it so easily:

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

If you would replace else with continue at the end of if, the last line wouldn't be reached.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
1

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.
TheArcticWalrus
  • 138
  • 3
  • 11
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.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 5
    disagree, no reason that `continue` should not be proceeded by other things. anyway, please don't respond until you've finished driving! – Chris_Rands Aug 07 '19 at 13:38
1

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.

h4z3
  • 5,265
  • 1
  • 15
  • 29
0

The only connection between continue and if is that you usually have a condition to decide if you want to continue. continue is used to end a single iteration of a loop, such as the for loop in your example, without exiting the entire loop (break does that). Since either branch here also ends the iteration, you have no practical difference in this example.

for ball in balls:
    if ball.blue:
        pass
    elif ball.red:
        pass
    else:
        pass

    if ball.static:
        continue

    ball.process_physics()

Any branching execution path could be transformed into another structure, such as if not ball.static: ball.process_physics(), but the continue informs us there can't be something like an else further down doing more work in this iteration. It's an aid in knowing the execution path folds back to the loop. (There could be things like finally blocks, but we'd have seen the try if there was.)

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26