{"id":45,"date":"2024-01-24T14:08:17","date_gmt":"2024-01-24T14:08:17","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=45"},"modified":"2024-01-24T14:08:19","modified_gmt":"2024-01-24T14:08:19","slug":"python-continue","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-continue\/","title":{"rendered":"Python continue"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about the Python&nbsp;<code>continue<\/code>&nbsp;statement and how to use it to control the loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python continue statement<\/h2>\n\n\n\n<p>The\u00a0<code>continue<\/code>\u00a0statement is used inside a\u00a0<code>for<\/code>\u00a0loop or a\u00a0<code>while<\/code>\u00a0loop. The\u00a0<code>continue<\/code>\u00a0statement skips the current iteration and starts the next one.<\/p>\n\n\n\n<p>Typically, you use the\u00a0<code>continue<\/code>\u00a0statement with an\u00a0<code>if<\/code>\u00a0statement\u00a0to skip the current iteration once a condition is\u00a0<code>True<\/code>.<\/p>\n\n\n\n<p>The following shows how to use the\u00a0<code>continue<\/code>\u00a0statement in a\u00a0<code>for<\/code>\u00a0loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>for index in range(n): if condition: continue <em># more code here<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And the following illustrates how to use the\u00a0<code>continue<\/code>\u00a0statement in a\u00a0<code>while<\/code>\u00a0loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>while condition1: if condition2: continue <em># more code here<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using Python continue in a for loop example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>The following example shows how to use the&nbsp;<code>for<\/code>&nbsp;loop to display even numbers from 0 to 9:<code>for index in range(10): if index % 2: continue print(index)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>0 2 4 6 8<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, iterate over a range of numbers from 0 to 9 using a&nbsp;<code>for<\/code>&nbsp;loop with the&nbsp;<code>range()<\/code>&nbsp;function.<\/li>\n\n\n\n<li>Second, if the index is an odd number, skip the current iteration and start a new one. Note that the&nbsp;<code>index % 2<\/code>&nbsp;returns&nbsp;<code>1<\/code>&nbsp;if the&nbsp;<code>index<\/code>&nbsp;is an odd number and 0 if the&nbsp;<code>index<\/code>&nbsp;is an even number.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Using Python continue in a while loop example<\/h2>\n\n\n\n<p>The following example shows how to use the\u00a0<code>continue<\/code>\u00a0statement to display odd numbers between 0 and 9 to the screen:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><em># print the odd numbers<\/em> counter = 0 while counter &lt; 10: counter += 1 if not counter % 2: continue print(counter)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>1 3 5 7 9<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define the&nbsp;<code>counter<\/code>&nbsp;variable with an initial value of zero<\/li>\n\n\n\n<li>Second, start the loop as long as the&nbsp;<code>counter<\/code>&nbsp;is less than 10.<\/li>\n\n\n\n<li>Third, inside the loop, increase the&nbsp;<code>counter<\/code>&nbsp;by one in each iteration. If the&nbsp;<code>counter<\/code>&nbsp;is an even number, skip the current iteration. Otherwise, display the&nbsp;<code>counter<\/code>&nbsp;to the screen.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about the Python&nbsp;continue&nbsp;statement and how to use it to control the loop. Introduction to the Python continue statement The\u00a0continue\u00a0statement is used inside a\u00a0for\u00a0loop or a\u00a0while\u00a0loop. The\u00a0continue\u00a0statement skips the current iteration and starts the next one. Typically, you use the\u00a0continue\u00a0statement with an\u00a0if\u00a0statement\u00a0to skip the current iteration once a condition is\u00a0True. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-45","post","type-post","status-publish","format-standard","hentry","category-3-control-flow"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"predecessor-version":[{"id":46,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/45\/revisions\/46"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}