 Since we can't meet for class today, I thought it might be helpful to show you a couple more examples of loops, and hopefully this will give you some hints and some guidance on the homework assignment this week. So I have right here a hello world program just to get started with, right? So if I hit run, it's going to say hello. And for my first example, I want to create a little game that lets you pick a random number between one and 10 and then try to get the user to guess that number. So I'm gonna do this in a separate method. Then we go ahead and create public static void. I'll call this guess, and you have to tell it what number that you want the user to guess, right? So for example, in my main method, I might call guess and say, let's say seven is our first number. I don't know, that's one of the most common numbers between one and 10 that people like to guess. And okay, so I might as well write a little java.comment if I'm getting started here, and it plays the guess my number gain. The number should be between one and 10, right? And so the parameter is going to be the number to guess, right? I'll hit control shift F so that all formats, it's save, and everything compiles just fine. So I think what I might say, let me grab this hello and put it up here now, and then we'll say guess my number, all right? And so what I wanna do is, normally if I were to do a scanner input like we've done before, let's see, create a scanner, and it hasn't been imported yet, so let me go ahead and do that. Scanner eight equals new scanner for system.in, and I would do something like display a prompt, so system.out.print enter a number between, well, let's just say enter number, let's close it up. And then the next thing I would do is in.nextint, right? If this is going to be reading an integer, and I'll have to assign this to some kind of number. So of course, number, the variable is what I wanna guess. So let's just say input is in.nextint, and then I could say with an if statement, like we've seen before, if input is number, then I'll say you got it. And if it's not, then I could say you didn't get it. Let's just say nope. Okay, so if I run this program as is, I don't have any loops yet, but we're about to add a loop to it. Let me just run this to make sure that it works. You know, I'll always say before launching, okay, guess my number, enter a number, five, nope. And of course, if I run this program again, guess my number, and I put seven, which is the number that I wanted to play. And so you got it. So I have a basic program so far. Let me get rid of this extra output we don't really need. And this, now let's say I wanna keep guessing until I get it right. So in other words, I need a loop. And in Java, there's basically two kinds of loops. There is a while loop, which will run indefinitely. So sometimes we call this an indefinite loop because it's going to keep looping until some condition is no longer true. There's also a for loop that will run for a specific number of times. So sometimes we call that a definite loop. We definitely know how long we want it to run. But in this case, I don't know how many times the user's gonna guess until they get it right. So I need an indefinite loop. So let's just say instead of if input equals number, I could say while input is not equal to number. And so when it's not equal to the number, I wanna say nope, maybe I could say try again. And then at the very end of my method, I could be like, you got it, right? So when I'm done looping and when the number actually equals the input, then that's when this little game will end. Now the problem is I need to keep asking for the input. So this part right here about intro and number should be inside the while loop. And now I've got my code all out of order because input, the variable, has not even been declared or initialized. I can't declare and initialize it here, three lines after I'm using it. So with the while loop, you have to do a little finangling. I could say, for example, input, and let's just make it negative one because I know if the number's between one and 10, then negative one is never going to be one of the correct answers. So that at least makes it so I can start using this in the while loop. And then instead of declaring it again in the loop, I can go ahead and say next int. So, and now maybe if I'm going to print out the message, nope, try again. Well, I only want to do that if it's not correct. So why don't I say if input is not equal to number, nope, try again. Okay, so what this program does is it creates a scanner, it initializes input to some dummy value, and as long as my input is not the number I'm trying to guess, I'm going to say int and number, read it in from the keyboard, and if it's not the number, then I'm going to say nope, try again. Otherwise, it will get me out of this loop and I'll be like, you got it. So let me run this program to make sure it works, int and number, one, nope, try again. Two, nope, try again. Okay, seven, you got it, right? So again, this while loop is going to keep repeating this code here in the middle until it satisfies the condition. Now, there's a couple of things that are awkward about this while loop. You'll notice I have the condition if input is not equal to number twice. It'd be nice if I only had to write that code once. It's also awkward that I have this variable in input equals negative one. In general, you should never have to initialize a variable until you actually have a value that's meaningful for it. And so what I want to do instead is use a different version of while that allows me to get rid of these kinds of issues. In fact, that's called the do while loop. What's nice about do while is that you know you need to do something at least one time and the while will keep doing it until you're satisfied. So whereas a while loop may never run if the condition is false to begin with. So if I were to change this into a do while, I'd have to put the while at the very end. So I'll just say do and then the while comes here at the end of that closing brace. So I want to do this while input is not equal to number. And what's nice about this is I don't even have to initialize the variable anymore because it's going to get initialized when there's actually a value for it. So do while is nice, especially for input validation when you know you're gonna have to run the code at least one time and then you don't have to do like extra work like initializing to dummy values. The other thing that would be nice is how do I get rid of this condition twice? Well, let's see here. I could just say enter a number and if that's not equal number, nope, try again. I can't really see an obvious way of doing that unless I go ahead and get rid of the condition altogether. I could if you want to loop forever, say true, right? So a while loop is going to run as long as it's true and this is actually called an infinite loop. While true means keep running forever. And now I need a way to get out of this loop. So what I could say if it's not equal to number, do that. Otherwise I could break and break is a way to jump out of a loop. So this will terminate the infinite loop as soon as the number it matches, right? So, and maybe it'd be a little bit cleaner if I formatted this round and say, you know, if it is the number instead of having a break statement, I could just be like, you got it. And then I could return from the method. And instead of saying, no, try again. Well, I'll just do that before looping back around, right? So there's lots of different ways to compose a solution, whether you use while or do while, whether you have a condition or whether you have a break or return. These are all tools in your toolbox for designing an algorithm to repeat instructions and make decisions. So in the end, the one that I'm demonstrating here is going to do everything forever. And eventually when I do guess the number, I'm just gonna return from the method and that basically terminates the program anyway. Of course, before I do that, I say, I got it. If I do loop back around, then I'm gonna say, no, try again. Let me just test to make sure that this code is working here. So enter a number, let's say, three, no, try again. Okay, six, no, try again, seven, you got it. Now, I could also add other functionality to this loop. Let's say they enter a number or let's say they don't even enter a number, right? I could be like, if in dot has next int. So it's possible that I might type the, well, here, let me show you a demonstration before I give you that scanner code. I'm gonna hit play and I say enter a number and I'll be like, ha, ha, ha, ha. And it's like, oh, I broke the program, right? Because I got this input mismatch exception. It wasn't expecting ha, ha, ha, it was expecting an int. And so in this situation, it'd be nice if I said something like, if in dot has next int and or rather if it does not, so if not in has next int, what I could do is say, you know, system dot, I could use system dot error if I wanna make it really an error message. I'm just gonna use system dot out and not deal with the error stream. So system dot out dot print, let's just say I need a number or let's just say, oops, or it doesn't matter what you say, you can come up with your own error message, right? But the idea is that I'm going to go ahead and read that next token, which is like the word ha or ha, ha. I mean, it's gonna read each token separated by spaces. So technically if I were to type ha, ha, ha on the line, it's gonna call this ha, ha, ha three times. But let's just do int dot next to I discard the invalid token. And then I can go ahead and hit continue. Continue is the opposite of break. Continue says go back to the start of the loop so you can see how do is highlighted by Eclipse right now and continue running that loop from the start again. So it's gonna ignore all this other stuff and then just try over again. And so this code will allow me to get rid of invalid input. Let me see if this works now. So if I hit play, enter a number, ha, oops, enter a number and I guess I should use print line there so it doesn't put it on the same line of output. And okay, in fact, let's go ahead and save and restart the program now. So if I were to say ha, ha, notice how it's like oops, enter a number, oops, because each of these ha's were read in different iterations of that loop by indot next. See, indot next is only gonna discard one word of the input. If I wanna get rid of the entire line, I could say next line and that's gonna read all the input to the end of line character. And so now if I hit play again and I say ha, ha, ha, you can't catch me, it's just gonna say oops and then say enter a number again. And I guess I could leave you with one exercise without showing the solution. What if I enter a number like 15 that's not between zero and 10? It would be nice if this guess method had an additional if statement that was like if the number is less than one or if the number is greater than 10, print out an applicable error message and then continue running the loop again until they enter the correct type of number. But you see basically how this works is I'm just repeating these decisions until I get the input I want and then eventually the game is over and I say you got it. That's how while loops work. They run indefinitely until the condition you're looking for is true. So let me now show you a different example using for loops. So let me just say demo of indefinite loop which is called a while. Let's say I wanna make a program that's we'll pick an example from math. If I give you a number like say 20 and I want to list all of the numbers that divide evenly into 20, we call them the factors of 20. How would I make a program that prints out every factor of an integer? Let's go ahead and call this factor. And in fact, I'll just go ahead and put the number 20 there as we get started. And this is going to be a demo of definite loop which we call for. And of course factor doesn't exist yet. So let's create a method called factor. Hey look, it even did it for me by clicking that little link, that was nice. I'd like to have my methods above main. So let me go ahead and just cut and paste that and put it up here. And we'll say that this is a prints the factors for the integer, for the given integer, let's just say. And let's just say the number you want. And of course can't misspell. And let's get rid of these to do every time the Eclipse generates a method that puts this to do comment. It's good to delete them when you actually complete the to do. In fact, if you ever put to do in all capital letters it puts these nice blue check boxes in the margin. So you can add to do comments throughout your program. And as a way to like scroll through and make sure you've done everything you meant to do. But I've been taking off points if you submit code with the to do. So just go ahead and delete it and don't submit any sort of incomplete code that way. Okay, so let's say that in the factor, so let's say I have the number 20. How would you tell which numbers divide into 20? Well, I have to look at all the numbers from one to 20. And a for loop is a nifty way to do that. I could say for X, let's say, well, let me go ahead and declare the int X and you can declare it inside the loop like this or you can declare the variable outside the loop. In fact, let me put it here just so you can see you don't have to put it inside the for statement. So I'm gonna start X at one and I'm going to count up one, two, three, four, five, six, seven, et cetera until I make it up to this number I. You know what, that variable I isn't very useful. Let's give that a better name. And in fact, if you hit the, there's a shortcut key in Eclipse to rename a variable. It's underneath refactor. So I guess you hit like control shift R or something like that. I'm on a max. I'm not sure exactly what the key command is. But now what's nice is when I rename it, it renames it everywhere. So if I type like the word number, notice how it renames it here as well as in the Java.com and this kind of handy. Okay, so number I guess is a little bit more meaningful than just I. And now I can say for X equals one, X is going to be less than or equal to number. So I want to keep iterating until I get up to that number. And then we'll say X plus plus because every time in this loop, I want to add one to X. And just to demonstrate how this works so far, I'll say system.out.printline. Well, let's just print the value X in this loop. And so I don't want to have to play the guess my number again. So I'm going to comment out that part of my main method right now. So I'm only running this factor method with the argument 20. And if I hit play, now what's wrong now? First sentence done with a period. Oh yes, semicolon's not a period. So I'm going to go and fix that. And now it doesn't like how I don't have a space after my slash slash, you can fix that too. Okay, check style's happy. It's always good to take care of check style errors as soon as they happen. Don't put them off till later because they just kind of pile up and then you forget what you were doing when they happen. Okay, so let me go ahead and hit play. And notice how in my output, it just printed the values one through 20. And it might be confusing how that happened. And if you really want to understand how these things works, I strongly recommend using the Eclipse debugger. So for example, I can go ahead and set a break point right here on line 41. Or let's see, I guess you have to double click. Let me double click line 42. I don't think you can break on a declaration because that doesn't actually run as code. That's just part of the compiling. And now instead of hitting run, I'm going to hit the debug button. And that's going to launch my debugger. Well, let me accept the connection and then we say, yeah, you can remember my decision and always switch to debugger mode. So it's going to rearrange my windows and allow me to step through the program one line at a time. And when I hit step over, which I guess is just the F six key, you can see how this is running one line at a time. And notice on the right, it shows me the value X. So each time this loop is running, it's going to increment X to three and then print it out and then four and then print it out. And X is going to keep incrementing all the way until it becomes 20. So let me actually show you that part. I've got to 13, 14, 15, 16. It's nice that Eclipse over here on the right automatically highlights any values that have changed in the debugger. So now it's 20 and I print X. I'm going to increase it to 21, but that terminated it because 21 is not less than or equal to a number. And so then the method ends and the program returns. You'll notice I've got some old programs running here. Let me go ahead and stop. I must have run these partially and never really completed them before watching another one. But in the debugger menu, you can terminate all your old partially run programs and then go ahead and hit this X to clear them out of the history. That kind of cleans up some resources on your computer. So I'm going to switch back to the Java perspective. Since I'm done debugging, let me go ahead and turn off this breakpoint because I don't need it anymore. You just double click on the line numbers to turn a breakpoint on or off. And so now what I want to do is I have these numbers from one to 20. Maybe if I want to see if it divides, I can just use mod. So if number mod X is zero. So in other words, if I divide the number by X and there's no remainder, then that's one of its factors. It divided evenly. So for example, four is going to divide evenly into 20. Two is going to divide into 20 as well. But three does not divide into 20. There's a remainder when that happens. So maybe what I want to do is say system.out.print. And let's just say... Well, let's use a print F just for fun. I could say D is a factor of D and let's put a new line at the end of that. And I'm going to say X is a factor of number. So that allows me to specify the whole line of output. Go ahead and put X and number in there. And now if I run this program, you'll notice how it says one is a factor of 20, two is a factor of 20, four, five, 10 and 20. Well, every number, one is a factor of it, every integer rather, right? And so it's not very useful to say that one is a factor of it. It's probably not useful to say that the number itself, I mean even prime numbers are a factor of themselves. So let's actually change the conditions to start counting it to. And let's stop before we hit the number because that will sort of trim off the first and the last result there. So what's nice about a for loop is you have the initialize, the test and the update on one line of code. And so if you want to control the behavior of the loop, like where it stops and where it ends, all that information is on the same line. It just allows you to debug or to customize without going all over the code, looking for those different parts of the loop, say if it were a while statement instead. So when I run this program, you'll notice it just lists two, four, five and 10. I can change the number 20 to something, I don't know, we're interesting, let's try 100, right? And so now it's going to say, oh, quite a few more. So two, four, five, 10, 20, 25 and 50, those are all of the integers that divide into 100 evenly. And of course I could pick a prime number like say 17. And when I run this program, nothing prints out because it's prime. Maybe as an additional exercise for you, can you imagine writing a method that tests whether a number is prime? Basically what you have to do is keep track of if you ever find a factor. So you could have, for example, a boolean variable is prime and it could be initially true. And then as soon as you find a factor, you set that variable to false because it's not a prime number or something that buys into it by definition. And then at the end of that method, you can then return that variable, whether it was true or whether it became false. So that's a simple primality test. There's lots of different algorithms for testing if numbers are prime. You could probably look some of them up on Wikipedia. But that'd be a nice little exercise to try out with loops. So I hope this video was helpful. Again, we've got a while or a do-while loop that shows you how you can keep running code until some condition changes and you may not necessarily know how many times that's going to repeat but when it does then you can even break it or you can continue it or you can make it repeat forever by just putting true there. Whereas at the for loop, you're going to run a specific number of times. I'm gonna go from two to number and that range is known before the loop even begins. I may not know necessarily what the value number is but it was given me from like the main method. So the computer knows what it is. Incidentally, if you want to see an infinite loop with a for loop, like a while true, you can just type for with two semicolons. Of course that breaks this code because X is no longer initialized but this is kind of a weird looking idiom in Java where it's the same as saying while true. It's just a for statement that doesn't do anything inside so it's gonna keep doing it basically forever but it's considered pretty bad style to have these empty semicolons so if you are gonna do an infinite loop you should just do while true. Well, I've probably told you more than you needed to know and hopefully these example programs and the hints I've given you today will be helpful on your next homework. Let me know if you have any questions and good luck.