The title of this video is a nested loop, Part 1. Before I start talking about nested loops, I wanted to point out a few things with this x, y, z point loop structure. Pose a question, would happen if y was also defined in terms of i, what would that produce? Right now, when I run this, the only value that's changing here is x, because I'm setting it equal to i. So I've got to eliminate my multiplier here just to simplify this a little bit. The only thing that's changing in the point value is x. So as x increases, the points march along the x-axis, and maybe changing this back to that value structure. The scene in the coordinate values rather than the index values might be helpful. I'm going to turn that back on here in the loop and then turn this off in the lower loop down here. So run that again. We can see the only thing that's increasing is our x value by one each time through the loop. What would happen if we set i equal to or y equal to i also? What would that produce? You can run it. We'll see it produces a diagonal line of points. That's makes sense if we look at the coordinates, because each time x increases by one, y also increases by one. So the value 1, 1 is right here in the coordinate space, 2, 2 is here, 3, 3 is here, 4, 4, etc. But what happens if I, let's say, added a multiplier of two to the i value just for y. So we said i times 2. What's that going to do to that line? Well, it's going to pull it away from the 45 closer to the y-axis, because my y value has increased relative to the x value. So it produces a line of points at a different angle. Let's go back to zero, set y back to zero for a second, from this again, and then ask, what if we wanted to create a grid or matrices points, not just a line of points, how would we do that? Well, in order to do that, we're going to have to create a nested loop. So the value for y is going to have to be determined in another loop that's nested within the i loop. To do that, I can take my i loop and copy it, just the top part, and then paste that. But I have to use a different looping variable. I can't use i again, that would be confusing, run into a bunch of problems within a loop. So I'm going to change this to j. Then I also want to make sure that all the stuff that I had in the initial loop is indented to be within the inner loop. So I can select all of that. If I select all that at once, and hit Tab, that's going to indent that. Actually, another short key for indenting a block is Shift tab, that will allow you to unindent, Tab allows you to indent. So if I'm defining x in terms of i, then I'm going to be defining y in terms of j. Now I just set y equal to j. Let's run this to see what it does. Actually, I'm going to go back. Before I do that, we'll switch, let's go back to our list structure, because that's going to show me the order in which my points are generated, which is going to be telling in certain ways. So I'm still saving my points in the list and then I'm going to loop through that point list and I'm going to label them with the index numbers. Let's run this. Sure, we don't have anything at our scene. No, that's undone. Okay. So the generation of the points and when the TexDot is added, is in the same exact order. If you look closely as the points get generated, it's descriptive of how a nested loop works. Also these points are also the numbering scheme where the point is also descriptive of how that loop works. So let's watch it again. If you notice, it starts down at my world coordinate center, that's my first, is 000, starts there. Then where does it go? It goes up. So it goes up along the y-axis, and then it gets to nine, and then it drops down and does this next point down here along the x-axis. Then it goes up, and then it drops down again. What that is, is a description of how the nested loop works. If we go back to our code, what happens is our first time through, i is zero. The first thing it does is it goes into the inner loop, because that's the first line of my code. The first time through j is zero. So my first values for x and y are zero and zero, and z is always zero. So my first point is 0, 0, 0, and then that's saved within the list. Then it doesn't jump back out to the i loop yet because it's got to run through everything within the j loop, because that's the loop that it's currently going through. So the next value for j is one. So y changes to one. What that means is that it produces this point. If we run this again, it might be actually more helpful to see it as the coordinates rather than the index values. So let's run it as that. Y is one because j is one. Next time through, is it out yet of the j loop? No, it's not. Next time through is j is two. That's what that is right there. It keeps doing that, it goes all the way through the j loop until we get to nine, j is nine. The next time through j becomes 10, 10 is equal to 10 here, where n it throws it out of the loop. Then where does it go? Well, it goes back up to the i loop. What's the next value for i? Is one. So x becomes one, and then it goes to the j loop again. J is reset to zero. That's why that's the next point. I becomes one, j becomes zero, and then we run through the j loop again, 1, 2, 3, 4, so on, going all the way back up to nine. Then it does the same thing again, jumps back out to the i loop, which becomes two, and then jumps into the j loop, which becomes one. It's very good practice, just play around with this structure a bit. You can even do things like, if you wanted to add multipliers here to understand how this nested loop structure works. But this is a really handy to actually have a visual description of it. Then you can also switch this back to the index values, which demonstrates the order of that point list in space. So rather than using the coordinates, I can use the index values. That's going to show me that order beginning down here, and just counting in sequence and then dropping back down here, going back up. So the matrices, it's going to be a handy way for generating geometric systems, but it's also a very good description of a nested loop structure.