{"id":8691,"date":"2021-03-02T16:12:07","date_gmt":"2021-03-02T10:42:07","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=8691"},"modified":"2026-01-05T05:23:52","modified_gmt":"2026-01-05T10:23:52","slug":"python-break-continue-and-pass-statements","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-break-continue-and-pass-statements\/","title":{"rendered":"Python Break, Continue and Pass Statements in Loops"},"content":{"rendered":"\n<p>In Python, the Python Break statement applies only to the loop in which it is written that is, the innermost loop. Unlike languages such as <a href=\"https:\/\/www.h2kinfosys.com\/blog\/java-spring\/\" data-type=\"post\" data-id=\"15807\">Java<\/a> or PHP, Python does not support labeled <code>continue<\/code> statements that allow you to skip directly to the next iteration of an outer loop.<\/p>\n\n\n\n<p>To simulate a \u201ccontinue outer loop\u201d behavior from inside an inner loop, you can use one of the following common techniques:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Using a Flag Variable (Recommended for Clarity)<\/h2>\n\n\n\n<p>You can set a boolean flag inside the inner loop and evaluate it in the outer loop to decide whether to continue.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in range(3):\n    skip_outer = False\n    for j in range(3):\n        if i == 1 and j == 1:\n            skip_outer = True\n            break  # Exit the inner loop\n    if skip_outer:\n        continue  # Skips the remaining code in the outer loop for this iteration\n    print(f\"Processed i={i}\")\n<\/pre>\n\n\n\n<p><strong>Pros:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Very readable<\/li>\n\n\n\n<li>Clearly communicates intent<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. Refactoring Logic into a Function<\/h2>\n\n\n\n<p>By placing the nested loops inside a function, you can use <code>return<\/code> to exit the current outer-loop logic cleanly.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def process_data(i):\n    for j in range(3):\n        if i == 1 and j == 1:\n            return  # Ends this function call, mimicking a \"continue\" on the outer loop\n    print(f\"Processed i={i}\")\n\nfor i in range(3):\n    process_data(i)\n<\/pre>\n\n\n\n<p><strong>Pros:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clean and structured<\/li>\n\n\n\n<li>Ideal for complex logic<\/li>\n\n\n\n<li>Improves modularity and testability<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. Using <code>for...else<\/code> with <code>continue<\/code><\/h2>\n\n\n\n<p>In Python, the <code>else<\/code> block of a loop executes only if the loop completes without encountering a <code>break<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in range(3):\n    for j in range(3):\n        if i == 1 and j == 1:\n            break  # Triggers break, so the else block is skipped\n    else:\n        # Executes only if the inner loop finishes normally (no break)\n        print(f\"Finished inner loop for i={i}\")\n        continue  # Proceed to the next iteration of the outer loop\n    \n    # Executes only if the inner loop was interrupted by break\n    print(f\"Special logic for break at i={i}\")\n<\/pre>\n\n\n\n<p><strong>Pros:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>No additional variables required<\/li>\n\n\n\n<li>Leverages built-in language behavior<\/li>\n<\/ul>\n\n\n\n<p><strong>Cons:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Can be unintuitive for beginners<\/li>\n\n\n\n<li><code>else<\/code> behavior may feel counter-intuitive at first<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">4. Using Exceptions (Not Recommended for Routine Use)<\/h2>\n\n\n\n<p>You can define and raise a custom exception to escape directly to the outer loop.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class ContinueOuter(Exception):\n    pass\n\nfor i in range(3):\n    try:\n        for j in range(3):\n            if i == 1 and j == 1:\n                raise ContinueOuter()\n        print(f\"Processed i={i}\")\n    except ContinueOuter:\n        continue\n<\/pre>\n\n\n\n<p><strong>Cons:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Slower than normal control flow<\/li>\n\n\n\n<li>Generally considered an anti-pattern for standard looping logic<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding Python Loops<\/h2>\n\n\n\n<p>Before diving into break, continue, and pass, let\u2019s briefly review Python loops. Loops in Python allow us to execute a block of code multiple times. The two primary types of loops are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>For Loop<\/strong> \u2013 Iterates over a sequence (<a href=\"https:\/\/en.wikipedia.org\/wiki\/List\" rel=\"nofollow noopener\" target=\"_blank\">list<\/a>, tuple, dictionary, set, or string).<\/li>\n\n\n\n<li><strong>While Loop<\/strong> \u2013 Repeats as long as a given condition remains true.<\/li>\n<\/ul>\n\n\n\n<p>However, sometimes we need to alter the default execution of these loops. That\u2019s where the Python Break, continue, and pass statements come into play.<\/p>\n\n\n\n<p>Using loops are foundational concepts in Python and virtually every other programming language. When learning the <a href=\"https:\/\/www.h2kinfosys.com\/courses\/python-online-training\/\">Python Online Course Certification<\/a>, understanding how loops work is essential for writing efficient and readable code. In Python, there are two primary types of loops: the for loop and the while loop.<\/p>\n\n\n\n<p>A for loop is used to iterate over elements in an iterable such as a string, list, tuple, set, or other sequence, processing each item one by one. In contrast, a while loop repeatedly executes a block of code as long as a specified condition remains true, making it useful for tasks that depend on dynamic conditions rather than a fixed sequence.<\/p>\n\n\n\n<p>But when working with loops, you can change how the iteration is done using the python break, continue and pass statement. In this tutorial, we will discuss how to use these statements to alter the behavior of both for loops and while loops in Python. By the end of this tutorial, you will learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using the break Statement in Python Break<\/li>\n\n\n\n<li>Using the break statement in For Loops<\/li>\n\n\n\n<li>Using the Break Statement on While Loops.&nbsp;<\/li>\n\n\n\n<li>Using the break Statement with Nested Loops.&nbsp;<\/li>\n\n\n\n<li>The continue statement in Python Break<\/li>\n\n\n\n<li>Using the continue Statement in while loops.&nbsp;<\/li>\n\n\n\n<li>Using the continue statement in nested loops.&nbsp;<\/li>\n\n\n\n<li>Using the pass statement.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>Without further ado, let\u2019s jump into it.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the break Statement in Python<\/h2>\n\n\n\n<p>The Python Break statement is used to terminate a loop abruptly, based on another condition. When a Python Break statement is used in a nested loop, the inner loop does not run anytime the specific condition is met. It terminates the inner loop and goes on to the outer loop. Let\u2019s see how the break statement works for both for loops, while loops and nested for loops.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the break statement in For Loops<\/h2>\n\n\n\n<p>Let\u2019s say we have a list of names and we wish to stop the loop when a particular name is found, the Python Break statement can be used for. Refer to the code example shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">names = ['Femi', 'Ken', 'David', 'Mah', 'Iliana', 'Hannah', 'Ahmed']\n&nbsp;\n#loop over the list and terminates if the name 'David' is found.\nfor name in names:\n&nbsp;&nbsp;&nbsp;&nbsp;print (name)\n&nbsp;&nbsp;&nbsp;&nbsp;#set the condition for the break statement\n&nbsp;&nbsp;&nbsp;&nbsp;if name == 'David':\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print('David Found. Loop terminates here')\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\nFemi\nKen\nDavid\nDavid Found. Loop terminates here\n<\/pre>\n\n\n\n<p>As seen, the names after David were not printed. This is because the Python Break statement stops the iteration once the name, David, is found.&nbsp;<\/p>\n\n\n\n<p>Now, let\u2019s see how to do the same task with while loops.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the Break Statement on While Loops.&nbsp;<\/h2>\n\n\n\n<p>The code below shows how to perform the earlier task using while loops.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">names = ['Femi', 'Ken', 'David', 'Mah', 'Iliana', 'Hannah', 'Ahmed']\ni = 0\n#loop over the list and terminates if the name 'David' is found.\nwhile True:\n&nbsp;&nbsp;&nbsp;&nbsp;print(names[i])\n&nbsp;&nbsp;&nbsp;&nbsp;i += 1\n&nbsp;&nbsp;&nbsp;&nbsp;#set the condition for the break statement\n&nbsp;&nbsp;&nbsp;&nbsp;if names[i] == 'David':\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print('David Found. Loop terminates here')\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\nFemi\nKen\nDavid Found. Loop terminates here\n<\/pre>\n\n\n\n<p>Note that when looping with the condition while True, it is an infinite loop. When using while loops in such a manner, it is critical to have some specified that would be required to terminate the loop. Else, the code will continue to run until you run out of memory or hit an error.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the break Statement with Nested Loops.&nbsp;<\/h2>\n\n\n\n<p>A nested loop in Python is a loop inside another loop. When you use the Python Break statement inside the inner loop of a nested loop, the Python code terminates that loop and jumps to the outer loop. The outer loop keeps on running but terminates the inner loop whenever the condition is met. Let\u2019s see an example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">list_1 = [1, 2, 3]\nlist_2 = ['a', 'b', 'c', 'd']\n&nbsp;\nfor x in list_1:\n&nbsp;&nbsp;&nbsp;&nbsp;for y in list_2:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if y == 'c':\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(f\"Outer loop: {x}, Inner loop: {y}\")<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\nOuter loop: 1, Inner loop: a\nOuter loop: 1, Inner loop: b\nOuter loop: 2, Inner loop: a\nOuter loop: 2, Inner loop: b\nOuter loop: 3, Inner loop: a\nOuter loop: 3, Inner loop: b\nOuter loop: 4, Inner loop: a\nOuter loop: 4, Inner loop: b\n<\/pre>\n\n\n\n<p>Notice that the inner loop stops a \u2018b\u2019 without printing \u2018c\u2019 and \u2018d\u2019. The outer loop however prints all its elements. This is how the Python Break statement works in nested loops. Now, we move on to the continue statement<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The continue statement in Python<\/h2>\n\n\n\n<p>The continue statement is used to skip a particular iteration if a condition is met. Notice the difference between a continue statement and a break statement. While the Python Break statement stops the loop, the continue statement only skips that iteration and moves on to the next element. Let\u2019s see how to use the continue statement in Python.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">names = ['Femi', 'Ken', 'David', 'Mah', 'Iliana', 'Hannah', 'Ahmed']\n&nbsp;\n#loop over the list and skip the name 'David'.\nfor name in names:\n&nbsp;&nbsp;&nbsp;&nbsp;#set the condition for the continue statement\n&nbsp;&nbsp;&nbsp;&nbsp;if name == 'David':\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue\n&nbsp;&nbsp;&nbsp;&nbsp;print(name)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\nFemi\nKen\nMah\nIliana\nHannah\nAhmed\n<\/pre>\n\n\n\n<p>You\u2019d realize that the name \u2018David\u2019 was not printed. The same thing goes for a while loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the continue Statement in while loops.&nbsp;<\/h2>\n\n\n\n<p>The continue statement can be used in while loops as well. See an example below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">number = 1\nwhile number &lt;= 7:&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;if number == 5:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;number += 1\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;print(number)\n&nbsp;&nbsp;&nbsp;&nbsp;number +=&nbsp;1<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n1\n2\n3\n4\n6\n7\nNotice that the number 5 was not printed. \n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using the continue statement in nested loops.&nbsp;<\/h2>\n\n\n\n<p>We can apply the same principle in nested loops. Again, it skips the iteration for that loop and moves on with the next iteration.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">list_1 = [1, 2, 3]\nlist_2 = ['a', 'b', 'c', 'd']\n&nbsp;\nfor x in list_1:\n&nbsp;&nbsp;&nbsp;&nbsp;for y in list_2:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if y == 'c':\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(f\"Outer loop: {x}, Inner loop: {y}\")<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\nOuter loop: 1, Inner loop: a\nOuter loop: 1, Inner loop: b\nOuter loop: 1, Inner loop: d\nOuter loop: 2, Inner loop: a\nOuter loop: 2, Inner loop: b\nOuter loop: 2, Inner loop: d\nOuter loop: 3, Inner loop: a\nOuter loop: 3, Inner loop: b\nOuter loop: 3, Inner loop: d\n<\/pre>\n\n\n\n<p>Notice that this time, the outer loop, \u2018d\u2019 was printed. Only \u2018c\u2019 was skipped. This is how the continue statement works for nested loops.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the pass statement<\/h2>\n\n\n\n<p>A pass statement is simply a null statement. It tells the Python interpreter to do nothing once encountered. It is typically used when creating a class or a function that you do not want to populate with codes yet. Using the pass statement would run the empty class or function without an error.&nbsp;<\/p>\n\n\n\n<p>The pass statement can as well be used in loops. As mentioned, it does nothing to a loop. But again, it is useful when you wish to run an empty loop without errors. See the code below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for x in range(10):\n&nbsp;&nbsp;&nbsp;&nbsp;pass<\/pre>\n\n\n\n<p>Without the pass statement, the code would throw an error. However, the pass statement was used to create the loop without doing anything.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Comparison Table: <code>break<\/code> vs <code>continue<\/code> vs <code>pass<\/code><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Statement<\/th><th>Functionality<\/th><\/tr><tr><td><code>break<\/code><\/td><td>Stops the loop entirely.<\/td><\/tr><tr><td><code>continue<\/code><\/td><td>Skips the current iteration and moves to the next.<\/td><\/tr><tr><td><code>pass<\/code><\/td><td>Does nothing; acts as a placeholder.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Key Takeaways<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>Python Break<\/code> statement Terminates a loop when a condition is met.<\/li>\n\n\n\n<li>The <code>continue<\/code> statement <strong>skips<\/strong> the current iteration and moves to the next one.<\/li>\n\n\n\n<li>The <code>pass<\/code> statement <strong>acts as a placeholder<\/strong> and does nothing.<\/li>\n\n\n\n<li>These statements are essential for writing clean, efficient, and readable Python code.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Mastering Python Break, continue, and pass statements in loops enhances your ability to control program flow effectively. If you\u2019re looking to gain hands-on experience, H2K Infosys offers <a href=\"https:\/\/www.h2kinfosys.com\/courses\/python-online-training\/\">Python Programming Training Course<\/a> with expert-led training. Enroll today and take your Python skills to the next level!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Python, the Python Break statement applies only to the loop in which it is written that is, the innermost loop. Unlike languages such as Java or PHP, Python does not support labeled continue statements that allow you to skip directly to the next iteration of an outer loop. To simulate a \u201ccontinue outer loop\u201d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8694,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-8691","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=8691"}],"version-history":[{"count":2,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8691\/revisions"}],"predecessor-version":[{"id":33807,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8691\/revisions\/33807"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/8694"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=8691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=8691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=8691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}