1 00:00:00,330 --> 00:00:02,680 We've done do, while, and we've done while, 2 00:00:02,680 --> 00:00:04,640 we got the pre-check, the post check. 3 00:00:04,640 --> 00:00:07,030 This one is the for loop. 4 00:00:07,030 --> 00:00:10,760 When you know how many times you want to loop, this is a great example for 5 00:00:10,760 --> 00:00:12,410 when you want to use fors. 6 00:00:12,410 --> 00:00:14,970 They're often called the C style for loop. 7 00:00:14,970 --> 00:00:18,910 And it looks like a whole bunch of code jammed into a little statement and 8 00:00:18,910 --> 00:00:20,940 it's a little overwhelming at first. 9 00:00:20,940 --> 00:00:23,990 Most C like languages implement them, so you'll see it in Java Script, 10 00:00:23,990 --> 00:00:25,810 you'll see them all over the place. 11 00:00:25,810 --> 00:00:28,024 And it is an old school way of looping over an array, 12 00:00:28,024 --> 00:00:31,647 which we'll talk about arrays here in a little bit, if you haven't heard of them. 13 00:00:31,647 --> 00:00:35,500 But it's sort of an old school and it's a little bit frowned upon, so 14 00:00:35,500 --> 00:00:37,210 let's just jump into it. 15 00:00:37,210 --> 00:00:40,622 Let's do 99 bottles of, you know the old song, 16 00:00:40,622 --> 00:00:44,042 99 Bottles of Milk on the Wall, right, right? 17 00:00:44,042 --> 00:00:47,731 Yeah, so, it's milk. 18 00:00:47,731 --> 00:00:51,060 So, you go 99 Bottles of Milk on the Wall, 99 Bottles of Milk, you take one down and 19 00:00:51,060 --> 00:00:53,860 pass it around, 98 Bottles of Milk on the Wall. 20 00:00:53,860 --> 00:00:54,360 So, that sort of thing. 21 00:00:54,360 --> 00:00:59,673 So, the for loop example, what we would do here, is we'd start at 99. 22 00:00:59,673 --> 00:01:02,449 We're gonna start our loop at 99, and as long as there's some, 23 00:01:02,449 --> 00:01:05,287 we're gonna take one off the wall, and we're gonna sing about it. 24 00:01:05,287 --> 00:01:07,741 And we're gonna drink our milk, and we're gonna throw it away, and 25 00:01:07,741 --> 00:01:08,893 then we're gonna go grab 98. 26 00:01:08,893 --> 00:01:09,982 So that's a for loop. 27 00:01:09,982 --> 00:01:14,440 This is the expression that's happening and this is checking. 28 00:01:14,440 --> 00:01:16,690 And these three little 29 00:01:18,970 --> 00:01:23,290 new lines here have words and those words also look a little bit intimidating. 30 00:01:23,290 --> 00:01:24,390 Let's talk about what they are. 31 00:01:24,390 --> 00:01:26,330 So, we have initialization. 32 00:01:26,330 --> 00:01:29,360 So that runs the very first time this enters. 33 00:01:29,360 --> 00:01:32,810 So the very first time for loop is encountered, initialization runs. 34 00:01:32,810 --> 00:01:36,440 That's where you want to set up different variables that you're tracking stuff with. 35 00:01:36,440 --> 00:01:39,760 The second, Java calls this thing a termination. 36 00:01:39,760 --> 00:01:43,110 What that is, if this statement returns false it will terminate. 37 00:01:43,110 --> 00:01:47,050 This will run before the for loop goes and then increment will happen afterwards. 38 00:01:47,050 --> 00:01:51,520 So, this statement runs and checks, and if it returns a true or false. 39 00:01:51,520 --> 00:01:53,160 And if it returns true, it continues looping. 40 00:01:53,160 --> 00:01:54,930 If it returns false, it stops. 41 00:01:54,930 --> 00:01:55,690 And the increment, 42 00:01:55,690 --> 00:01:59,410 what happens is, every time the loop goes through, this statement will run. 43 00:01:59,410 --> 00:02:01,870 It doesn't have to increment like we saw with the pluses. 44 00:02:01,870 --> 00:02:05,930 I'm using these words because this is how it is in the Java documentation, but 45 00:02:05,930 --> 00:02:07,110 I find them a little bit confusing. 46 00:02:07,110 --> 00:02:10,290 What we're gonna do in this next statement with our for loop, is we're gonna do 47 00:02:10,290 --> 00:02:13,480 99 Bottles of Milk on the Wall, [LAUGH] I almost said the other thing. 48 00:02:13,480 --> 00:02:15,060 And it's going to count down. 49 00:02:16,100 --> 00:02:16,970 So let's do it. 50 00:02:16,970 --> 00:02:21,346 You can go refill your milks now, if you'd like to. 51 00:02:21,346 --> 00:02:28,279 So milk, okay, so the for loop looks like this. 52 00:02:28,279 --> 00:02:30,477 So this is where we're gonna initialize. 53 00:02:30,477 --> 00:02:33,704 Here we're gonna initialize ta variable named i and 54 00:02:33,704 --> 00:02:36,060 i is going to be less than 99, okay? 55 00:02:37,690 --> 00:02:40,717 So as long as this is true, we want to keep going, okay? 56 00:02:40,717 --> 00:02:41,940 So this is the termination. 57 00:02:43,330 --> 00:02:46,554 Then, each time we're gonna decrement, okay? 58 00:02:46,554 --> 00:02:49,558 [LAUGH] In the increment spot we're gonna decrement. 59 00:02:49,558 --> 00:02:51,510 So that's what minus, minus does, right? 60 00:02:51,510 --> 00:02:54,340 It says, i = i- 1. 61 00:02:55,770 --> 00:02:57,440 Decrementing by 1. 62 00:02:57,440 --> 00:02:59,800 Shorthand for doing that, okay? 63 00:02:59,800 --> 00:03:05,749 And then we'll say, %d, 64 00:03:05,749 --> 00:03:13,518 bottles of milk on the wall.... 65 00:03:13,518 --> 00:03:16,020 And a %n, or that will get messy. 66 00:03:16,020 --> 00:03:17,190 With the %, not a /. 67 00:03:19,470 --> 00:03:22,124 Okay, and good, that's closed out. 68 00:03:25,902 --> 00:03:28,462 And we will compile Milk. 69 00:03:30,660 --> 00:03:34,610 >> [INAUDIBLE] >> Oh, but you guys caught it, thanks! 70 00:03:34,610 --> 00:03:37,965 >> [APPLAUSE] >> Thank you, that was awesome. 71 00:03:37,965 --> 00:03:41,891 Thanks, we're like peer programming with 30, 40 people [LAUGH]. 72 00:03:41,891 --> 00:03:44,478 >> [LAUGH] >> [INAUDIBLE] 73 00:03:44,478 --> 00:03:48,736 >> [LAUGH] That's true [LAUGH]. 74 00:03:48,736 --> 00:03:49,821 Okay, so- >> So 75 00:03:49,821 --> 00:03:53,520 why are you having i start at 0 [INAUDIBLE]. 76 00:03:53,520 --> 00:03:55,280 >> You're right! 77 00:03:55,280 --> 00:03:57,909 Ken wow, [INAUDIBLE] double fail, wow! 78 00:03:57,909 --> 00:04:00,451 >> [APPLAUSE] >> Thanks. 79 00:04:00,451 --> 00:04:01,784 Thank you. 80 00:04:01,784 --> 00:04:03,710 Would've been messy. 81 00:04:03,710 --> 00:04:08,191 All right here we go [LAUGH], I think it's gonna work we've had a human compiler run- 82 00:04:08,191 --> 00:04:09,194 >> [LAUGH] 83 00:04:09,194 --> 00:04:14,720 >> [INAUDIBLE] 84 00:04:14,720 --> 00:04:15,853 >> Guys, what am I doing? 85 00:04:15,853 --> 00:04:18,521 >> [LAUGH] >> You don't need me up here. 86 00:04:18,521 --> 00:04:21,935 All right, so, [LAUGH] I was gonna start at 99. 87 00:04:21,935 --> 00:04:25,769 >> [LAUGH] >> While i is greater than 0, 88 00:04:25,769 --> 00:04:28,869 we're going to decrement it and keep going. 89 00:04:28,869 --> 00:04:30,598 Let's see if that works, I bet it will [LAUGH]. 90 00:04:30,598 --> 00:04:33,416 >> [LAUGH] >> Can we edit that? 91 00:04:33,416 --> 00:04:34,773 Can we edit that that part? 92 00:04:34,773 --> 00:04:36,502 Hey, there it is! 93 00:04:36,502 --> 00:04:40,220 >> [APPLAUSE] >> All right, wow. 94 00:04:40,220 --> 00:04:44,630 Okay, so we initialized and 95 00:04:44,630 --> 00:04:49,520 when this was false we terminated. 96 00:04:49,520 --> 00:04:51,848 And then each time we decremented through.