Pass, Break and Continue Keywords in Python

Updated 20 April 2022

Hola on your next viaje in Python. Python is an absolutely asombroso programming language and this is the next step on the escalera of understanding. This guide will focus on the | pass |, | continue | and | break | keywords. These are all loop control statements which are tools to change the execution flow from its normal sequence. The control flow is the sequence in which individual statements, instructions or functions of a software program are executed and evaluated. Control structures are a flowchart method to represent this control flow within programming languages. These will be used in this tutorial to demonstrate the effects of these keywords. 

Keywords are the nuts and bolts vocabulary of any programming language, are reserved words and have special meaning. This special meaning can be commands or parameters which will be executed as soon as the computing device encounters the keyword when the code is run. For a great reference on keywords check out the guide All the Keywords in Python. By having a firm grasp on how to use these keywords effectively you will be able to muerte any programming scenario. The contents can be seen below.

There are several related tutorials on our website to help you become a coding mago. A great place to start your journey would be Python Rundown and Main Windows. This will also tell you where to download Python and access online Python emulators. Python is an excellent language to utilise with Raspberry Pi Boards such as Raspberry Pi 4 Model B 8GB. These are computing devices that can easily fit on the palm of your hand.

As always if you have any questions, queries or things to add please let us know your thoughts!


Overview of Pass, Break and Continue Keywords

A snake lollipop. And who else uses a lollipop? A traffic controller! And what do they do? Traffic controllers sometimes let cars Pass and Continue, other times they Break the flow of cars.Using loops in Python repeats and automates tasks in an efficient manner that would otherwise take many lines of code. For more on loops check out the guide Loops in Python. However, there may arise a situation where you want to exit the loop completely, ignore parts of the internal code for an iteration cycle or even skip an entire iteration cycle. Perhaps you may even want to create a new internal condition or stop a nested loop and start the next iteration of the external loop. All of this can be done with loop control statements!

Python has three loop control statements which can be seen below. These are all keywords with unique characteristics and effects. Within this tutorial whenever there is text/symbols that would be written inside a Python IDLE Shell or a Python Program Window (ergo typed as code) it will be denoted inside line symbols, |  |.

Loop control statements change the execution control flow of Python away from the normal sequence. Worth noting, when the control flow leaves the iteration straight away using these keywords, any variables that potentially would have been created in future lines inside the loop body will not exist. Effectively their scope is destroyed (or more correctly never created). Scope refers to the coding region from which a Python variable is accessible. Each variable is accessed using a unique identifier. Identifiers can also be referred to as namespace. A lifetime of an identifier depends upon the scope of each variable. The lifetime of an identifier comes to an end as soon as the scope of a variable ends. A namespace (identifiers) is the system Python uses to have a unique name for every variable or method within Python. Python itself maintains the namespace in the form of a Python dictionary.


CONTROL STRUCTURES

Control structures are a flowchart method to represent the control flow of programming languages. In computer science, the control flow is the order in which individual statements, function utilisation or instructions of a software program are executed. This is also referred to as the flow of control. Simply program flow is a general term which describes the order in which your lines of code are evaluated by the computing device.

Control structure flowcharts convey each step in the software program as boxes of various kinds. The order is demonstrated by connecting the boxes with arrows. The shape of the box represents the code line type and allow for easier identification. The shape of the box can represent either a command statement, a variable creation, a condition or a decision. A great reference point for these can be found at Control Structures in Python.

Three Simple Code Flowcharts Using Control Structures


Pass Keyword

The pass statement is a null statement. The pass statement acts as a placeholder and usually employed when there is no need for code or commands to be executed but a statement is still required to make the Python syntax to be correct. This is a great tool to use in places where your code will eventually go but has not been written yet. In these situations, the Pythonista simply types the | pass | keyword. In fact, you can use this keyword anywhere inside a Python script. The rules of Python syntax is that empty code is not allowed in loops, function definitions, class definitions, or if statements. By correctly using a pass statement you can avoid error messages for all empty code situations.

Now, for instance, no code within a while loops will cause a syntax error but you can have a while loop with no content. To do this you type the | pass | keyword inside the body of the while loop. When the control flow reaches the pass statement it will execute the while loop without issue. Python is happy with the syntax. The pass statement tells the interpreter to do nothing. Thus the while loop will run until the initialising condition is satisfied, the natural demise of a loop. The control flow will then transfer to the next statement immediately following the while loop. This continues the program. This demonstrates the | pass | keyword purpose, to maintain the Python syntax needed to permit continuous and persistent execution flow.

