The Difference Between Break, Continue and Pass

I thought I would do a quick programming tutorial explaining the difference between various control statements in Pythonic languages. I recently realised when streaming that some people might misunderstand the point of pass in GDScript (and Python) and I thought it might be useful to explain these concepts in case there’s any other misunderstandings.

Break

Let’s look at a simple for loop in GDScript.

for i in range(10):
    if i == 5:
       break

In the above code the program will iterate over the variable i, incrementing the value by 1 from values of 0 to 9 (range() is non inclusive of the final number in GDScript). When the value reaches 5 the loop will come to a stop. This is what break is used for, breaking out of the current loop. This can be useful for nested loops too.

for i in range(10):
    for j in range(10):
        if j == 5:
           break

In this example the first for loop will iterate over i from 0 to 9, however for each value of i the second loop will iterate over j from values 0 to 9. When j reaches 5, however, the inner loop will break, returning the flow to the end of the first (outer loop.) As such if you were to print the values for i and j you would see this:

i = 0, j = 0, j = 1, j = 1, j = 2, j = 3, j = 4, j = 5,
i = 1, j = 0, j = 1, j = 1, j = 2, j = 3, j = 4, j = 5,
i = 2, j = 0, j = 1, j = 1, j = 2, j = 3, j = 4, j = 5...

and so on until i = 9.

You can of course, have multiple breaks in nested loops that will break you out into the next highest scope. I won’t cover this here for brevity but if you would like me to cover this, let me know and I’m sure I can write a quick follow up.

Continue

Continue works similarly to break except instead of breaking OUT of the loop it terminates the current iteration and restarts the loop at the next iteration.

for i in range(10):
    if i == 5:
       continue
    print("i = %s" % i)

In this case you would notice this behaviour:

i = 0
i = 1
i = 2
i = 3
i = 4
# Notice the lack of 5. This is because the iteration at 5 is stopped before the print can be executed.
i = 6
i = 7
i = 8
i = 9

Pass

Pass is a unique case that exists in Python and GDScript. This is because these languages use indentation to determine scope and as such functions require at least 1 line of code to parse correctly. Take the example below:

func my_special_function(): # Unimplemented
    # some comment

func some_maths_function(a, b):
    return a + b

This is invalid code because my_special_function() has no statements in it. This is where pass comes in.

func my_special_function(): # Unimplemented
    # some comment
    pass

func some_maths_function(a, b):
    return a + b

The code is now valid. The catch is, and the main reason as to why I wrote this article… people seem to think that pass works like return, ending the current function. This isn’t true though, pass is essentially just a null/none statement. As such you can actually do something like this:

func my_special_function(): # Implemented now!
    # some comment
    pass
    print(some_maths_function(1, 2))

func some_maths_function(a, b):
    return a + b

func _ready():
    my_special_function()

Despite the fact that pass is at the top of my_special_function(), when it is called, the subsequent line is run and the code will print “3”. This is because pass is just ignored and stepped over, continuing to the end of the function. Again, the only purpose pass has is that it acts a non-functioning block of code to make the indentation make sense. This is true in both GDScript and Python, this is because GDScript takes a lot of its inspiration from Python for its design. In fact the languages are so similar (but not the same) that for the most part if you replace func with def and try to run any of these code snippets in Python and they will work.

Return

A quick final note about return. Return is used to return a value from a function. If you add any variable, value or statement (such as a function call) after return that value is returned.

e.g. return 1

If no value is give this will be null in GDScript or None in Python. Return can be used at any point in any depth of scope and the most recent function will end and return the value. Return is not required for void functions, the function will just run until its last statement then the code will return the previous point on the program and continue.

func some_fancy_function():
    for i in range(10):
        for j in range(5):
            if i + j == 11:
                return
            if i + j == 12:
                break
        if i == 8:
           break

I hope this has helped clarify the usage of pass in languages that use indentation to determine scope. If you’d like to talk programming, I often stream programming content on Twitch. If you like tabletop games, consider reading the blog posts about my current card game A Taste for the King.

Support This Site

If you enjoyed this content consider giving a tip on Ko-fi to help us keep producing content alongside our products.

You may also like...