{"id":7390,"date":"2023-01-29T07:24:26","date_gmt":"2023-01-29T01:54:26","guid":{"rendered":"https:\/\/tutorpython.com\/?p=7390"},"modified":"2023-10-19T15:09:09","modified_gmt":"2023-10-19T09:39:09","slug":"python-break-continue-statement","status":"publish","type":"post","link":"https:\/\/tutorpython.com\/python-break-continue-statement","title":{"rendered":"Python Break &#038; Continue Statement"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">In This Article<\/p>\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/tutorpython.com\/python-break-continue-statement\/#Python_Break_Statement\" >Python Break Statement<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/tutorpython.com\/python-break-continue-statement\/#Python_Break_Statement_with_For_Loop\" >Python Break Statement with For Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/tutorpython.com\/python-break-continue-statement\/#Python_Break_Statement_with_While_Loop\" >Python Break Statement with While Loop<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/tutorpython.com\/python-break-continue-statement\/#Python_Continue_Statement\" >Python Continue Statement<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/tutorpython.com\/python-break-continue-statement\/#Python_Continue_Statement_with_For_Loop\" >Python Continue Statement with For Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/tutorpython.com\/python-break-continue-statement\/#Python_Continue_Statement_with_While_Loop\" >Python Continue Statement with While Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/tutorpython.com\/python-break-continue-statement\/#Python_Continue_Statement_with_Nested_Loops\" >Python Continue Statement with Nested Loops<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<p>In this article, we are going to discuss the <a href=\"https:\/\/tutorpython.com\/tutorial\/python-break-continue-statement\">break &amp; continue statements in Python<\/a>. The <strong>break<\/strong> statement allows a programmer to exit a loop early, while the <strong>continue<\/strong> statement allows a programmer to skip an iteration of a loop.<\/p>\n<p>These statements can be incredibly useful in certain situations and can help to improve the efficiency and readability of our code. In this post, we will be diving deep into the usage and practical applications of these statements and providing examples to help explain their functionality.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Python_Break_Statement\"><\/span>Python Break Statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <strong>break<\/strong> statement in Python is used to exit a loop early before the loop has completed all of its iterations. This can be useful in situations where a certain condition has been met and there is no need to continue iterating through the loop.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Python_Break_Statement_with_For_Loop\"><\/span>Python Break Statement with For Loop<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Here is an example of how the <strong>break<\/strong> statement can be used in a <a href=\"https:\/\/tutorpython.com\/tutorial\/for-loops-in-python\" target=\"_blank\" rel=\"noopener\"><strong>for<\/strong> loop<\/a><\/p>\n<pre><code class=\"language-python\">for number in my_list:\r\n    if number % 2 == 0:\r\n        print(f\"The first even number is {number}\")\r\n        break\r\n<\/code><\/pre>\n<p>Here, the loop iterates through the list <strong>my_list<\/strong> and checks each number to see if it is <strong>even<\/strong>. If an even number is found, the loop exits early and the number is printed.<\/p>\n<p><strong>Note:<\/strong> It&#8217;s important to note that <strong>the break statement only exits the innermost loop in which it is used<\/strong>. If we want to exit multiple loops, we can use a loop control variable or flag variable along with the break statement.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Python_Break_Statement_with_While_Loop\"><\/span>Python Break Statement with While Loop<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The break statement in Python can also be used within a <a href=\"https:\/\/tutorpython.com\/tutorial\/while-loop-in-python\" target=\"_blank\" rel=\"noopener\"><strong>while<\/strong> loop<\/a> to exit the loop early. A while loop repeatedly executes a block of code as long as a certain condition is true.<\/p>\n<p>The break statement can be used to exit the loop when a certain condition is met before the loop&#8217;s condition becomes false.<\/p>\n<p>Example of Python break statement in a while loop-<\/p>\n<pre><code class=\"language-python\">i = 0\r\nwhile i &lt; 10:\r\n    print(i)\r\n    if i == 5:\r\n        break\r\n    i += 1\r\n# output: 0 1 2 3 4 5\r\n<\/code><\/pre>\n<p>Here, the loop starts with the variable <strong>i<\/strong> being 0 and will continue to execute as long as <strong>i<\/strong> is less than 10.<\/p>\n<p>The loop will print the current value of <strong>i<\/strong> and then increment <strong>i<\/strong> by 1. Once the value of <strong>i<\/strong> is equal to 5, the loop will exit early using the <strong>break<\/strong> statement. This means that the code inside the loop will stop being executed and the program will continue to run the next line of code after the while loop.<\/p>\n<p><strong>Note:<\/strong> It&#8217;s important to be careful when using the <strong>break<\/strong> statement inside a <strong>while<\/strong> loop, as it can be easy to create an <strong>infinite loop<\/strong> if the condition that causes the loop to exit is never met. This can lead to the program running indefinitely and consuming a lot of resources.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Python_Continue_Statement\"><\/span>Python Continue Statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <strong>continue<\/strong> statement in Python is used to skip the current <a href=\"https:\/\/tutorpython.com\/tutorial\/iterators-in-python\" target=\"_blank\" rel=\"noopener\">iteration<\/a> of a loop and move on to the next iteration. This can be useful in situations where we want to skip certain iterations based on a certain condition.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Python_Continue_Statement_with_For_Loop\"><\/span>Python Continue Statement with For Loop<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Example of <strong>continue<\/strong> statement used in a <strong>for<\/strong> loop:<\/p>\n<pre><code class=\"language-python\">for number in my_list:\r\n    if number % 2 == 0:\r\n        continue\r\n    print(number)\r\n<\/code><\/pre>\n<p>Here, the loop iterates through the list <strong>my_list<\/strong> and checks each number to see if it is <strong>even<\/strong>. If an even number is found, the current iteration is skipped using the <strong>continue<\/strong> statement and the program moves on to the next iteration of the loop. If the number is odd, it is printed.<\/p>\n<p><strong>Note:<\/strong> It&#8217;s important to note that the continue statement only skips the current iteration of the loop in which it is used. It doesn&#8217;t exit the loop or change the control flow of the program.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Python_Continue_Statement_with_While_Loop\"><\/span>Python Continue Statement with While Loop<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The <strong>continue<\/strong> statement in Python can also be used within a <strong>while<\/strong> loop to skip the current iteration and move on to the next one. A while loop repeatedly executes a block of code as long as a certain condition is <strong>true<\/strong>. The continue statement can be used inside the loop&#8217;s block to skip the current iteration when a certain condition is met.<\/p>\n<p>Example of continue statement in Python while loop-<\/p>\n<pre><code class=\"language-python\">i = 0\r\nwhile i &lt; 10:\r\n    i += 1\r\n    if i % 2 == 0:\r\n        continue\r\n    print(i)\r\n#output: 1 3 5 7 9\r\n<\/code><\/pre>\n<p>Here, the loop starts with the variable <strong>i<\/strong> being 0 and will continue to execute as long as <strong>i<\/strong> is less than 10. The loop will increment <strong>i<\/strong> by 1 and check if <strong>i<\/strong> is an even number using the modulo operator. If the number is even, the loop will skip the current iteration using the continue statement and move on to the next one. If the number is odd, it will print the current value of i. This way, the loop will only print the odd numbers from 1 to 9.<\/p>\n<p><strong>Note:<\/strong> It&#8217;s important to remember that when the continue statement is executed, the code below it will not be executed for that iteration. Also, the continue statement does not affect the loop&#8217;s condition, so the loop will continue executing as long as the condition is true.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Python_Continue_Statement_with_Nested_Loops\"><\/span>Python Continue Statement with Nested Loops<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The <strong>continue<\/strong> statement can also be used within <strong>nested<\/strong> loops in Python, allowing us to skip certain iterations of inner loops based on a certain condition. A nested loop is a loop that is inside another loop, and it allows you to iterate over multiple sequences at once.<\/p>\n<p>Example:<\/p>\n<pre><code class=\"language-python\">for i in range(3):\r\n    for j in range(3):\r\n        if j == 1:\r\n            continue\r\n        print(i, j)\r\n#output: \r\n0 0\r\n0 2\r\n1 0\r\n1 2\r\n2 0\r\n2 2\r\n<\/code><\/pre>\n<p>Here, the outer loop iterates over the range 0 to 2 using the variable <strong>i,<\/strong> and the inner loop iterates over the range 0 to 2 using the variable <strong>j<\/strong>. The inner loop has an <strong>if<\/strong> statement that checks if the value of <strong>j<\/strong> is 1. If it is, the current iteration of the inner loop is skipped using the continue statement and the program moves on to the next iteration of the inner loop. If the value of <strong>j<\/strong> is not 1, then the program prints the current values of <strong>i<\/strong> and <strong>j<\/strong>.<\/p>\n<p><strong>Note:<\/strong> When using <strong>continue<\/strong> statements within <strong>nested<\/strong> loops, it&#8217;s important to be mindful of the indentation, as it should be inside the inner loop. Otherwise, it will skip the outer loop iteration instead of the inner one.<\/p>\n<h2>Conclusion<\/h2>\n<p>In conclusion, the <a href=\"https:\/\/tutorpython.com\/tutorial\/python-break-continue-statement\" target=\"_blank\" rel=\"noopener\">break and continue statements in Python<\/a> are important tools for controlling the flow of loops.<\/p>\n<p>The <strong>break<\/strong> statement is particularly useful when we need to exit a loop early once a certain condition is met. It can be used in both <a href=\"https:\/\/tutorpython.com\/tutorial\/for-loops-in-python\" target=\"_blank\" rel=\"noopener\"><strong>for<\/strong><\/a> and <a href=\"https:\/\/tutorpython.com\/tutorial\/while-loop-in-python\" target=\"_blank\" rel=\"noopener\"><strong>while<\/strong> loops<\/a> and it helps to avoid infinite loops and unnecessary iterations.<\/p>\n<p>The <strong>continue<\/strong> statement, on the other hand, is useful when we want to skip certain iterations of a loop based on a certain condition. It allows us to continue iterating through a loop without performing certain actions during certain iterations. It can also be used in both <strong>for<\/strong> and <strong>while<\/strong> loops.<\/p>\n<p>They can help to improve the efficiency and readability of our code by allowing us to <a href=\"https:\/\/tutorpython.com\/tutorial\/python-break-continue-statement\" target=\"_blank\" rel=\"noopener\">exit a loop early<\/a> or skip certain iterations based on a certain condition.<\/p>\n<p>It is important to use them with caution and test the code to ensure that the loop behaves as expected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to discuss the break &amp; continue&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[28],"tags":[],"class_list":["post-7390","post","type-post","status-publish","format-standard","hentry","category-tutorial"],"acf":[],"_links":{"self":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/7390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/comments?post=7390"}],"version-history":[{"count":8,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/7390\/revisions"}],"predecessor-version":[{"id":8802,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/7390\/revisions\/8802"}],"wp:attachment":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/media?parent=7390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/categories?post=7390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/tags?post=7390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}