Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Do While Loops4:21 with Craig Dennis
Do While loops are a post-check looping construct, they will run at least once.
All right, so. 0:00 While loop. 0:01 We checked before hand, right? 0:02 I wouldn't of had to keep on asking that, 0:03 wouldn't have keep on doing that loop if I didn't ask for it. 0:05 Now let's do a do while loop, okay. 0:07 So a do while loop. 0:09 It's the same concept, right. 0:10 We don't know how many times it's going to happen but 0:11 we do know that we want it to happen one time. 0:12 Right, so it does what's called a post check. 0:14 So this is useful when you want to make sure that one time it happens. 0:17 So. The example that I have for this, 0:21 we're totally shifting examples here. 0:23 The example that I have for this one, this pseudo code is Mini-Golf, right? 0:24 So, you can't get a hole in zero. 0:28 You have to get a hole in one, right? 0:30 That's the best you can do. 0:31 So you gotta putt once, okay? 0:32 So we're gonna say do. 0:34 Where, is the ball in the hole is a Boolean over here. 0:37 I should've said, is the ball in the hole? 0:39 See, I'm bad at pseduo-code. 0:41 And we do puttPutt. 0:42 We're gonna call and see if we hit that ball in the hole and 0:43 until here, that's actually, while not. 0:47 Right? In English, too, if you wanted to say, 0:50 until something happens, you're really saying, wow, this isn't happening. 0:52 Right? So, until is a verb, or a whatever 0:55 type of word that is in other programming languages is a keyword in other type of 1:00 programming languages, and it really means while not, but Java does not have that. 1:04 So we're gonna do, well, let's code it. 1:08 So cross your fingers for me, here we go. 1:11 Some more live code. 1:12 All right. 1:14 So, mini golf. 1:14 All right, so I imported something that we probably have not talked on yet. 1:17 It's handy for testing and example things like this. 1:22 It's randomness, right. 1:24 It's gonna allow us the ability to give a 50/50 thing, right. 1:26 So I don't know about you but my mini golf skills is not 50/50, 1:29 I usually have about 99, you know I take a very long time to go through. 1:32 So what we'll do is we want one time, we want to do a putt putt. 1:36 And let's keep track of it so that we can see how bad we did. 1:40 Or how bad the computer did, actually, right? 1:44 So we're gonna do numberOfPutts. 1:46 Such a weird word, you know, how like when you're typing words and you're like, 1:48 is that really how you spell putt? 1:51 I don't know. 1:52 And putts, too, like. 1:53 All right. 1:54 So, boolean ballInHole is gonna be false. 1:55 We're gonna start out with it false, right? 1:59 So the do while loop, we're gonna say do while, and 2:01 this is where we're doing the end tells, so not the ballInHole, right? 2:05 So it's a little strange, right? 2:10 So you're thinking, while the ball isn't in the hole, 2:12 while that's not true, keep on looping, okay? 2:14 Let's just print out something so we know that we're put that's not great. 2:17 Alright. 2:24 So let's use that random generator. 2:25 And I called that thing luck, 2:28 I called the variable luck, which is what you need when you're doing this stuff. 2:30 And it has random has the opportunity to return numbers or 2:34 other random things, one of the things that can return is a Boolean. 2:39 And remember, Booleans are true or false. 2:41 We're setting ballInHole to the Boolean, so therefore when it comes to check, 2:44 we'll see, we want to increment. 2:47 I don't know if everybody knows what this incrementing is or 2:49 if we remember what that is. 2:52 What it does is it basically is shorthand for saying, I could say number of putts 2:54 equals what number of putts was plus one. 2:57 So it just takes whatever's in there and adds one to it. 3:03 It's a shorthand. 3:06 Let's keep it in there. 3:07 We're gonna talk about that a little bit later, too. 3:09 All right. 3:13 For sure one time it goes through. 3:15 It will at least be one, right? 3:17 Cuz they tried one time. 3:18 And if the ball went in the hole, it would be over, and good job, 3:19 we did a hole in one. 3:22 But we'll say, you got it in %d, because it's a number. 3:23 And we're gonna pass n the number of putts. 3:31 What do you say? 3:34 I remember that. 3:38 Let's do it. 3:40 I don't know where my history went. 3:42 Let's see. So we'll call this Minigolf. 3:46 We're gonna compile Minigolf, and then we are going to run Minigolf. 3:48 Don't put the Java extension there. 3:53 One, hole in one, we did it randomly. 3:58 We randomly did a hole in one! 4:00 >> [APPLAUSE] >> Yeah! 4:02 Still, a hole in one. 4:03 Come on, oh there it is. 4:05 Three putts. This should. 4:06 Wow, it's doing pretty good. 4:10 >> Looks good. 4:11 >> There's two putts. 4:12 Yeah. >> [LAUGH] 4:13 >> Yeah. 4:14 Let's go golfing and 4:14 let's put some money on it. Cool. 4:15 >> [LAUGH] 4:17
You need to sign up for Treehouse in order to download course files.Sign up