Last time we learned how to use while loops to execute a loop body an indeterminate number of times while some condition was true. This time we'll learn how to do the same thing with a do-while loop instead. We'll start with our code from last time, but I've changed this comment to talk about do-while loops. Let's convert this code to use a do-while loop instead of a while loop. I'll start here and I'll say do, and then I'll put the body of the loop, and then I'll put the condition that we check to see if we're going to keep looping or not. And that condition is exactly the condition we used before to decide whether or not we're going to loop. I'm going to make Visual Studio indent it the way I want it indented, and now we can continue with our work. The key difference between a do-while loop and a while loop is that for a while loop, we know the body of the loop executes zero or more time. For a do-while loop, the body of the loop always executes at least once, because we do this work before we get to checking the Boolean expression to see whether we should keep looping or not. So that is the key difference between the two is that the loop body for a do-while loop always executes at least once. We know we're going to prompt for the score, and we also know that we're going to need to print an error message if they've entered an invalid score. And this is where it gets a little more complicated than our while loop body because we need to say we're going to print an error message, As appropriate. Because we don't want to print an error message every time the user enters the score here. We only want to print an error message if they entered an invalid score. So we need to include an if statement with the same Boolean expression that we used to decide whether or not we're going to keep looping. And then within that if statement, we do this like we did before. I'm going to now delete all the stuff that I kept around so I could steal from it. And let's run it. From the user's perspective, this works exactly the same way. If I enter a valid score right away, it ends. If I enter an invalid score followed by a valid score, it ends. And if I don't understand the prompt or I'm malicious and I do a number of invalid scores, it keeps looping until I enter a valid score. From the user perspective, this looks just like a while loop implementation. You might be wondering, how do you decide between the two then? And a lot of people, for the input validation problem, will say you should use a do-while loop. Because you know you're going to have to prompt for and get their input the first time, so you know you need the body of the loop to execute at least once. We didn't have the body of our while loop execute at least once. But we had this code that I've highlighted here appearing twice. Once before the while loop and once in the while loop if we needed to reget a test score from the user. So that's a win for the do-while loop is that we haven't duplicated these two lines of code. On the other hand, we have to include this if statement inside the do-while loop to print the error message only as appropriate. Whereas, in the while loop, we didn't need an if statement because we knew we were in the body of the while loop because we had incorrect input. For me personally, despite the fact that I know I need to prompt for and get the input once, I prefer using a while loop for input validation because it seems less complicated to me. We don't need both a loop end selection and if statement. We only need a loop, even though it requires some extra duplicated code. And that's how we can use a do-while loop. To recap, in this lecture, we learned another way to execute a loop body an indeterminate number of times while some condition is true. And that way was the do-while loop. The big difference between the two is that for a do-while loop, the loop body always executes at least once, while for a while loop, the loop body can execute zero times. Which one you decide to use is essentially personal preference. I tend to use the while loop, but that's up to you.