Last time, we finished talking about the three different kinds of loops that we commonly use in C++. That's a little lie because we'll learn about another variant of the for-loop in the next lesson, but it's good enough for now. In this lecture, we'll learn how to use lots of what we've learned in this lesson to solve a particular problem. Here's the problem we're going to solve. We need to build a box with asterisks all around the edges of the box, using a user-specified width and height. The constraints on this problem are that both the width and height needs to be between 3 and 20 inclusive. Let's go solve that problem. This is a really interesting problem because it lets us use while loops and regular for-loops and nested for-loops. It's a great problem, the idea for this problem comes from Beginning C, a book by Ivor Horton, and I know I'm teaching you C++ here, but this is a great book to use if you want to learn how to program in C, and I took the idea for this problem from that book. My starting code, I have variables for the width and height of the box. Then I have while loops to do input validation to make sure I get a box width that's between 3 and 20 inclusive, and a box height that's between 3 and 20 inclusive. I will freely admit to you that I wrote my input validation for the width and tested it, and then I copied this code and I pasted it down here and I changed width to height. As a programmer, that made me feel really sad, that's a horrible thing to do. Copying and pasting is never the right idea if you need the same code. What I really wanted was to write a function that would get me a value that was between a lower bound and an upper bound. Unfortunately, we haven't really talked about writing our own functions yet, so we're not ready to do that yet. I did it the same way that you would probably have done it, but I found it very unsatisfying to do it that way. Once we know about functions, you'd absolutely write your own function. How do we print out this box? Well, the first thing we're going to need to do is we're going to need to print out the top row of the box, which is really just a solid line of asterisks, that's the user specified width. We know how to do something a particular number of times. That's definitely a for-loop. We can start i at 1, and we'll go for the full width, so i less than or equal to width. We'll increment. The only thing we need to do in here is print out a single asterisk. Once the loop is done, we'll want to print out a new line so that we can move off that top row. Let's test this. It doesn't matter what I enter for height, but if I enter a reasonable width, you'll see I print it five asterisks as the top row. Again, feeling like a bad programmer, I'm going to print the bottom row by copying and pasting that top row stuff, and I can test that. If I enter some valid width and some valid height, you'll see I have the top and bottom rows of the box. The most interesting part of this problem is print the middle of the box. We need to think about how many times we're going to do this. First of all, how many rows are there in the middle because that's a good for-loop for us. I'm going to actually start i at the second row. How many total rows do we print in the middle? We print height minus 2. Height minus the top row, minus the bottom row. We can specify our condition here in a variety of different ways, but I'm thinking of i in this loop as keeping track of which row we're printing. We start by printing row 2, and then we're going to print all the way up to row height minus 1. I'm going to say i less than or equal to height minus 1. Now, I know that implies we always need to use a less than or equal to here. I could just as easily have said i less than height, but I find this more readable, thinking of i as the actual row number I'm printing here. I am going to increment i. Now we need to think of what does a particular row look like? A particular row looks like an asterisk, and then a bunch of spaces, and then an asterisk, and then a new line. Let's do the asterisks at the end first. Of course I'll do the new line as well. Let's go ahead and do this. I know it's not going to be correct, but let's watch all those rows print without the spaces in the middle. I'll run the code. I'll say the width is 5, and I'll say seven rows. There you go. I have the top row, and I have the five middle rows, even though we're just doing the asterisks, and then I have the bottom row. Now we need to add the spaces in-between. I'll need another for-loop for this because I don't know at compile time how many spaces I need. But I will say that I'll think of this the same way as I did the outer loop. I'll think of j as being the column I'm printing in. I know that I'll start j at 2, and I'll print while j is less than or equal to width minus 1. I'll increment j. Here, what I'm outputting is a space. Now I'll test my code again. I'll say 6 by 6, and there's my nice 6 by 6 box. I will point out, of course, I was printing out strings up here, but as I'm printing out characters, I can print out character literals as well. It doesn't have to be strings when I'm printing out a single character, but I'll show you we get exactly the same output if we print out asterisk characters and space characters. Let's do a big box, 20 by 20. You can pause the video and check to make sure, but I promise you that's 20 by 20. I'll show you a 3 by 3 box, our smallest possible box. As I said, this is a really interesting problem to solve. We have a regular for-loops, we have nested for-loops, and we have while loops. To recap, in this lecture, you learned how to use while loops, and for-loops, and a nested for-loop to solve an interesting problem. I know I mentioned Ivor Horton's C book, but Horton also has an awesome Beginning C++17 book, if you'd like to dig deeper into C++.