So we've made it to the end of Module ten, about iteration, what some people call looping. So, let's look at the module learning objectives. So last time, we finished our discussion about for and for each loops. The loops that we use when we know how many times we're going to loop when we get to the loop as the program executes. This time, we're going to look at while loops. Which are loops that we use when we don't know how many times we're going to iterate. So, just to see if you've paid attention to what I said over the last 30 seconds, here's an in-lecture quiz for you. So, I've started a program that we're going to start looking at. Before I do, I want to talk about sort of a standard problem that we solve using while loops. So, input validation means making sure that whoever is using our software has entered valid data when we ask for it. And the reason we need a while loop, here rather than a for, or for each loop, or even just an if statement is because we don't know how stupid the user is. They could enter invalid inputs 10 times before they finally enter a valid one. And so a while loop is a great choice for doing input validation. So, I'll open up our project. I've already added a little bit of code. So this is a console application, darned no graphics or explosions, or anything like that. So we have a menu here. And the user can enter J for Jump, C for Crouch, or Q for Quit. We have the standard prompt and reading in that we've done before. I will point out that the console read line gives us a string. And I'm calling the ToUpper Method on that string, so that we turn all the characters in the string to upper case. And hopefully, that's only one character. because we're going to do a char.Parse on it, and put it into this choice variable. So we know, when we get here, as long as the user doesn't do something super stupid, we're going to end up with an upper case single character. So I'm going to Ctrl+F5 to run this, just to show that I can do that. But I can also enter, you know, D and it's okay it gets to the end. So for this example, we're not actually going to do anything with their menu choice. We're just going to make sure that they've entered the valid one so that when we get to this point in the code where this cursor is we know that we have a valid input. So how do we validate input? Well, what we need to do is we need to make sure that, as long as the user has entered invalid input. That we can in fact give them an error message, give them the menu again and get a new input from them. And so, we're going to use a while loop for that. And I put the structure in first. Like that. So, what goes here is a Boolean expression that when it evaluates to true, we want to go into the loop body and do whatever happens in there. So, let's say, we know what the valid choices are. The valid choices are capital J, C, or Q. So, the Boolean expression here needs to evaluate to true when that, when the input is invalid. Which means that choice is not equal to J and choice is not equal to C, and choice is not equal to Q. So, if it's the case, that they haven't entered a J and they haven't entered a C, and they haven't entered a Q, then we know they've given us invalid input. And we need to get valid input from them. So, the first thing we'll do is print an error message. And you can be as nice or as mean with this error message as you want. I'll be reasonably nice and just say invalid input, must be J, C or Q. You can column name if you want to and so on. We're going to add a couple of lines in the output to set the error message off from the menu and from where they entered their choice. And now I need to print the menu again and get their choice again. And I'm going to copy and paste, even though I hate doing that. Once we learn how to write our own methods, we'll see that this whole copy paste thing doesn't really need to be done anymore by us. And of course I have to not declare the choice variable again. So now, what will happen is, I'm going to print the menu and get their choice. And then I'm going to check to see if their choice is invalid with this Boolean expression. If it is, I'm going to print the err, error message. I'm going to give them the menu and ask for their choice again. And then when I we, When I reach this closing curly brace, I'm going to loop back to the top of the while loop and I'm going to evaluate this Boolean expression again. And if it's true again, because the user is an idiot, then I'm going to go into the loop again. So I will keep looping through this while loop until they've entered a valid input, at which point I'll get here and I'll know it's valid. So let's Ctrl+F5. I'll enter some invalid inputs, D, D, D. But then when I finally enter a valid one J, it gets past the while loop, because this Boolean expression here now evaluates to false. And so it gets past the while loop and I get to the end of my program. So, before we continue, you should go do an in-lecture quiz, that's, talks about or asks you about infinite loops. And now you should do another in-lecture quiz that asks you about an acronym that can help us solve the infinite loop problem. Great. Now, infinite loops are not really infinite. Right? At some point the sun will explode, or our computer will burn up. Or, or we'll get so angry that we'll you know, throw it against the wall or something. But they're really called infinite loops because they don't terminate when they really should. You know, with sort of normal behavior. So, let's show how initialize, test, and modify both, bold, all three matter when avoiding infinite loops. It actually turns out that in this particular case, the initializing doesn't lead to an infinite loop, it just leads to nasty behavior. So, let's say that I have in fact declared my choice variable and initialized it to some invalid choice at the beginning of the main method. And then I don't actually initialize it by getting input from the user. What happens when I, when I run, is that it immediately says that I'm a moron. And gives me the message again, which is really bad for users. Right? That's not the appropriate way to do it. That's kind of a poor example of how initialize leads to an infinite loop, because in fact it does not here. So just trust me that sometimes initialize is the problem. Let's actually look at test and modify, because those are way better examples here in this particular example that we're working through. So let's say we got this test wrong. Let's say we've gotten confused about and versus or. And so here we've put or's instead of ands. So we've messed up the test. The test is invalid for the loop. And this is going to lead us to an infinite loop. So I'll Ctrl+F5, and I will enter an invalid input. And I say, oh, darn, I should have read the menu and I enter j. And wow. I'm looping again. So, like any good programmer, I'll do exactly the same thing and expect a different result. That's the sign of madness. Except, in the context of software, it's not the sign of madness, because something is different. The state of the computer, the memory, something is in fact different. We're mad as programmers, but not because we try the same thing multiple times. Okay. So now I'm stuck. Right? Even though I'm entering valid inputs, I can't get out. So I'll use the red X. Here's what's happening. When I entered J, this part of the Boolean expression, J not equal to J, was in fact false. [INAUDIBLE] good. The problem is, then I got to this part. And J not equal to C is true. And the way that or's work. Right? Is that any of them can be true to make the whole thing true, which pushes us into the loop again. So that's how a test can, an in, an incorrect test in our Boolean expression for the while loop can lead to an infinite loop. Let's hit the M part of the acronym, so if we forgot to modify. So I'll, I'll comma this out. If we forgot to modify the loop control variable, let's watch what happens. I'll Ctrl+F5, and now I'll enter a bad one, D. And oh dear, loop, loop, loop, loop, loop forever until I reach quit. So what's happening there, I'm going to fix this before I forget. What's happening there is, if I got into the loop, this Boolen expression evaluated to true. That's the only way I got into the loop body was because this is true. So if I don't change choice inside the loop. When I get to the end of the loop here, and I come back around and check it again, it's true again, because nothing changed. I've never changed choice. So, I'm stuck in this infinite loop because I didn't modify the variable that I'm using in my test. Sometimes we have multiple variable in our test, but the concept still applies. At least one of those variables has to get changed inside the loop body, otherwise we're guaranteed to end up with an infinite loop. Because, if we got into the loop body the first time, we're going to get into it forever. Okay. So one last. That's, that's it for the code for today. Those are while loops, and input validation tends to be the most common place we use them. But there are other places we find to use them as well. So go do one more in-lecture quiz about actually testing while loops. Okay. So, when we test while loops, it makes sense to make it, to run it so that the loop body executes 0 times. And note, I did that. And may not have done that, but I meant to do that by entering a valid input right away. So then the while, the Boolean expression in the while loop evaluated to false when we got to it the first time so we just skip the loop body. You also should execute it once, which we saw numerous times where it enters D and the J and something like that. And then you should execute it multiple times. Try a few invalid input followed by a valid one. The reason for this is that the sort of most common errors that people find in the while loops that they write, occur because they got the test not quite right. And so when you go for a zero run, basically, you end up going into the loop instead. And so you modify your test. You might, trying it once, we'd certainly catch the you forgot to modify it thing and you end up with an infinite loop. So if you have a test case that runs that while loop body loop that goes into the while loop or body once, you'll catch that. And sometimes much more rarely. We have a few iterations of the loop and then the loop breaks. But, that is more rare, certainly, than the first two. So that's a way to actually test of, to do a good test, of code that has a while loop. So to recap for today, we actually covered how we can use a while loop to iterate an unknown number of times, because it's based on some condition rather than a count. It's not how many things are in the collection, it's how times is the user a moron. That's the end of module ten. So we're all done with iteration. Again, what a lot of people call looping. We're all done with iteration, and you now have all three control structures you need to solve all kinds of problems in your game development. Next time, we're going to move on to the next module, where we're going to start building, designing and building a customed class of our own.