{"id":343,"date":"2023-02-13T16:03:18","date_gmt":"2023-02-13T16:03:18","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=343"},"modified":"2023-02-15T09:13:57","modified_gmt":"2023-02-15T09:13:57","slug":"while-loops","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-2\/while-loops\/","title":{"rendered":"While loops"},"content":{"rendered":"\n<p>A while loop continuously loops until a conditional check is no longer True.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>The code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>counter = 0\nwhile counter &lt; 5:\n    print(counter)\n    counter += 1<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>Gives an output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1\n2\n3\n4<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>In this example the loop checks if the counter variable is less than 5. So long as that check returns a boolean True then the code inside the loop will run.<\/p>\n\n\n\n<p>Be careful with while loops to avoid infinite looping. In the code below the programmer forgot the line that changes the value of counter. As such it always stays at 0 which will never be equal or greater than 5.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>counter = 0\nwhile counter &lt; 5:\n    print(counter)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Break<\/h2>\n\n\n\n<p>Just as before with the for loop, the break keyword will stop the loop and go to the first line of code after it.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>The code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>counter = 0\nvalue_to_check = 3\n\nwhile counter &lt; 5:\n    print(counter)\n    if value_to_check == counter:\n        break\n    counter += 1<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>Gives an output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1\n2\n3<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Continue<\/h2>\n\n\n\n<p>Same as with the for loop but remember when the continue line is encountered, the program jumps back to the start of the loop section. Because of this it is important to remember to change the value used in the conditional check before the continue line if using a While loop. Otherwise the program can get stuck in an infinite loop.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<h2 class=\"greentext wp-block-heading\">Good<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>counter = 0\nvalue_to_check = 3\nwhile counter &lt; 5:\n    counter += 1\n    if value_to_check == counter:\n        continue\n    print(counter)<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<h2 class=\"redtext wp-block-heading\">Bad<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>counter = 0\nvalue_to_check = 3\nwhile counter &lt; 5:\n    if value_to_check == counter:\n        continue\n    counter += 1\n    print(counter)<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>In the bad example, when the counter is equal to 3 the continue line runs. So it skips to the start of the loop, but the counter is still 3. It&#8217;s stuck in an infinite loop.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Design<\/h2>\n\n\n\n<p>So how do represent the above code. Pretty much the same as a for loop. Just change the description in the decision box. But notice the lines where we add a number to our counter. That is neither a decision, initialisation or an input\/output. We need a new shape for the flowchart.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"323\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13162457\/fc_process-1.png\" alt=\"Flowchart process symbol.\" class=\"wp-image-353\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13162457\/fc_process-1.png 900w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13162457\/fc_process-1-300x108.png 300w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13162457\/fc_process-1-768x276.png 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/figure>\n\n\n\n<p>The process box can be used to represent one or more lines covering anything from simple arithmetic to function calls. If another shape doesn&#8217;t cover what you need to do, then use a process box.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"713\" height=\"783\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13163500\/fc4.png\" alt=\"Flowchart with a while loop\" class=\"wp-image-354\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13163500\/fc4.png 713w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13163500\/fc4-273x300.png 273w\" sizes=\"auto, (max-width: 713px) 100vw, 713px\" \/><\/figure><\/div>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-2\/challenge-4\/\" data-type=\"page\" data-id=\"348\">Next: Challenge 4<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A while loop continuously loops until a conditional check is no longer True. The code: Gives an output: In this example the loop checks if the counter variable is less than 5. So long as that check returns a boolean True then the code inside the loop will run. Be careful with while loops to&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-2\/while-loops\/\">Continue reading <span class=\"screen-reader-text\">While loops<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":16,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-343","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/users\/5710"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/comments?post=343"}],"version-history":[{"count":5,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/343\/revisions"}],"predecessor-version":[{"id":1351,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/343\/revisions\/1351"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/16"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/media?parent=343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}