{"id":370,"date":"2019-07-04T07:27:57","date_gmt":"2019-07-04T07:27:57","guid":{"rendered":"http:\/\/askpython.com\/?p=370"},"modified":"2022-07-07T07:05:10","modified_gmt":"2022-07-07T07:05:10","slug":"python-continue-statement","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/python-continue-statement","title":{"rendered":"Python continue Statement"},"content":{"rendered":"\n<ul class=\"wp-block-list\"><li>Python continue statement is used to skip the execution of the current iteration of the loop.<\/li><li>We can\u2019t use continue statement outside the loop, it will throw an error as &#8220;<strong>SyntaxError: &#8216;continue&#8217; outside loop<\/strong>&#8220;.<\/li><li>We can use continue statement with\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/www.askpython.com\/python\/python-for-loop\" target=\"_blank\">for loop<\/a>\u00a0and\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/www.askpython.com\/python\/python-while-loop\" target=\"_blank\">while loops<\/a>.<\/li><li>If the continue statement is present in a\u00a0<strong>nested loop<\/strong>, it skips the execution of the inner loop only.<\/li><li>The &#8220;continue&#8221; is a\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/www.askpython.com\/python\/python-keywords\" target=\"_blank\"><strong>reserved keyword in Python<\/strong><\/a>.<\/li><li>Generally, the continue statement is used with the <a rel=\"noreferrer noopener\" label=\"if statement (opens in a new tab)\" href=\"https:\/\/www.askpython.com\/python\/python-if-else-elif-statement\" target=\"_blank\"><strong>if statement<\/strong><\/a> to determine the condition to skip the current execution of the loop.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Flow Diagram of the continue Statement<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"774\" height=\"930\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/continue-statement-flow-diagram.png\" alt=\"Continue Statement Flow Diagram\" class=\"wp-image-371\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/continue-statement-flow-diagram.png 774w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/continue-statement-flow-diagram-250x300.png 250w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/continue-statement-flow-diagram-768x923.png 768w\" sizes=\"auto, (max-width: 774px) 100vw, 774px\" \/><figcaption>Python continue Statement Flow Diagram<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python continue Statement Syntax<\/h2>\n\n\n\n<p>The continue statement syntax is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncontinue\n<\/pre><\/div>\n\n\n<p>We can\u2019t use any option, label or condition with the continue statement.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python continue Statement Examples<\/h2>\n\n\n\n<p>Let&#8217;s look at some examples of using the continue statement in Python.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. continue with for loop<\/h3>\n\n\n\n<p>Let&#8217;s say we have a sequence of integers. We have to skip processing if the value is 3. We can implement this scenario using for loop and continue statement.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nt_ints = (1, 2, 3, 4, 5)\n\nfor i in t_ints:\n    if i == 3:\n        continue\n    print(f&#039;Processing integer {i}&#039;)\n\nprint(&quot;Done&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"666\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-continue-statement-for-loop.png\" alt=\"Python Continue Statement For Loop\" class=\"wp-image-373\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-continue-statement-for-loop.png 706w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-continue-statement-for-loop-300x283.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-continue-statement-for-loop-24x24.png 24w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><figcaption>Python continue Statement with for Loop<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. continue statement with the while loop<\/h3>\n\n\n\n<p>Here is a simple example of using continue statement with the while loop.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncount = 10\n\nwhile count &gt; 0:\n    if count % 3 == 0:\n        count -= 1\n        continue\n    print(f&#039;Processing Number {count}&#039;)\n    count -= 1\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"716\" height=\"744\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-continue-statement-while-loop.png\" alt=\"Python Continue Statement While Loop\" class=\"wp-image-374\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-continue-statement-while-loop.png 716w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-continue-statement-while-loop-289x300.png 289w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-continue-statement-while-loop-24x24.png 24w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><figcaption>Python continue Statement with while Loop<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. continue statement with a nested loop<\/h3>\n\n\n\n<p>Let&#8217;s say we have a list of tuples to process. The tuple contains integers. The processing should be skipped for below conditions.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>skip the processing of tuple if its size is greater than 2.<\/li><li>skip the execution if the integer is 3.<\/li><\/ul>\n\n\n\n<p>We can implement this logic with nested for loops. We will have to use two continue statements for implementing above conditions.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlist_of_tuples = &#x5B;(1, 2), (3, 4), (5, 6, 7)]\n\nfor t in list_of_tuples:\n    # don&#039;t process tuple with more than 2 elements\n    if len(t) &gt; 2:\n        continue\n    for i in t:\n        # don&#039;t process if the tuple element value is 3\n        if i == 3:\n            continue\n        print(f&#039;Processing {i}&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"910\" height=\"776\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/continue-statement-with-nested-loop.png\" alt=\"Continue Statement With Nested Loop\" class=\"wp-image-375\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/continue-statement-with-nested-loop.png 910w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/continue-statement-with-nested-loop-300x256.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/continue-statement-with-nested-loop-768x655.png 768w\" sizes=\"auto, (max-width: 910px) 100vw, 910px\" \/><figcaption>Python continue Statement with Nested Loop<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Why Python doesn\u2019t support labeled continue statement?<\/h2>\n\n\n\n<p>Many popular programming languages support a labeled continue statement. It\u2019s mostly used to skip the iteration of the outer loop in case of nested loops. However, Python doesn\u2019t support labeled continue statement.<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/peps.python.org\/pep-3136\/\" target=\"_blank\">PEP 3136<\/a>\u00a0was raised to add label support to continue statement. But, it was rejected because it&#8217;s a very rare scenario and it will add unnecessary complexity to the language. We can always write the condition in the outer loop to skip the current execution.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python continue vs break vs pass<\/h2>\n\n\n\n<table class=\"tg\">\n<tr>\n<th class=\"tg-two\">continue<\/th>\n<th class=\"tg-two\">break<\/th>\n<th class=\"tg-two\">pass<\/th>\n<\/tr>\n<tr>\n<td class=\"tg-one\">The continue statement skips only the current iteration of the loop.<\/td>\n<td class=\"tg-one\">The break statement terminates the loop.<\/td>\n<td class=\"tg-one\">The pass statement is used to write empty code blocks.<\/td>\n<\/tr>\n<tr>\n<td class=\"tg-two\">We can use continue statement only inside a loop.<\/td>\n<td class=\"tg-two\">We can use break statement only inside a loop.<\/td>\n<td class=\"tg-two\">We can use pass statement anywhere in the Python code.<\/td>\n<\/tr>\n<tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Python continue statement is used to skip the execution of the current iteration of the loop. We can\u2019t use continue statement outside the loop, it will throw an error as &#8220;SyntaxError: &#8216;continue&#8217; outside loop&#8220;. We can use continue statement with\u00a0for loop\u00a0and\u00a0while loops. If the continue statement is present in a\u00a0nested loop, it skips the execution [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-370","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=370"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/370\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}