In the previous lectures, we've learned about the common forms of loops that we use in our C programs. In this lecture, we'll solve a particular problem using much of what we've learned about loops in C. Our problem is, that we need to print out a box that is composed of asterisks using user provided width and height for the box. The constraints on this problem are that both the width and the height need to range from 3-20. We picked three as the minimum, so that we can actually have one space or at least one space in the interior of our box. We picked 20 as an arbitrary upper limit, because that's how many asterisks we're willing to print out in a row or a column of the box. So let's go see how we can use the stuff we've learned about iteration to solve this particular problem. As you can see from the comment, this example will demonstrate using while loops, for loops, in nested for loops. The idea for this problem comes from beginning C, a book by Ivo Horton, and it's a great book. I use it for my classes at UCCS. So if you want an additional resource for learning to program in C, that's a good book to take a look at. I'm going to start by declaring variables for the width and the height of the box that we're going to print out. Remember we're going to get both width and height from the user. So we're going to have to do input validation on that user input. I'm going to type all that stuff in and then we'll come back. Okay. Here we go. I have two input validation loops that should look very familiar to you. I asked for the width, telling the user the valid range for the width. Then I'll do a while loop to make sure they've entered a valid width. I do the same thing for height. Now I will freely admit to you that I wrote this code and then I copied and pasted it down below to do the height. That is unsatisfying for me as a programmer, I hate to copy and paste. What we really would want to do, is write a function that gets a valid value between a lower bound and an upper bound. But we don't quite know how to write our own functions yet. Don't worry, we'll get there before the end of the course. So here I had to copy and paste because that's how you would have done it. But later in the course, we'll learn how to write our own functions. Let's go ahead and test this piece. So I'll enter invalid numbers to make sure everything loops properly and then I'll enter a valid one. I'll enter invalid heights as well. Then I'll enter a valid one. So now we have a valid width and a valid height. Now we need to print out the box. The way this needs to work is, the top of the box is a solid row of asterisks, and the bottom of the box is a solid row of asterisks. But the interior of the box is just an asterisk on each end. Let's print the top row first. You should have guessed that we'll use a for loop to do this, because we don't know at compile time how wide the box is going to be. So we'll start i at one, and will go i less than or equal to width. We will increment i each time. The only thing we'll do inside this for loop is, we will print an asterisk. I know I'm going to want a new line, after I've printed this entire row of asterisks. When I run it, I'll enter five for the width. The height will matter in this case. You'll see that I get five asterisks. I want one more new line before I start printing the whole box. Now, let's actually print the bottom row as well. I'm going to copy and paste again. But again, I will say that as a real programmer, what I really want to do is call a function that prints a row of a certain number of asterisks. But we can't do that yet. So we'll just do this instead. This is the bottom row, and it's going to be the full width as well. As you can see, I've got at the top row and the bottom row. So all I have left, is to print the interior of the box. So the first question is, how many rows are there in the interior? The answer is, that there are height minus two rows. We can build a for-loop that handles that. So this is the outer loop. We can do it in a variety of ways. I could start i at one and go up to height minus two. Or I could think of this in terms of rows that I'm printing, and so I could start i at the second row and run i up to height minus one. That's why I'm checking for less than rather than less than or equal to here. Then increment i. This outer loop prints the rows of the interior of the box. Now we're inside this outer loop. So we're printing a particular row. How do we print a particular row? We know we start with an asterisk. This will be the asterisk on the left-hand side of the row. We know we end with an asterisk. We know when we're done with the row, we're going to need to print a new line. But what about all the spaces inside the row? Well, we can use the same logic we just used for the outer loop, where we knew we needed to print height minus two rows to say well, inside the box for the spaces, we know we need to print two up to width minus one spaces. So in here, we'll printf in individual space. But we'll do that width minus two times. Let's see if we got these right. First, I'll do three by three, the smallest box possible. As you can see that worked fine. Now I'll do five by seven, because our box doesn't have to be square. As you can see, if you want to count those up, that is a five by seven box. Of course, I can do 20 by 20 if I want a big huge box, and feel free to pause the video and count those if you want to. But trust me, that's a 20 by 20 box. For these for-loops, this is a really good example because it shows we don't have to start the loop control variable at one. We can start it at anything. We don't have to use less than or equals here, we can use less than or any Boolean expression right here that makes sense for us. We have kept incrementing our loop control variables, because we just want to increase by one each time in the problems that we've solved so far, but you can actually increase i by two, or three, or 10, or whatever you want. There's no magical rule about incrementing, that just happens to be the thing that we've been doing so far. In this overall problem is really nice, because we have two while loops, for input validation. We have a regular for loop for the top row, and a regular for loop for the bottom row, and then nested for loops for printing the interior of the box. So this problem actually ties together everything we've learned about iteration, except for do-while loops and we didn't need any of those for this problem solution. To recap, in this lecture, we use what we've learned about iteration in the previous lectures, to solve an interesting problem.