WEBVTT 1 00:00:02.228 --> 00:00:05.225 We've already looked at the While Loop 2 00:00:05.229 --> 00:00:08.110 but what we're going to look at now is a different form of 3 00:00:08.111 --> 00:00:10.229 looping structure called the For Loop. 4 00:00:11.222 --> 00:00:13.222 Now a distinct feature about the for loop 5 00:00:14.005 --> 00:00:16.117 is that it allows you to specify and control 6 00:00:16.118 --> 00:00:18.220 exactly how often a loop should run 7 00:00:18.228 --> 00:00:21.003 depending on what kind of 8 00:00:21.116 --> 00:00:23.119 sensors and inputs you're actually using 9 00:00:23.221 --> 00:00:25.117 to make this work you have to give it three 10 00:00:25.118 --> 00:00:27.008 particular forms of information. 11 00:00:27.116 --> 00:00:30.002 The first thing is you have to give it 12 00:00:30.005 --> 00:00:32.224 some kind of counter or variable 13 00:00:32.227 --> 00:00:36.002 that is set up at the start, thats going to keep track of 14 00:00:36.006 --> 00:00:38.004 how often the loop has run. 15 00:00:38.225 --> 00:00:41.009 In any kind of loop you need some kind of test 16 00:00:41.111 --> 00:00:43.002 thats going to allow you to decide 17 00:00:43.003 --> 00:00:45.008 whether or not the loop should end 18 00:00:46.002 --> 00:00:47.226 and the third thing we have to do, again 19 00:00:47.227 --> 00:00:50.000 as you have already found with the while loop 20 00:00:50.005 --> 00:00:53.221 is you've to have some mechanism that changes the value 21 00:00:53.222 --> 00:00:55.007 of the counter that you're using 22 00:00:55.008 --> 00:00:57.225 otherwise you'll never exit the loop 23 00:00:58.115 --> 00:01:00.110 lets look at how that works. 24 00:01:02.220 --> 00:01:04.008 To make this test work 25 00:01:04.117 --> 00:01:06.113 you can use logic expressions 26 00:01:06.115 --> 00:01:08.004 again these have been looked at before, 27 00:01:08.001 --> 00:01:10.000 lets just reiterate then for a moment 28 00:01:10.118 --> 00:01:13.003 so we've got a greater than statement 29 00:01:13.221 --> 00:01:16.111 which test whether one side is greater than the other 30 00:01:16.112 --> 00:01:19.007 and its partner less than 31 00:01:19.228 --> 00:01:21.112 we can also test if something is 32 00:01:21.113 --> 00:01:23.118 greater than or equal a particular value 33 00:01:23.006 --> 00:01:26.000 or less than or equal to a particular value 34 00:01:26.114 --> 00:01:28.227 then we've got the particular test 35 00:01:29.009 --> 00:01:31.003 of being equals and again 36 00:01:31.009 --> 00:01:34.002 the actual logical test for if one side of the test 37 00:01:34.003 --> 00:01:37.004 is equal to the other is to use the double equal sign 38 00:01:37.006 --> 00:01:40.000 not the single one, equally important 39 00:01:40.110 --> 00:01:42.118 is the not equals sign 40 00:01:43.115 --> 00:01:45.223 these are the different ways that we can check 41 00:01:45.228 --> 00:01:47.004 whether one side 42 00:01:47.221 --> 00:01:50.111 matches or doesn't match another and then whether 43 00:01:50.112 --> 00:01:52.005 the loop is true to continue. 44 00:01:53.112 --> 00:01:56.008 Here's the general layout of the for statement 45 00:01:57.009 --> 00:01:58.117 as you'd expect with any loop 46 00:01:58.118 --> 00:02:01.001 its got a marker at the start the brace 47 00:02:01.111 --> 00:02:02.229 to see where the code that will be executed in 48 00:02:02.229 --> 00:02:04.226 the loop is and a marker at the end 49 00:02:05.001 --> 00:02:06.223 to see where the loop should end 50 00:02:07.116 --> 00:02:09.111 the key differences that you have 51 00:02:09.114 --> 00:02:11.220 are that all the control 52 00:02:12.002 --> 00:02:15.000 is done inside this bracket after the for statement 53 00:02:15.009 --> 00:02:18.004 we specify what's the starting condition 54 00:02:18.221 --> 00:02:21.004 what check are we going to perform 55 00:02:22.002 --> 00:02:24.226 and then how were going to change 56 00:02:25.009 --> 00:02:27.228 the starting value everytime the loop runs 57 00:02:28.225 --> 00:02:31.114 also its worth noting there's no semi-colon 58 00:02:31.117 --> 00:02:33.002 at the end of the statement 59 00:02:33.007 --> 00:02:35.225 because its not actually the end of a statement 60 00:02:36.115 --> 00:02:39.006 the statement finishes down at the bottom of these braces 61 00:02:39.009 --> 00:02:42.003 which act in the same way a semi-colon would. 62 00:02:44.006 --> 00:02:45.225 Here's a small example 63 00:02:47.003 --> 00:02:49.008 we've already looked at this for the while statement 64 00:02:49.226 --> 00:02:52.007 so what we've got here, is we have to setup 65 00:02:52.111 --> 00:02:55.117 a variable first of all, which we're going to use 66 00:02:55.229 --> 00:02:57.220 and then just as the previous slide shows 67 00:02:57.221 --> 00:02:59.115 we setup a starting condition 68 00:03:00.003 --> 00:03:03.009 that sets it to be zero at the start, we setup 69 00:03:03.111 --> 00:03:06.116 the condition to keep it in the loop so long as the value of i 70 00:03:06.221 --> 00:03:10.001 is less than five this loop will continue to run 71 00:03:10.117 --> 00:03:13.220 and then finally we have how i changes 72 00:03:13.221 --> 00:03:16.008 in this case we're using the standard increment command 73 00:03:16.112 --> 00:03:19.005 thats built in to the programming language 74 00:03:19.222 --> 00:03:21.220 and then we've got the actual operation 75 00:03:21.221 --> 00:03:23.116 between these two braces 76 00:03:23.227 --> 00:03:25.112 and as you can see this particular case 77 00:03:25.113 --> 00:03:27.008 is going to drive forward for half a second 78 00:03:27.115 --> 00:03:29.009 and do it five times 79 00:03:30.111 --> 00:03:32.222 so this loop will run five times 80 00:03:32.227 --> 00:03:34.225 for when the value of i is zero 81 00:03:35.113 --> 00:03:37.222 then when i is one, when i is two 82 00:03:37.225 --> 00:03:39.228 when i is three, when i is four 83 00:03:40.007 --> 00:03:42.001 and then once it goes through the loop again 84 00:03:42.002 --> 00:03:43.226 and increments to i being five 85 00:03:44.000 --> 00:03:47.009 and exits the loop and continues on executing the code 86 00:03:47.228 --> 00:03:49.118 beyond the for loop 87 00:03:52.005 --> 00:03:54.229 and this just highlights the roles of these elements