You may well ask what the difference between the | pass | keyword and comments created using the | # | symbol. Let me lay it clear, comments are completely ignored by the Python interpreter. On the other hand, the | pass | keyword is not ignored by the Python interpreter and instead commands the interpreter to do nothing. The | pass | keyword is a placeholder. The Python interpreter is the application that runs your Python script. The Python IDLE Shell is an example of this, for more information check out the guide Python Rundown and Main Windows.

Below is a script typed in the Python Programming Window with the results of the script printed to the Python IDLE Shell adjacent. Below demonstrates the | pass | keyword allowing the for loop structure to exist with no content. This keyword can also be seen again in an if statement. As you can see adjacent, the script has been run and the | pass | keyword does not interrupt or stop the print function from executing. 

Pass keyword being used in an If Statement and a For Loop 


Break Keyword

The break statement is a method inside Python which terminates the current loop and pushes the control flow out of the loop body to the next line of code in the script. The most common use for a break statement is in conjunction with a unique condition, different from the looping condition, which when met requires a rapid cancellation of the loop structure. With the | break | keyword it makes it possible to stop a loop before the Python interpreter has executed through all of the planned iterations (or before the original loop condition has been met).

A break statement must be put inside a loop body and are generally employed in combination with an if statement. An if statement is an excellent way to set up a condition. More information on if statements can be found in the guide If, Elif and Else Keywords in Python. The | break | keyword can be used in all Python loops, both while loop structures and for loop structures. If the break statement is executed inside a nested loop the keyword will stop the execution of the inner loop only.  More information on these looping structures can be found in the guide Looping in Python.  

Below is a script typed in the Python Programming Window with the results of the script printed to the Python IDLE Shell adjacent. Further below is a control structure flowchart representing this script. The flowchart demonstrates exactly the paths of execution that the computing device can do when this script is run. Take note, the loop structure is cancelled early and the element 0 is not printed to the Python IDLE Shell.

Break Statement example. On the hunt for a former president. Stop searching once found.

Break script example control structure flowchart annotated with code


Continue Keyword

The continue statement is a method inside Python which will return the control flow back to the start of the loop body. The continue statement effectively rejects all the unexecuted statements within the current iteration of the loop and instead pushes that control flow to the loop body beginning. Thus, the continue statement is an excellent tool to skip the effects of particular loop iterations.

The continue statement is a loop control statement with a lot of similarities to a break statement. A continue statement must be put inside a loop body and are generally employed in combination with an if statement. An if statement is an excellent way to set up a condition. The | continue | keyword can be used in all Python loops, both while loop structures and for loop structures. However, the effects of a continue statement are somewhat opposite to that of a break statement. The Python interpreter will not abandon the future iterations of the loops, instead, it just skips the remaining lines of code of a particular iteration. If a continue statement is executed inside a nested loop the keyword will skip one iteration of the inner loop only.

Below is a script typed in the Python Programming Window with the results of the script printed to the Python IDLE Shell adjacent. Further below is a control structure flowchart representing this script. The flowchart demonstrates exactly the paths of execution that the computing device can do when this script is run. Take note, the loop structure completes the initially planned iterations however the element 0 is not printed to the Python IDLE Shell.

Break Statement example. On the hunt for a former president. Once found continue search for other members of the dispatched leadership.

Continue example control structure flowchart annotated with code


Overall Comparison

Below is a comparison table between the keywords | pass |, | break | and | continue |. This should be a great reference point and useful tool to ensure the concepts of these keywords have been solidified.   

Pass, Break and Continue Keywords all compared in one place!


The Next Step

The next step in our coding adventures would be to learn deeper on how exactly to increase functionality of our Python code through user made functions. The guide Making Your Own Python Functions! will give you exactly this knowledge.  

After that natural next steps in our coding adventures would be string operations followed by list and tuple operations. Or perhaps creating your own functions. Or perhaps rad code examples using the knowledge we already have.


DOWNLOAD THE CODES

Below you can find all the code from the above examples. You will be able to run the codes here on your own computing device and modify the code at your leisure. Open it up with Python IDLE Shell and run it using the Python Programming Window.  

Attachment - Python-pass-break-continue-keywords-scripts.zip

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.