1 00:00:00,320 --> 00:00:04,870 This pattern of using an integer that increases each time the loop runs, is so 2 00:00:04,870 --> 00:00:08,700 common that there's a special kind of loop just for that. 3 00:00:08,700 --> 00:00:10,860 It's called a for loop. 4 00:00:10,860 --> 00:00:15,713 I'll write what that looks like lower existing loops so we can compare the two. 5 00:00:15,713 --> 00:00:22,990 This is doing 6 00:00:22,990 --> 00:00:32,250 exactly the same 7 00:00:32,250 --> 00:00:37,090 thing the while loop, up here does. 8 00:00:38,260 --> 00:00:41,770 We can break our while loop up here into for parts. 9 00:00:41,770 --> 00:00:46,600 First, we declare index as an integer and initialize it to 0. 10 00:00:46,600 --> 00:00:51,780 Second, we check to see if we’ve looped through all the indexes of the array. 11 00:00:51,780 --> 00:00:57,900 Third, we do something with each of the array items Fourth, we increment index so 12 00:00:57,900 --> 00:01:01,390 that the next time through the loop we're getting the next item in the array. 13 00:01:03,160 --> 00:01:09,140 For loops also have the same for parts only there written more concisely. 14 00:01:09,140 --> 00:01:12,390 Here's where we declare index as an integer and initialize it to zero. 15 00:01:13,450 --> 00:01:18,000 Here's where we check to see if we've loop through all the indexes of the array. 16 00:01:18,000 --> 00:01:21,730 The body of the loop is used for doing something with the array item. 17 00:01:22,980 --> 00:01:25,160 And here's where we increment index so 18 00:01:25,160 --> 00:01:29,040 that the next time through the loop we're getting the next item in the array. 19 00:01:30,220 --> 00:01:33,470 Just think of for loops as condensed while loops. 20 00:01:34,710 --> 00:01:37,980 Now that we have this for loop we don't need this while loop anymore. 21 00:01:39,040 --> 00:01:42,760 For loops aren't only used for looping through a race. 22 00:01:42,760 --> 00:01:47,150 They're also really handy when we want to count from one number to another. 23 00:01:47,150 --> 00:01:52,270 We can set the starting number here and it can be any number we want. 24 00:01:52,270 --> 00:01:55,110 We decide where to stop counting here. 25 00:01:55,110 --> 00:02:00,530 This can be any condition that eventually stops the loop by evaluating to false. 26 00:02:00,530 --> 00:02:05,000 Each time the loop runs, index will count up by one. 27 00:02:05,000 --> 00:02:08,320 In fact, this variable is often called a loop counter. 28 00:02:09,440 --> 00:02:13,200 We could also count by more than one by using plus equals like this. 29 00:02:14,560 --> 00:02:15,980 This will count up by two. 30 00:02:17,560 --> 00:02:21,190 We want to count by one so I'll change this back to index plus plus. 31 00:02:22,450 --> 00:02:26,420 Because this variable index is being declared right here, 32 00:02:26,420 --> 00:02:29,180 its scope is limited to inside of the loop. 33 00:02:30,490 --> 00:02:34,020 That means that it can only be used inside the 4 loop. 34 00:02:34,020 --> 00:02:38,930 If we want to access the loop counter variable after the 4 loop has ended, 35 00:02:38,930 --> 00:02:42,495 we'd have to move the declaration up here above the 4 loop somewhere. 36 00:02:42,495 --> 00:02:51,090 [PAUSE] It may look strange, but this is perfectly valid code. 37 00:02:52,900 --> 00:02:57,940 When reading code, you'll notice that most for loops use the single letter I 38 00:02:57,940 --> 00:03:00,790 as the name of the loop counter variable. 39 00:03:00,790 --> 00:03:05,580 This tradition goes all the way back to the first programming languages created. 40 00:03:05,580 --> 00:03:08,200 You can name it whatever makes sense for your code though. 41 00:03:08,200 --> 00:03:11,787 Just to maintain tradition I'll shorten index to I for us.