 Okay, hi, welcome to the mini lecture for today where we're going to be talking about loops and strings. Well, loops today probably strings tomorrow. And here's a program you've seen this a million times that written a million times where we ask for the age and years. And if it's less than zero, we can't have a negative age. Otherwise, we multiply it out and tell you about how many days it is. Okay, so if I wanted to test it if somebody's 46 years, 40 years old, that's nice around a number. Sounds good. Run it again. Negative three can't be negative. Okay, I don't know about any of you folks, but I'm getting awfully tired of having to run the program over and over and over again to test it. Gee, wouldn't it be nice if I could somehow have the program repeat all that stuff for me instead of me having to run it over and over and over again. And the answer is, well, the instructor never says wouldn't it be nice if the answer is, yeah, it'd be nice, but we can't do it. Of course we can do it otherwise I wouldn't be talking about it. And what we're going to do is we're going to use something called a while loop. I guess I'd better make my notes here. The part of Alice, you have to go in. And here's what a while loop looks like. You have some variable that you set. Then while some condition is true, you do the body of the loop. You know what, I probably should make a similar, I'm going to use one of the things that they haven't, so I wouldn't use one of the ones they have in the book. Sure, why not. And this is going to count down from five to zero. This is a good example of an example. It's not a good example of something you'd use in everyday life, but I need something that's straightforward. Let's have an integer called counter and we're going to set it to five. As long as the counter is greater than or equal to zero, we're going to print the counter. And then we're going to set the counter to counter minus one. So here's what's going to happen. And then when we're done with system five is greater than or equal to zero. That's true. Since the condition is true, we have to print out our counter, which is five. We're going to take five minus one is four and put it back into the counter. Now we come back up to the top of the loop is four greater than or equal to zero. Why? Yes, it is. Okay, good. That means we print out the counter, which will be a four, and then counter becomes three four minus one. And we come back up to here is three greater than or equal to zero. Well, we keep going on this eventually counter is going to be a one. So I have one minus one. We're going to have a zero count zero is greater than or equal to zero. We're going to print the counter and then we're going to take zero minus one, which is negative one and put it back into counter. Now, here's the important part, you know, the loop is done. I know the loop is done. Everybody knows the loop is done, but Java doesn't. It always comes back and tests this again. And it says, okay, is negative one greater than or equal to zero. No, it's not that means the while loop is ended, and it's going to print out the words of the end. Because I should be given since I got rid of my scanner, I guess I'd better get rid of all of it. Curse me. Okay. Fine. Let's make it match up or lower case. And let's run it. And there it is. Okay, so that's the whole that's the idea of a while loop. I happen to have a participant here. Somebody watching as I'm actually doing this. So if I do is saying anything that you don't understand or if there's anything that you'd like me to expand upon just let me know. Okay, let's go back to age dot Java. Now what we're going to do is this. We're going to enter the age and years. Yeah. We want to keep doing this. Now the question is how long do we want to keep doing it. And we want to keep doing this until we get some signal from the user that they're done. And one way that we can get a signal from the user that they're done is if they enter a zero. So what we're going to do is we're going to say enter age and years, or zero to quit. Now we're going to read the years as long as the years is not equal to zero. Then we're going to go through all of this shabazz here. So let's say the person puts in 20. 20 is not equal to zero. It's not less than zero. So we're going to multiply 20 times 365, which is 70. 7300 I think, and it'll print 20 years about 7300 days. Are we happy with this? Well, let's see what happens when we run it. Let's compile it first. And then we run it. I say 20 years. Hmm. What we have done is we've created something called an infinite loop. This loop is not going to stop ever until we close this window, or press control C to interrupt the program. Why is it? Why did it go forever? Well, let's think about it. I didn't go all the way through what the loop does. 20 is not equal to zero. 20 isn't less than zero. So we calculate 7300 and print that out. Now we come back to the top of the loop. What's years? It's still 20. We didn't ask again. So since years is not equal to zero, it's not less than zero. We use 20 years and print that come back up here. Oh, yours still is not equal to zero. It's still 20. What we have to do now is we have to say, enter next age or zero to quit. And then years becomes input next into. It's very important. This is one thing that the book says you always make sure that when you have your loop variable, you have to update your loop variable. Otherwise, you may end up with that problem that we just had. So let's compile this. And now let's run it again and see if anything works better. 20. Good. Negative five. Can't be negative. 37. 19. Now zero. And the program ends because there's nothing else after this loop. Just to make sure that you took care of it. Just to convince you that it really did exit the loop and not the whole program. So we have 35 negative to 17. 20. And zero. So that's one use of a while loop. This is because we don't know how many times somebody is going to be entering things. As long as the years is not equal to zero, we're just going to keep doing this. Ah, interesting point here. Let's run this. If you enter zero right away. It does absolutely nothing. Remember, let's say I put in a zero here for years. Zero is not equal to zero. Well, that's false. Zero is equal to zero. And therefore, when this became false, we immediately exited the loop. So that's a very important thing to notice. If the condition is false. When we first encounter. The while loop. The body will never get executed. So let's say this. Everybody happy with that. I'm trying to think of something that. What I'm going to do the next is a little bit weird, but I think I can make this happen. Okay. Let's run this over again here. And if somebody types in. Boom, everything crashes because FIV is not an integer. What we would like to be able to do here is this. Let's do this. While it is not true that input dot has a next integer. I'm not sure if this is going to work. I'm, I'm, I'm working without a net here. This should be updating our next. This, this, this may, this is going to, this is probably going to fail. Dramatically. This should be fun. So if I type in five. Yeah, I got an infinite loop. Okay. Curse me. Ah, this is for a whole number. What we need to do is we need to clear out. Let's try that. And let's get rid of this here. Okay. Now let me explain what I just did there because this is all terribly mysterious to you. Yeah. As long as I don't have an integer waiting for me, whatever they typed is not an integer. I have to read in whatever the line was and that clears out everything they typed. I don't care what they typed. I'm not going to use it for anything. I'm throwing it away. Then what I'm going to do is I'm going to print the prompt again. And I'm going to check again for an integer, which means it's going to have to stop and wait for more input. At least. If I've done this right, if I haven't done this right, this is just going to turn into the world's greatest mess, but oh well. Okay, so let's say I have if I be. Okay. Excellent. If I say 27. It's working. Enter next stage or zero to quit. Now, unfortunately, if I say five, I still have that because the only one I trapped it on was right here. This one. I didn't. Now that means, oh gosh, I've got to do another while loop in here to make sure I have good input. Question. Do you know for the person who's watching, do you need me to go over this while loop again? No, I think it's good. Okay, good. So, well, if I'm going to have this core code, which is going to be essentially the same. Here's what I'm going to do. I'm going to make a method for this. Let's have a public static called. Give us back an integer. And we'll call it get age. We'll give it an input scanner to use, and we're going to give it a prompt to use. Then what we're going to do is we're going to say system dot print prompt, whatever the prompt is. And then we're going to say, as long as input doesn't have a next integer, we'll clear out whatever they typed these use digits. And then we're going to do a system dot print of the prompt again. Then once we're out of the loop, we do have an integer. We can say result becomes integer scan, input dot next int. And we can return the result. So this nice little get age here method. And wait until we get an integer, a real live integer. And then I can use it as often as I want to without having to repeat all that code. And that's why we love methods, by the way. So here instead of saying input dot next int, I'll say get age. We'll give it our input scanner and the prompt, which is enter age in years or zero to quit. And we don't need this prompt anymore because all the prompting is going to be handled here by get age. Then we can call get age again. And again, we don't have to print it here because get age will take care of that for us. Let's run it. So now somebody says F I V E 12. Now the next question is, well, what happens if they give us an integer right away? What if they give us a like 45? Are we going to get a message and the answer is no, it's going to just give us exactly what we want. Why is that? The first thing we're going to do is we're going to call get age with our input scanner and the prompt. It's going to print out enter age in years or zero to quit. And now because we typed 20 or 45 or an actual integer. Input dot has an accident is true. Not true as false. And remember the moment we have a false condition we drop out of the while loop, which means we never have to go through all of this stuff. So if we do have an integer ready friend waiting for us, we read it and return it. And that's what makes this work pretty nicely actually. Again, let's run this. You know what, there's something I want to do here and also here after the reprint this. I don't really need to do this is not going to affect whether the program works or not it's just going to make things look a little bit nicer. So if I say, these digits. So that's a nice use of a while loop. We can use it to validate data. Alright, this is what I was going to do earlier. I was going to take this days per month and put in some couple of methods. I can't type and get year. And then I could use that here for the enter month and enter year to make sure that I had a number that was. Do I need these to be different? No, I don't need these to be different because here I'm already doing the test for the month. And here I'm already doing the test for a valid year. So really, I only need this to be get integer prompt. And so I'm just going to leave that there and if anybody wants to go and implement that that would be just terrific. These while loops are great when I don't know how many times something's going to happen. That's why I did not like this countdown one because I know exactly how many times it's going to happen. It's going to happen five times or six rather because I'm going to greater and then equal to zero one, two, three, four, five, six. I know exactly how many there are. But I needed a quick example of a while loop. In general, we use a while loop when we don't know how many times we have to go through the loop and we're waiting for valid input. Somebody might be really stubborn and give us wrong data 20 times in a row. Other people might just give us one bad data and some people might give us zero bad data items. We don't know how many times we have to go through the loop. We really want to use a while. Now there are times when we do want to know exactly how many times the loop. For example, if I would need to average seven days of a week's worth of high temperatures. I know I need to loop seven times. And the sum of quarterly sales. I'm going to loop four times. It's our four quarters in a year. When we have a situation like that. This is a good place to use a for loop. And the way it works is we have for we have initialization part. A condition. And then I'm going to call it an action part. And then the loop body. And what this does is that this is exactly the same as saying, do the initial initialization. As long as the condition is true. Do the loop body. Then do the action. So this is a nice little shortcut. Let's take this one because we know how many times it's going to go. Let's save this as. We're going to do the opposite transformation. We're going to say four. Here's our initialization. Our condition is going to be counter greater than or equal to zero. We don't need to put it in parentheses, by the way. And our action is going to be subtracting one from the counter. So I've taken the action. And put it into. The for loop. I've taken the condition and put it into the for loop. Which means I can get rid of these two lines here. Compile it. And run it. And we get the exact same result. But it's a little bit more compact. Man. There we go. I'll get, I'll get this. Working at some point. And this is going to count. From one to. I'll say 10. So here we're going to say counter becomes one. As long as the counter is less than or equal to 10. And this time we're going to say counter becomes counter plus one. You get one through 10. Now. There's a shortcut for this. So we're going to say, Now. There's a shortcut for this. Whenever I want to add one to a variable. If I say counter plus plus, that's a shortcut for. In fact, I'll put that in here. So if you look at this later. There's also counter minus minus, which is a shortcut. For counter. Becomes counter minus one. And you will see everybody who's a professional programmer using this. Almost nobody will ever write it this way to be quite honest. And sure enough, it works, which means, by the way, I can now use my countdown to. And let's save that as. Countdown three dot Java. And here I could put. Counter minus minus. And it works exactly the same. But this whole plus plus and minus minus business is pretty weird. Okay. When we have these alone. Inside of a statement. I could also put it. This way I could say minus minus counter. I believe this will work. Let's find out and see if that does the same thing. Yes, it works fine. So let's put this in the notes. You know what I'm going to just. I'm going to hedge my bets here. Has the same effect as they are not precisely the same thing. But it has the same effect. That's more accurate. And that's the only. Increment. Which is a fancy word for saying add one. That you're doing. Everything is great. But if you have more than one of them. In an expression. You have to be very careful. For example, if I say integer, let's say a is 27 integer B is. Five. And I say integer C becomes a plus plus. Let's make this a different. Let's make this a 27. Let's do this. Again, let me rephrase this to be more accurate. You have to know. When the increment. And decrement, which is again, a fancy word for. Subtract one. I am not going to go into that right now. But that is some here. In the extra exercise book. I'm not going to go into that. I'm not going to go into that. I'm not going to go into that long shabazz about this. Okay. About the plus plus and minus, minus operators. And you have to be very clear. It says when the variables all by itself, there's no difference between the operator before or after. But if you use them inside of an expression, then there's an incredibly big difference. So for example, if I say, if I put plus plus and I'm going to get different answers. You can read this at your leisure. This is the kind of thing that often appears on tests, by the way, that you know the difference between a post increment, namely the increment comes after. Versus a pre increment where the increment comes before. So they are different and it's something you should know about. I'm going to talk about compound assignment probably tomorrow. And let's see where I where did I stop here in the other book. For statement I talked about that was excellent. Oh, whoa, what's this? We've never seen anything like that before. I've got a couple of minutes. Let me talk about that real quick. And I'll put that in the notes also. It's called a compound assignment. A shortcut if I want to say I becomes I plus five. I can shortcut that as I plus and becomes five. Or let's say I have J becomes J times 7.3. That's J times and becomes 7.3. This only works when the variable on the left hand side is the same as the first variable on the right hand side of the assignment. There's no shortcut for that. And I can use this with any of my operators. So I can say I'll say minutes, which is the same as saying minutes becomes minutes mod 60. And that's a compound assignment. And they're very handy. Again, you will see people using these all the time. And I read this as again, plus and becomes times and becomes and mod and becomes. And there are different ways you can read it. I don't care how you read it to yourself as long as you understand what it's a shortcut for. So compound assignment, very useful. And I think that's quite enough for one evening here. I'm going to stop recording here.