In the previous couple of lessons, we've looked at for loops and nested for loops. And with for loops, wWhen we get to the loop as we're executing our code, we know how many times the loop will iterate. This time, we will look at while loops, which are loops we use when we don't know how many times we're going to iterate when we get to the loop. Let's go see how while loops work in C Sharp. Our starting code doesn't have any while loops at all. All I do is prompt for and get a health value between 0 and 100, and then I print out that health value. If I run this code, you can see I can enter 0 and it's fine, I can enter 100 and it's fine. The problem with this code is that I can enter an invalid value as well. I could say I'm very healthy, I have 110 health, and it still accepts that and moves on in the code. And there are lots of times when we're going to have the player enter some information for us, and we want to validate it. Of course, we're not going to have the player enter their health, right? The game will keep track of the player's health for them, but this is a good example of a value that is supposed to be within a particular range. So what we want to do now is we want to use a while loop to do something called input validation. Which means validate the input, make sure it's valid before we move on through the rest of our program. We'll use a wire loop to do that. And the way this works is we'll put a bullying expression here between the open and closed parentheses. And if that bullying expression is true will go into the body of the wire loop and execute whatever code is here, and then we'll come back and check that bullying expression again. And for as long as or while that bullying expression is true, we'll keep executing the body of the loop. And the only way we'll get past the loop is when the bullying expression evaluates to false. So what we need to do here is we need to write a bullying expression for when the input from the user is invalid. And that might feel counterintuitive, you might be thinking, well, can't I just check if it's valid. but we really need to check if it's invalid here, so that we can go in and get a new input from the user. So what does it mean for their input to be invalid? There are two ways that the health value is invalid. Either health is less than 0, or health is greater than 100, and that leads us to our bullying expression. And of course, we learned about bullying expressions back in the selection module, so we know how to write these. While health is less than 0 or health 1s greater than 100. And while that's true, we need to get a new input from the user to try to keep getting a valid input. I'm going to add a blank line because it will look nice to have a blank line between the input and the error message I'm about to write, and you can be as nice as mean with your error message. I think I'll be nice and say, health needs to be between 0 and 100. I'm not mean, I'm not calling them names or anything. The next thing I need to do though is I've printed this error message to them, but now I need to get a new health input from them. I'll copy this code down here, But of course, I can't redecrare health. I already have a health value here in my main method, so I can't redecrare health, but of course I can reuse the health variable. Variables are great, we can keep changing their values as we run our code. So now what will happen is I'll ask for their health, and I'll read in the value. And then while they've given me an invalid value, I'll go down here, and I'll print that blank line and an error message. And I'll get a new input from them, and then I'll come back here. And I'll check, and I'll see if this is still true. Ad if it's still true, I'll go back into the body of the loop again, and keep doing that until this evaluates to false. When this evaluates to false, we know that health is between 0 and 100 inclusive. So we can go past the while loop, and come down here to print out the health. Let's run the code and make sure it still works for valid input, so 0 is valid and 100 is valid. 101 is not valid, so we go into the body of the while loop. And the nice thing about while loops is they could keep giving us invalid inputs and even down on the negative side. And we're just going to keep looping until they give us a valid value. And when they finally give us a valid value, we exit the while loop and move on through the rest of our code. I will point out that we are making an assumption here that the user is actually going to enter an integer. So if we run the code and ask your health, and they say bob is my health, our code blows up with something called an exception. We will learn before we're done with all the courses in the specialization how to handle exceptions. But we don't know how to do that yet, so we're going to just assume that they're going to enter integers. We'll make sure they enter an integer that's in a valid range before we move on, but we're only going to deal with integer input from the user. Although it's possible to write for loops that don't work properly, it's much more common for people to mess up their while loops so they don't behave the way we expect them to. One way to help with that is to remember the acronym ITM, which stands for initialize, test, and modify. Let's go back to the code and see how ITM can help us write while loops that behave the way we expect them to. So let's talk about the I part of ITM. We need to make sure that health is initialized before we actually get to the while loop for this bullying expression to have a meaningful evaluation. In this particular example, it's unreasonable to assume somebody did this that they didn't include this code, but they did in fact declare a health variable up here. And the compiler won't even let us do it this way, we get an error message here, use of unassigned local variable health. So we do have to make sure that health is initialized before we get here. This is obviously a simple example, but we could have a more complicated method, where we actually passed in health. And then the compiler wouldn't know that health wasn't initialized before we get here, so we could get bad behavior from our while loop. The next thing we should do is talk about test. So the test part is the bullying expression that you write for the while loop to decide whether we're going to execute the body of the while loop again or move on. So if we write our test incorrectly, let's say we got confused about the difference between and and or, and we wrote this as and instead, you should think about this bullying expression. Can this bullying expression ever evaluate to true? This bullying expression can only evaluate to true if you can think of a number that is both less than 0 and greater than 100. And you can't, right, it's impossible. So we're never going to go into the body of this loop even if the user enters an invalid input. So this test is incorrect, it doesn't lead to an infinite loop, although there are ways to incorrectly write you're bullying expressions to get an infinite loop. This isn't one of them, but it does lead to bad behavior from our loop,not our intended behavior, so I'll fix the test. The last thing that we have to do is we need to make sure in the body of the while loop that we modify the variable that's used in our bullying expression. And sometimes we'll have multiple variables included in our bullying expression for our while loop, and we have to modify at least one of them. Let's see the importance of that. And this truly will be an infinite loop because if I run the code now, and I enter an invalid value, I'm just stuck in this infinite loop forever and ever. So what's happening here? I entered a -1, so this bullying expression was true, and then I did these things, but health is still -1. So when I come back here again that bullying expression evaluates to true again. No matter what variables you're using for your bullying expression, if you don't change any of them in the body of the while loop. If you got into the body of the while loop you'll always be going into the body of the loop. So that's an example of making sure that we do our modify correctly, so that we don't get stuck in an infinite loop. And I'll test the code one more time. And it's working fine. So that's how we can use a while loop to do input validation in our code. To recap, in this lecture, you learned how to use while loops to iterate an unknown number of times as we execute our code. You also learned about the danger of infinite loops and other while loop misbehavior. And you learned how we can use the ITM, initialize, test, and modify acronym, to help make sure that we implement our while loops correctly.