{"id":318,"date":"2023-02-13T12:22:25","date_gmt":"2023-02-13T12:22:25","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=318"},"modified":"2024-06-14T08:59:33","modified_gmt":"2024-06-14T07:59:33","slug":"for-loops","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-2\/for-loops\/","title":{"rendered":"For loops"},"content":{"rendered":"\n<p>A common requirement in programming is to be able to repeat an action a given number of times, or once for each entry in a list.<\/p>\n\n\n\n<p>A For loop allows you to do this. The name basically means &#8220;For each entry, do this code&#8221;.<\/p>\n\n\n\n<p>A for loop goes element by element through a list. The current element of the list is stored in a variable defined in the for loop. The format is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for <strong>x<\/strong> in <strong>y<\/strong>:<\/code><\/pre>\n\n\n\n<p>Where&nbsp;<strong>x<\/strong>&nbsp;is the new variable that will hold the contents of the current list element. And&nbsp;<strong>y<\/strong>&nbsp;is the list that you are looping through.<\/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>print(\"Program starts\")\n\nlist_of_animals = &#091; \"cat\", \"dog\", \"rabbit\"]\n\nfor animal in list_of_animals:\n    print(animal)\n\nprint(\"Program ends\")<\/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>Program starts\ncat\ndog\nrabbit\nProgram ends<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>In each time through the loop the variable animal is set to the next element in the list_of_animals array. So on the first iteration the value of animal is \u201ccat\u201d, the second time it is overwritten with \u201cdog\u201d and so on.<\/p>\n\n\n\n<p>Just as you saw with the conditional statements the for line ends in a colon and all the code to be ran each time through the loop is indented.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Range<\/h2>\n\n\n\n<p>The range function generates a list of integers. This is particularly useful in for loops if you want to carry out a block of code a set number of times.<\/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>for counter in range(5):\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<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>All that range does it return a list of numbers that start at 0 and counter up to (but never reach) the number provided to the function in the brackets.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<p>So far we have looped through a list (technically range is its own iterator of type range), but a dictionary behaves differently because it is made up of key:value pairs. <\/p>\n\n\n\n<p><\/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>If you just did:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exam_grades = {'JDoe': 85, 'MSmith': 67, 'BMacleod': 81}\r\nfor entry in exam_grades:\r\n  print(entry)<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>You would get:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>JDoe\nMSmith\nBMacleod<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>Only the keys were printed. <\/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>To get the values you need to use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exam_grades = {'JDoe': 85, 'MSmith': 67, 'BMacleod': 81}\nfor key,value in exam_grades.items():\n  print(key, value)<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>You would get:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>JDoe 85\nMSmith 67\nBMacleod 81<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Break<\/h2>\n\n\n\n<p>A break statement allows you to stop the current loop before the natural end. When the break line is encountered, the program continues from the first line after the end of the 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<p>The code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>value_to_check = 3\n\nfor counter in range(5):\n    print(counter)\n    if value_to_check == counter:\n        break\n<\/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<p>Here we can see the counter counting up from 0. Each time through the loop it will have 1 added to it. When it equals 3 though the if statement returns True and a break line is run. This stops the loop before the natural end.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Continue<\/h2>\n\n\n\n<p>A Continue Statement allows you to skip the remaining lines of code in the current time through a loop section. When the continue line is encountered, the program jumps back to the start of the loop section. <\/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>value_to_check = 3\nfor counter in range(5):\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<p>Gives an output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1\n2\n4<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>You can see that 3 has been skipped. When the if statement condition returned True the continue line was run, this then exited that time around the loop which skipped the print(counter) line.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Design<\/h2>\n\n\n\n<p>So how do we represent a for loop in a flow chart? There are multiple ways to do it. You could go into a lot of detail and show the initial value of the loop, how it changes each time through and what the decision is going to be checking. Or simply put the for loop description into the decision box directly. It will be clear enough to yourself making the program or any tutors how many times that loop will run.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"446\" height=\"324\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13155147\/fc2-1.png\" alt=\"Example flow chart with a for loop.\" class=\"wp-image-334\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13155147\/fc2-1.png 446w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13155147\/fc2-1-300x218.png 300w\" sizes=\"auto, (max-width: 446px) 100vw, 446px\" \/><\/figure>\n\n\n\n<p>Now your flow chart has a visual loop so that it is clear there is a looped bit of code in your program. Any lines of code in your for loop happen between the decision and where it joins back to the main flow just before the decision.<\/p>\n\n\n\n<p>What about when we are using an existing list rather than one made by range? Time for a new flowchart shape.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"980\" height=\"323\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13155339\/Py39.png\" alt=\"Initialisation flow chart shape\" class=\"wp-image-337\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13155339\/Py39.png 980w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13155339\/Py39-300x99.png 300w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13155339\/Py39-768x253.png 768w\" sizes=\"auto, (max-width: 980px) 100vw, 980px\" \/><\/figure>\n\n\n\n<p>You can use this to initialise or declare variables or other data structures.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"476\" height=\"490\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13155803\/fc3.png\" alt=\"Flowchart with a for loop and initialised variable\" class=\"wp-image-338\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13155803\/fc3.png 476w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/02\/13155803\/fc3-291x300.png 291w\" sizes=\"auto, (max-width: 476px) 100vw, 476px\" \/><\/figure>\n\n\n\n<p>Just make sure the loop line goes back to just before the decision so that you aren&#8217;t re-initialising the variable.<\/p>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-2\/while-loops\/\" data-type=\"page\" data-id=\"343\">Next: While loops<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A common requirement in programming is to be able to repeat an action a given number of times, or once for each entry in a list. A For loop allows you to do this. The name basically means &#8220;For each entry, do this code&#8221;. A for loop goes element by element through a list. The&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-2\/for-loops\/\">Continue reading <span class=\"screen-reader-text\">For loops<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":16,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-318","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/318","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=318"}],"version-history":[{"count":13,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/318\/revisions"}],"predecessor-version":[{"id":1383,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/318\/revisions\/1383"}],"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=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}