{"id":126,"date":"2023-06-02T19:23:26","date_gmt":"2023-06-02T19:23:26","guid":{"rendered":"http:\/\/python.garden\/index.php\/2023\/06\/02\/controlling-loop-execution-break-continue\/"},"modified":"2023-06-02T19:53:55","modified_gmt":"2023-06-02T19:53:55","slug":"controlling-loop-execution-break-continue","status":"publish","type":"post","link":"https:\/\/python.garden\/index.php\/2023\/06\/02\/controlling-loop-execution-break-continue\/","title":{"rendered":"Chapter 2.3: Controlling Loop Execution &#8211; Break, Continue"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Understanding and using break and continue within loops<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">In programming, it&#8217;s often necessary to have more control over the flow of execution within loops. Python provides two keywords, <code>break<\/code> and <code>continue<\/code>, which help to control loop execution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>break<\/code> Statement<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>break<\/code> statement is used to exit or &#8220;break&#8221; a loop prematurely. When encountered inside a loop, the <code>break<\/code> statement immediately terminates the loop, and program control resumes at the next statement following the loop.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">for i in range(10):\n    if i == 5:\n        break\n    print(i)<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In this code, the loop will break as soon as <code>i<\/code> equals <code>5<\/code>, and thus the numbers printed are <code>0<\/code> through <code>4<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>continue<\/code> Statement<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>continue<\/code> statement skips the remaining portion of the loop and immediately moves on to the next iteration of the loop. It does <em>not<\/em> terminate the loop; instead, it just skips the current iteration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">for i in range(10):\n    if i == 5:\n        continue\n    print(i)<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In this code, when <code>i<\/code> equals <code>5<\/code>, the <code>continue<\/code> statement is encountered and the rest of the loop for this iteration (the <code>print<\/code> statement) is skipped. The loop then moves on to the next iteration (where <code>i<\/code> equals <code>6<\/code>). Thus, the numbers printed are <code>0<\/code> through <code>4<\/code> and <code>6<\/code> through <code>9<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note: Be careful when using <code>break<\/code> and <code>continue<\/code> in nested loops (loops inside loops), as <code>break<\/code> and <code>continue<\/code> affect only the innermost loop in which they are placed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>break<\/code> and <code>continue<\/code> statements provide powerful ways to control the flow of your loops:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>break<\/code> when you want to terminate the loop prematurely.<\/li>\n\n\n\n<li>Use <code>continue<\/code> when you want to skip to the next iteration of the loop without executing the rest of the loop body.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">By understanding and using these keywords effectively, you can have greater control over your loops and make your Python programs more efficient and readable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understanding and using break and continue within loops In programming, it&#8217;s often necessary to have more control over the flow of execution within loops. Python provides two keywords, break and&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[120,99,119,94],"tags":[],"class_list":["post-126","post","type-post","status-publish","format-standard","hentry","category-break-continue","category-control-structures","category-controlling-loop-execution","category-introduction-to-python-2"],"featured_image_src":null,"author_info":{"display_name":"shababdoo","author_link":"https:\/\/python.garden\/index.php\/author\/shababdoo\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/comments?post=126"}],"version-history":[{"count":0,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/126\/revisions"}],"wp:attachment":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/media?parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/categories?post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/tags?post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}