Over the last couple of lectures, we've been using for loops because we knew when we got to the loop in our code, how many times we're going to loop. Sometimes though, we don't know how many times we're going to loop. We're going to learn how to implement something called a while loop today. While loops let us loop an indeterminate number of times while some condition is true. One of the most common uses for while loops, is to do something called input validation. In other words, we ask for input from the user, and then we want to validate that input to make sure that it's within an acceptable range or in an acceptable format or something like that. We'll do something within an acceptable range, this time, we'll ask for a test score, and the only valid test scores that we'll accept are 0-100 inclusive. So we'll start by, prompt for and get score. Of course, I will want a variable to hold their score as well. As always, it's reasonable to tell the user what the valid range is when we ask them for input. So this is all standard stuff that we've seen before, now we went to make sure that they're actually entering a valid score. So we're going to use a while loop, and we have the keyword while, followed by parentheses, followed by curly braces for the body of the while loop. As always, if there's a while loop with only one line of code in it, you don't need the curly braces, but I suggest you always use them. Between the parentheses, we provide a Boolean expression. The way while loop works is, while that Boolean expression evaluates to true, we're going to go into the body of the loop and loop again. So the Boolean expression I need to write here is something that will evaluate to true if the user's input was invalid. That means that if they enter something that's less than zero or greater than a 100. That might feel counter-intuitive to you, but remembering that we only want to loop when we have to get a new value for score from the user, it makes sense that the Boolean expression has to evaluate to true if the score is invalid. Here, we'll print an error message and get a new score. Of course, I know we're going to want a new line before we print the error message, score must be between zero and 100. Then, we just prompt for and get this score again, like so. Okay, lets run it with a few different inputs, and then we'll talk about how it's actually working back here in the code. First, I'll provide a valid input. I'm smart, I got 100, great. We didn't even see the while loop execute. If the score is valid here, when we check this Boolean expression the very first time, the Boolean expression evaluates to false, so we don't ever enter the body of the while loop. That tells us that the body of a while loop executes zero or more times. Now, let's say we have someone who enters an invalid input, so they get the error message and they get to try again and they say, "I didn't mean negative five, I meant five." What happens is, they entered negative five the first time. Negative five is less than zero, so this part of our Boolean expression is true, and true odd with anything is true, so we're going to enter the body of the loop. We print the error message and we get a new score. This time they entered five, so we come back around and five is not less than zero and five is not greater than 100, so this Boolean expression is false, so we leave the while loop. Now, let's go ahead and say somebody who is either really having a hard time understanding the R prompt or is just malicious. So they enter negative five, and they enter a big number. This is going to keep looping until they finally say, "Okay, I will enter a valid input." So in that case, we had an invalid input, so we went into the while loop. They provided another invalid input. So this was still true, so we stayed in the while loop. We were going to keep looping until they finally entered a valid input. That's how we implement while loops. But you will regularly hear people talk about getting an infinite loop. Although it's possible to write an infinite for-loop, you have to work hard at that. No one writes infinite loops on purpose, so this is by mistake. The three things we need to worry about, are the three things we actually included in a for-loop. We need to initialize our loop control variable. We need to have an appropriate test or condition to tell us when to stop looping, and we need to modify our loop control variable. For this while loop example, score is our loop control variable. We're making sure we initialize it before the while loop right here as we read in that input from the user. So we don't have to worry about that, but we could get our test wrong. If we got the less than and greater than confused and we did it this way instead, that means that we've got our condition, our Boolean expression, incorrect. I'll run it to show you what happens. Somebody says 100 and they get an error message, and they say, "No, really I got a 100" and they got an error message. They say, "Okay, 50. I'll take a 50." Even if they enter something invalid, it will loop forever. The reason that happens is, you can't find a number that is not greater than zero or less than a 100. All numbers are greater than zero or less than a 100 or even both, right? For 50,50 is both of those things. So if we get our test incorrect, we're going to end up with an infinite loop. Now, of course, it's not actually infinite, right. Eventually, the sun will explode or your computer will burn out or you'll give up and reach quit from the program, but the term, infinite loop, just means that it keeps looping more than it should. The last thing that we have to worry about, is modifying our loop control variable. Let's talk about why that's important. What if we comment this out so that we don't actually modify score inside the body of the loop. This Boolean expression had to evaluate to true for us to get into the body of the while loop. If we don't change score in the body of the while loop, when we loop back around and check this Boolean expression again, it's still going to be true because we didn't change score at all. So if we don't modify our loop control variable, and we can have multiple loop control variables in a wild loop. This particular while loop only has one, but we have to modify at least one of the loop control variables in the body of the while loop, otherwise we'll end up with an infinite loop. I'll show you that as well. I have to enter an invalid input to push us into the body of the loop, and then the loop goes forever. Let me correct this before I give it to you. I'll test it to make sure it's working properly. It is. So there you go. That's how we do while loops. In this lecture, we learned how we can use a while loop to loop an indeterminate number of times while some condition is true. We also learned about the importance of initialize test and modify as we make sure we avoid infinite loops.