We spent the last few lectures talking about for loops and for each loops which we used when we know, when we get to the loop as we're running our code, how many times we're going to loop. In this lecture, we'll look at while loops which give us away to iterate or loop when we don't know how many times we're going to need to loop. So of course you need to go do an in video quiz. And now we'll go look at how while loops work in C#. We'll start with code that doesn't have a while loop at all and then we'll add a while loop to it. So all we do is we prompt for and get a level from the user, and then we print out that level. So if I run the game, we'll see of course I'm level 110 and that's great but the problem is that I can also say I'm level 111 and it says great, and I could be really bat at wow and say it's -1. So I really ought to do some input validation to make sure that the player enters a valid level. Now I will say that we're going to work on the assumption that they're entering an integer. So if they decide to answer those questions by saying Bob, our code is going to blow up and that's just the way its going to be. In the next course, we'll learn how to do something called exception handling that will keep our code from blowing up when that happens. But for now we're just going to make sure that the integer that they provide to us is in the appropriate range. So I will add a comment here that says input validation, And we'll do that with a while loop. So what I'll do is between these parentheses, I will put a Boolean expression and you should remember Boolean expressions from when we learned about if statements in the previous course. So a Boolean expression evaluates to true or false. If the Boolean expression between these parentheses evaluates to true, then we're going to go into the body of the while loop and do whatever it says. And then we're going to come back again and see if it's still true. And if it is, then we'll go into the body of the loop again and so on and we will keep doing the body of the loop until the Boolean expression we include here evaluates to false at which point we'll drop out of the loop. So while loops are really useful because we don't have to know how many times we're going to loop. We don't know how many times the user is going to provide invalid input before they finally provide us with valid input. So it's great to use a while loop because we can keep looping until the player provide valid input. So what constitute invalid input for their while level? Well, there are actually two ranges of numbers that are invalid. Anything less than one, and anything greater than 110. So we'll say that while level is less than one, or level is greater than 110. If either one of those is true, because that's how or works, is one or both of them is true means that the total Boolean expression is true. So if level is less than one, this will be true, and if level is greater than 110, this will be true. It's impossible to have both, right? You can't think of a number that's both less than one and greater than 110. But if this Boolean expression evaluates to false, it means that level is in fact between 1 and 110 inclusive. So this might feel strange to you, the way we're sort of talking about what's invalid instead of what's valid but really the bottom line is, we need to loop here while their input is invalid, so that's what we're checking for. Once we get valid input, we can move on and print the level. If they've provided invalid input, we should print an error message and reprompt. And you can be as mean or as nice as you want with your error message. I'll be reasonably nice. Level must be between 1 and 110. And then I will read in their level again. I need to make sure I don't actually redeclare the level of variable, I just want to use it here so I don't want to declare it again, I just want to use it again. Now I can Ctrl+F5, and if I say 1, that's great because it's a valid input, and I can say 110 and that's a valid input too. If however I say -1 for example, it says the level has to be between 1 and 110. And I'm going to add a blank line here in a minute because it will look nicer that way. But I can be a bad input provider as many times as I want and the while loop is just going to keep going and I might decide to go topside instead and still it's going to keep looping until I enter a level that is valid between 1 and 110. So I'm going to add that blank line I said I'd add. And it's always a good idea to retry the code and so I'll say 0 and that's bad, so I'll say 1 and I'm all done. With while loops, it's possible for us to end up in something called an infinite loop. And you should go do an in video quiz about that. So of course, we can't actually loop infinitely in our programs because at some point the sun will burn out or will rage-throw our computer against the wall or something. But the big idea is in infinite loop is a loops that loops far more many times than you had intended when you wrote that particular loop. There's actually and acronym that we can use to help us avoid infinite loops and other problems in our while loops and you should take an in video quiz to guess that. And the correct answer is, ITM, means initialize, test and modify. And we should do those three things properly to implement our while loops correctly. So let's go back to the code and look at that a little bit. Not initializing level, so violating the i part of the acronym is sort of unreasonable in this scenario. You wouldn't really expect anyone to say this. int level and then just forgetting to do this piece. That is pretty unlikely and it won't actually lead to an infinite loop. And in fact the compiler won't let us do it. So we will in fact have to give level a value before we can use it here. So let's restore our code to its former beauty. And of course we can compile again. The next thing, the t in that acronym, then test. What if we got confused about or verses and we did this instead? So here is the problem. When I run my code, I might provide an invalid input and it doesn't work anymore. Because when I enter -1, this piece is true but -1 is not greater than 110 so the whole Boolean expression evaluates to false and we just skip the loop entirely. So that also didn't lead to an infinite loop but it did lead to misbehavior from the loop. The last part of the acronym was modify, so if in fact you forgot to modify the loop control variable, watch what happens. I'll enter 0, and then I have an infinite loop with no possibility of actually providing more input. And the reason for that is this Boolean expression was true, so I went into the loop and I didn't change level, so when I come back here again it's true again of course so I do the loop body and continue and continues and do that. The input validation example. It's pretty rare to forget to modify the loop control variable because you're re-prompting for the input that you're trying to validate. But there are certainly lots of other situations where forgetting about i, t, or m, or getting them wrong will actually lead to while loops that don't behave the way you hope they will. To recap, in this lecture we learned how to use while loops when we don't know how many times we're going to need to iterate. And we also learned the danger of infinite loops in while loops and how we can help avoid improper while loop behavior by remembering to initialize, test, and modify correctly.