While Loops8:15 with Craig Dennis
While loops are a pre-check loop construct that will run every time the expression is evaluated to true.
[MUSIC] 0:00 We're gonna talk about loops today and I tried to be funny and made it called, 0:04 Feeling Loopy. 0:08 Let's talk about some ground rules too for this. 0:10 This is a little bit different than what we're used to. 0:12 We've probably been working, you can kind of kind of stop and pause and 0:15 that sort of thing. 0:18 We can't do that here, so I, also, as Dave was talking about there, 0:19 the amazing video editors when we make mistakes. 0:25 You don't see them unless we include them on purpose. 0:29 So I might make a mistake while I'm up here, but what I want to do 0:31 is I want to talk about failure a little bit just before we start here. 0:35 I want to encourage you to view failure as a good thing. 0:40 Okay? Because, and follow me, 0:44 hold on a second, I know you're like, no, it doesn't feel good when I fail. 0:45 You were trying, you were trying something. 0:48 Right? 0:51 And trying is learning. 0:51 Okay? So, you're gonna get there. 0:53 You get there through failing. 0:55 So, in this environment, if anybody fails, if you go like this, 0:56 it's the fail wave, and you can clap. 1:00 I want everybody to clap crazy, if I fail, I'm gonna raise my hands, and 1:02 I'm gonna expect you to clap for me. 1:05 So, good? 1:07 Here's an example. 1:08 When I film Java basics, 1:10 we always try to cut, we try to make these videos 10 minutes long, right? 1:11 Because that's what you're attention span is. 1:14 We try to keep everything at about 10 minutes, 1:16 maybe a little bit less than that. 1:18 And so we try to cut things a little bit short. 1:20 And so I cut the portion of my class on loops, I cut it pretty tight and I failed. 1:21 So, sorry. 1:28 >> [APPLAUSE] >> But I'm gonna try today to fix that. 1:29 So let's fix that. 1:33 Let's go and we'll dive a little bit deeper into that stuff. 1:34 Another thing that's on the negative side of things. 1:37 Sometimes when we say, are there any questions and people have questions, but 1:39 they don't ask questions. 1:43 Let's not do that, okay. 1:44 So if you have a question somebody else is thinking about that question. 1:45 Somebody else, you're not the only one. 1:48 You're like, oh he didn't explain that very well. 1:49 I wanna know about that, but 1:51 I don't wanna say anything cuz I don't wanna look like I'm failing. 1:52 No, if you have a question, I'm gonna stop. 1:54 That's how we're gonna do this ten minute thing. 1:57 This is the ground rules. 1:58 We're gonna stop, maybe a little bit shorter than 10 minutes. 1:59 But we are going to stop each time, and I'm gonna ask for questions and 2:02 I am going to clap. 2:05 I am going to have people clap for you if you ask the question. 2:05 Okay? And we will feel good. 2:08 Okay. 2:11 So here we go, actually, while there are questions, I will continue answering them. 2:11 Right, it's like the loop, right? 2:17 So, a loop, okay, so 2:20 there's like a sequence of instructions that you want to happen again and 2:22 again, and you want to continually repeat them until a certain condition is reached. 2:25 Until something that you've said stop. 2:29 Stop doing that. 2:32 Keep doing these things and then stop at this certain time. 2:33 And there is different ways of doing them in Java. 2:35 So let's look at a couple of them. 2:37 While loops is what we're gonna start with here. 2:39 These are commonly used when you don't know how 2:41 many times you're gonna loop through something. 2:44 There's some sort of condition that you're going to eventually hit, but 2:45 you don't know at the start of it. 2:48 You don't know how long that's going to be. 2:50 Right? 2:52 Now, Java has a pretty cool feature that allows you to check stuff before hand, 2:52 or if you want to, you can do it afterwards and that's the pre-check. 2:57 And the post-check. 3:00 So there's two different types of loops. 3:01 There's a while loop and there's a do while loop. 3:03 So let's take a look first at the while loop, and that's a pre-check. 3:05 That's the pre-check version. 3:08 The cool thing about that is it's sort of got a built in if, right? 3:10 All right, so what happens is, if you hit a while loop and 3:13 the condition isn't there, you're never gonna run the code inside of it. 3:15 So let's take a look at some pseudo-code really quick. 3:18 So this is pseudo-code, I am not very good at writing pseudo-code, 3:22 it usually ends up looking a lot like Python. 3:24 But I don't want it to look like Java. 3:27 I just want you to think through this a little bit first. 3:28 So here's an example of a while loop. 3:30 So we'd say, hey, are there any questions? 3:33 And while there were questions, if there were any questions. 3:35 If there weren't any questions I wouldn't have a question to answer. 3:38 While there's questions, we'll answer the question. 3:41 And then I'll say, are there any more questions? 3:43 And we'll just keep on going, keep on asking. 3:45 Once there aren't any more questions, we stop, we pop out, and 3:47 we go to the next slide. 3:50 Make sense? 3:51 All right, I'm gonna do a live coding example in workspaces, so 3:54 it's on the Internet. 3:58 And that's breaking two rules, you don't live code and 3:59 you don't use the Internet during a presentation. 4:02 So, get ready for the fail whale. 4:03 Okay. 4:08 That worked, the Internet's working. 4:14 I have the weirdest project that I have 4:15 ever created in my life on this side over here. 4:18 These are what we're gonna walk through. 4:20 So the first one that we're gonna look at here is questions 4:21 I've built the shell that you're all starting to get familiar with I'm sure. 4:25 So we've got the start of a program and we have a console available to us. 4:29 Don't need to worry about that. 4:33 So we want to ask, were there any questions? 4:34 So we'll make this pretty basic. 4:36 We'll do a console, and we'll do a read line and we'll say are there 4:39 any questions? 4:44 That's gonna store whatever they entered into this variable any questions, 4:48 which is a string. 4:51 We're cheating a little bit here, but, hey, it's a demo. 4:53 So, while anyQuestions=yes, and 4:55 it's only lowercase yes that's going to accept. 4:59 I'm aware of that bug that I just introduced. 5:04 Let's make this app, let's do like version one of Siri. 5:07 That sound good? 5:10 We'll do Siri on the iPhone, you ask her a question and she tells you what happens. 5:11 So this is what happens when I ask, I say Siri a question. 5:14 We'll say what is your question? 5:21 Okay, so Siri is going to respond with, I don't 5:25 know if she says this to you, but she says, I do not understand. 5:31 So again, that's the printout. 5:36 We're formatting and then we're going to stick in the question she just said, 5:38 because that's exactly what she says to me every time. 5:40 To know what you mean by hope. 5:43 What do you mean by call mom. 5:44 So okay any questions? 5:46 Now this is the variable that we set above outside of the scope right. 5:47 We set it here to this, but we're going to say are there any more questions? 5:51 Okay. 6:00 So, we're inside the while loop. 6:00 So here's the while has these breaks here, and see how it's highlighting in yellow? 6:02 The coding here is going to loop each time as long as things equal yes. 6:06 And then of course we're going to pretend that we're going to go to the next line. 6:10 Okay? 6:19 All right, and then I am going to use the mail line here to compile our program. 6:20 So we're gonna compile questions, and then we're gonna run it. 6:26 Then we're gonna see how it works. 6:31 What do you think? 6:34 Think we did it, or think we're gonna get a fail wave? 6:35 Let's see. 6:36 Oh! 6:37 >> [APPLAUSE] >> Thank you. 6:39 What did I forget? I forgot to close that method out. 6:43 I do that all the time. 6:46 Do that all time and the video guys get it all the time, cleared up. 6:47 All right, so let's clear that up. 6:50 Here we go. All right, are there any questions? 6:52 Actually, that's actually the next slide. 6:56 The next slide is are there any more questions? 6:57 Are there questions? 6:59 Remember, it's a good time. 7:01 Anybody that- >> This is actually [INAUDIBLE]. 7:02 >> Okay. 7:06 >> But how did you get into [INAUDIBLE]? 7:07 >> [LAUGH] [APPLAUSE] >> Christine, do you wanna answer? 7:10 >> [INAUDIBLE] >> Woo! 7:16 >> Awesome. [APPLAUSE] 7:23 >> [APPLAUSE] 7:24 >> Think how happy that makes me. 7:26 That's awesome. 7:27 So are there, there was a question. 7:29 Let's see it, see what Siri has to say. 7:30 >> [LAUGH] >> [LAUGH] 7:36 >> Siri doesn't understand, 7:44 she doesn't know. 7:44 Oh, and I forgot. 7:46 She asked, but I didn't put a new line, so I also fail there. 7:48 [SOUND] Shoot. 7:50 But anyway, so there should be a new line. 7:52 Right, so it's that percent end. 7:53 Cool. 7:56 It's a feature. 7:58 Is there another one? 7:59 Yeah, it's asking again. 8:00 Is there any more? 8:01 Let's see, where's he at? 8:01 I should've done the fail wave. 8:03 Oh, next slide, cuz I hit the. 8:05 Fail, totally fail. 8:07 >> [APPLAUSE] >> Thanks. 8:08
You need to sign up for Treehouse in order to download course files.Sign up