Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Difficulties With Loops6:31 with Craig Dennis
Problems may occur
Sometimes you're faced with the decision, right, between while and a for. 0:02 The weird thing is, that you can basically reword it the same way. 0:08 You could write a for loop and a while loop because all the for 0:11 loop is doing is it's a start here, check this thing and 0:15 then increment, and you can write a while loop and just move those words around. 0:18 So, let's do an example of that, so that I can show you that there's synonyms for it. 0:23 It's difficult to know what to do, and so 0:28 the choices have some basic rules and you always wanna lean towards legibility. 0:34 So, let's do that. 0:39 And then the other difficulty, which we touched on already a little bit, 0:39 is the infinite loop, and we will cause one of those to happen. 0:44 So in Java Basics, I failed big time. 0:47 >> [APPLAUSE] >> I assumed that everybody knew what 0:52 a dunk tank was, and then they didn't. 0:56 Right, so internationally, people did not know what a dunk tank was. 0:58 And they were like, I didn't know what a dunk tank was. 1:00 I had to look it up and oh my gosh, I wish I had one of those. 1:02 I wish I saw one of those before. 1:05 So dunk tank, when you are sitting on a board and 1:06 then you fall in the water, that sort of thing. 1:09 First of all, I felt really bad about it because why would I give you this fun 1:11 while loop that you don't understand that made you learn about dunk tanks. 1:14 But secondly I want that fun in the world. 1:18 I want that out there. So what we talked about, 1:22 what we did was we said, hey, let's get a dunk tank and you can do that for 1:24 200 bucks, you get a dunk tank. 1:28 [LAUGH] So, Jim, you guys might know Jim from Counsel Foundations, 1:30 he's one of the original teachers here. 1:33 He was given three tries to try to dunk me. 1:36 And, while I was sitting there, and he had tries remaining, he throws the ball. 1:39 Okay, so while I'm there, and he has three tries, he keeps going. 1:43 So, it's a complex while loop. 1:48 A little bit more complex than our one batch that we're doing. 1:50 So, what I'm going to do, I put that in a course. 1:52 And you'll find it as you go through the Java track, but 1:55 I'm gonna show you some behind the scenes footage. 1:57 So this is Jim. 2:00 Don't let the beard throw you. 2:01 He had a shorter beard during it. 2:03 And this is a dunk tank. 2:04 Look at it. 2:06 That's the dunk tank, and you have to hit this thing. 2:06 Notice I'm dry. 2:09 This is take one and watch what he does. 2:10 [SOUND] Oh, come on, you throw better exceptions than that. 2:13 [SOUND] Bummer, try again. 2:16 [SOUND] >> [LAUGH] 2:19 >> Watch Jim, watch Jim. 2:22 Check this out. 2:23 So good, so good. 2:25 >> [LAUGH] >> [LAUGH] So, 2:28 here's the PSEUDO-DUNK. 2:32 I'm I Dunk? 2:37 Nope. 2:38 For each new attempt starting at 0 tries, 2:39 isDunked is nope and the tries under 3 increase the tries. 2:42 So, we're gonna go through that for loop again and then each time in there, 2:47 we're gonna catch and see if I got dunked. 2:50 Okay, so we're gonna use that same random again. 2:52 Okay, so let's take a look first. 2:54 So this is a complex, and we kind of know how many throws, right? 2:55 We know he's gonna do three, but we're not sure he's gonna hit me, right? 2:59 So let's walk through the while version real quick. 3:01 All right, so the while version of the dunk tank. 3:05 So I wrote this code out already so we don't have to type it. 3:10 So, we're gonna set some variables. 3:16 We're gonna initialize. 3:17 This right here, we're gonna set that, it's false, and 3:19 we're gonna start tries at 0. 3:22 While I'm not dunked and it's less than or equal to three, we're going to print out 3:24 the number of tries, and then we're gonna get, I used skill because it wasn't luck. 3:30 It was very clear that Jim is skilled, he did not have luck there. 3:34 And then we're gonna loop on the tries. 3:37 So we have a complex loop here, and I'm gonna flip this, 3:39 because this is not edited. 3:42 So, here we go, ballsThrown = 0, and 3:44 when ballsThrown is less than or equal to 3. 3:48 We're gonna go ahead and say try number. 3:52 We're gonna do some math in there, and we're gonna increment balls thrown. 3:55 Anybody catch a compiler error? 4:00 Those for having that. 4:03 So, if I am dunked, after the loop, if I am dunked, 4:05 he's gonna air punch, which he did do, or we're gonna say boo, okay. 4:09 Come on, Jim, get it together. 4:13 But he did it, he hit me, first try. 4:15 All right, so, we will do 4:18 First one. 4:39 So he does an air punch. 4:40 So, that's pretty clean. 4:42 I feel like somebody could take a look at that and 4:45 kinda understand what's going on, right? 4:47 It's a little advanced, but so is the concept. 4:49 It's got a multiple expression and a negation [LAUGH] and an and. 4:51 But let's take a look at what that looks like when we look at the for 4:55 version of that. 4:59 Again, I'm gonna fix this to be ballsThrown. 5:04 Sorry about that. 5:07 I'm gonna do a fail way as soon as I take my hand off the keyboard. 5:12 Then each time we're gonna do a ballsThrown, and do ballsThrown here. 5:14 Cool, so that for loop, if you came in here and you looked at that, that is 5:24 a long for loop and if my screen was over a little bit more, it might flip over. 5:27 I think it might not be as clear, okay? 5:32 So, you always wanna go from the more legible version, 5:34 although this works, right? 5:38 This will do the same, I think, we didn't try to run it but conceptually, 5:39 this is gonna work. 5:42 This is gonna do the same thing as the while loop. 5:43 So it's a synonym for 5:45 the same code, [INAUDIBLE] though I promised an infinite loop. 5:46 So, I'm gonna flip back to our while loop and let's say that we're 5:51 just testing some stuff out we forgot, and we come without these two lines. 5:56 We're not gonna increment and I'm not gonna get dunks. 6:00 It's always gonna be false. 6:02 Let's bring down the servers. 6:05 [LAUGH] So there we go, it's going, it's going, it's going. 6:07 You can't really tell. 6:11 It's still running, it's running, it's always gonna run, it's always gonna run. 6:12 Ctrl+C, so, Ctrl+C helps stop that, if that happens. 6:14 And that does happen, right? 6:19 When that does happen to you, you'll see an error from us saying that, hey, 6:20 it timed out. 6:24 But, it timed out because your infinite loop was still running. 6:25 So there are difficulties with loops. 6:28
You need to sign up for Treehouse in order to download course files.Sign up