Last time, we finished our discussion of for loops. In this lecture, we'll look at while loops which help us solve problems where we don't know how many times we're going to need to iterate when we get to the loop. One of the places that people regularly use while loops is for something called input validation. We're going to have the user input some data for us and we need to validate or make sure that it's within a particular range or a specific format or something like that. We're going to do input within a particular range in this lecture. So what we're going to do is we're going to have the user enter a test score. So we'll prompt for and get the score. Of course, before I do that, I'll actually need a variable to hold the score. First, we'll prompt. If we have an expectation that the user input will be within a particular range, we should tell the user what that range is and then we'll read it in. Here's the while loop part. We put the while keyword and we put parentheses, and we put curly braces, and of course, as always, if the body of the while loop is only one line of code, you can omit the curly braces, but I suggest you always include them. In between these parentheses, we put a Boolean expression. The way while loops work is if the Boolean expression evaluates to true, we're going to go into the body of the while loop. So the Boolean expression we need to specify here needs to be an invalid test score, that's the only reason we're going to want the loop to ask for a new input from the user. This may seem counter-intuitive to you, but we need to write a Boolean expression for an invalid score. An invalid score is either less than zero or it's greater than a 100. Any score that's between zero and 100 inclusive is valid. So for those scores, this Boolean expression will evaluate to false, but for invalid scores, this will evaluate to true. So we should print an error message and get a new score. First, we'll print an error message. Score needs to be between zero and 100, and then we prompt for and get the score just like we did above. Let's see how this works. If I enter a valid score, we're all done. That tells us that the body of a while loop executes zero or more times. We entered 100 up here, and this Boolean expression evaluated to false so we immediately went to the line after the while loop body. Of course, the zero or more times, I can make it execute more than zero times. Here's an invalid score and it looks like I need some line breaks, but now, I'll enter a valid score and everything is okay. Let me put some line breaks in here. One to offset the error message from the user input they just provided. Of course, one here as well. We'll just test to make sure I did that properly. Negative five, five, and we're good. The reason we're using a while loop here is because we don't know how many times the user is actually going to provide invalid input. We can't use a for loop, right? We don't know what the limit is for how many times the user will either be confused or malicious. We'll just keep looping with invalid inputs as long as the user keeps entering invalid inputs and then finally, when they finally enter a valid one, we'll break out of that loop because the Boolean expression evaluates to false. I'm going to add one more comment here. I also want to mention that people will sometimes talk about getting stuck in an infinite loop. The for loop is not as prominent to infinite loops. You can build an infinite loop if you choose to with a for loop. But people usually end up with infinite loops by mistake. I will point out that there will be times, as you look at code, that you'll see somebody do something like this. They'll say while true, which is by definition an infinite loop until they put some code inside here to break out of the loop when you should break out of the loop. There are certainly places where that structure is appropriate but certainly not in input validation. We should just do it a more reasonable way like this. But people can get into infinite loops and there are three things we need to think about when we are trying to avoid infinite loops for our while loops. And the initials for those things are ITM. ITM stands for initialize, test, and modify. Let's talk about them one at a time. First of all, before we get to our while loop, we need to make sure that we have initialized our loop control variable to a reasonable value. Now, it turns out that we could have multiple loop control variables for a while loop and that's fine. We should make sure they're all properly initialized before we get to the loop. In this particular example, we've initialized score before we get here so we don't need to worry about that particular issue. The next one is test. We need to make sure that this Boolean expression that we write is correct. But let's say we got confused as we were writing this Boolean expression and we said, while score is greater than zero or score is less than 100, we got our relational operators incorrect. When we run the code, I can enter any number I want and it gives me an error message. That's true of invalid numbers and more invalid numbers and you say, "Oh, I'll take a 50," you're just stuck in this infinite loop forever. The problem is that you can't think of a score that is neither greater than zero nor less than 100. Some scores are both, right? Any score that's 1-99 will meet both of these criteria for the or, but all numbers meet at least one. So that's an example of getting the test incorrect and ending up in an infinite loop. That last initial M for modify tells us that we need to be sure to modify at least one of the loop control variables that we have controlling our loop, which we're doing with this line of code. But if we forgot that line of code and we were in our code, I'll have to enter an invalid score to push us into the body of the while loop the first time, but then I go forever. Let's talk about how this works. If this Boolean expression evaluates to true, I go into the body of the while loop and I do this stuff, and I never change score. So when I loop back around to the top to check the Boolean expression again, it's still true because I never change score. So I go to the body and loop back around and it's still true and so on. I'm going to fix this. Those are ways that we can end up with an infinite loop. Now, it's not really infinite. At some point, you'll rage, throw your computer against the wall, or the sun will explode or something. They're not actually infinite but the while loop doesn't actually behave properly in terms of how many times it iterate. To recap, in this lecture, you learned how to use while loops to solve problems where we don't know how many times we'll need to iterate when we get to the loop. You learned that the body of the while loop executes zero or more times. You learned that thinking about initialize, test, and modify helps you avoid writing infinite loops.