Nested Loops
We are back to our for loop to sample out how to create a loop inside of a loop(nested loop) using this new knowledge we will create a function that outputs a duplication chart.
Video Transcript
00:00We are returning to loops and returning to our for loop to see how could we actually create a loop inside of a loop?
00:08To do that what I'm going to do is I'm just create going to start off with a new loop our
00:13Basic for loop that we already know
00:15I'm setting its first I to be set to zero and actually going to set it to one this time around because I want
00:21To create a duplication chart from one to ten
00:24So I will as long as I smaller or equal to ten because you're on a run
00:28We want to run through the numbers one through ten and as long as I is
00:35I grows by one each time
00:38So far so good, and we've seen how to do that this part right the next step is we want to actually move on to the
00:45To run two different loops at the same time and to do that
00:48I could create another for loop right inside of my first loop and
00:52Same deal around here. I want to do the exact same thing
00:55I want to make sure my J is smaller or equal to ten and that I make my J
01:00Step one step further each time it runs so we have our two loops one inside of each other already running and just to make
01:08sure
01:08We understand what's going on here our for loop our eye in position one starts our for loop starts
01:15And it's in position one
01:17When it's in position one it meets a new for loop, and it's a for loop J
01:22So now our eye remains at one as this for loop runs from one through ten
01:29When that is completed only then it goes back to the top of our initial for loop and upgrades our eye to be two
01:35And now when it's two again it goes inside and again. It has another loop waiting for it
01:40So in a way this loop is being is actually being called a hundred times right because it's ten times
01:47at a time
01:49Well this loop is being called only ten times
01:52All right, so it's important to know because we're putting a loop inside of a loop the secondary loop the loop inside is actually being called
01:59J times I times if we would put another for loop inside it would be whatever we said that times and
02:07Basically, we're creating when we're talking about something like that in programming that's called a complexity of our code
02:13so the complexity of this code is actually
02:17Is actually x squared?
02:19It's x squared because we're we're going inside of a loop twice if we had three loops it would be x
02:27power three
02:29All right, but let's see and let's see how we actually can leverage this
02:33Unique feature in programming this really immensely powerful feature all right so first of all I want to create a new
02:40variable which I'm going to call output, and I'm going to set it to be a string and initialize it with a value of
02:46Nothing although it's not really important because we're going to have to initialize it again inside of our for loop
02:52Because in our for loop what I want to do is I want to output the math chart, which is one you know I want to run
02:58One through ten in every line, so I'm going to
03:02Reset my output
03:04To be equal nothing an empty string and before I even started the inter inside the inner loop
03:11Now when I'm inside the inner loop. I want to add to my output whatever it has
03:16I want to add to it some value
03:18And I want to make sure that it's a string so I'm going to set it to be I'm going to put your a
03:22String value to kind of force the number to turn into a string, and I'm going to add to it J times I
03:29Okay, so J times I is going to give us in the beginning when the value is one
03:33It's going to do one times when I is one is going to it's going to run through J
03:37Ten times and it's going to take I times J
03:40Which is one two three four five six seven eight nine ten so it's going to do that ten times
03:45Now if we just even just trace our output before we continue
03:50We're going to see that we're getting close to what we want, so this is starting to look good
03:54But it's a little bit messy so to clean up that mess what I want to do is
03:58I just want to go inside of this empty strings that I added here
04:02And I just want to add a slash backslash T
04:06And what would that do that what that will do is actually add a tab in between the copy that is entered inside
04:13enabling us to see it all in a nice grid and
04:17Now if we look at our math grid
04:18We're going to see that we could see for example five times five is 25
04:24Nine times nine is 81 ten times ten is a hundred eight times five is 40 and so on and we have a really lovely chart
04:34Now if we take this lovely chart, and we decide let's say we want to we want to extract it and turn it into a function
04:41So I could just grab this whole thing, and I'm going to create a private function
04:46I'm going to call it create chart
04:49create math chart
04:52And I'm going to paste inside all of our output now nothing's special about what we did right now
04:59But that will enable us to add properties
05:01Let's say we want to tell our chart from what value to what value we want our number to duplicate
05:08All right, so let's say we wanted to go from instead of from 1 to 10. Let's say we wanted to go from
05:1420 from let's say we wanted to go from 10 to 20 and we could start we could set here the start
05:21We're going to call it. I start
05:25Which is an integer and?
05:28We could give it a default value of 1 because that's the default we would want probably and I end
05:34Which would be an integer as well, and we want to give it a default value of 10
05:38Although we're going to override that default immediately, so I start we could override our I
05:44Equals 1 and our J equals 1 and for our I end we could override our I
05:52Equals or equals smaller or equals to 10 or J equals or smaller to 10
05:59By just doing this small operation now our chart is going to run from 10 to 20
06:04Enabling us to see much more clearly
06:09Our results now the only problem is is that we don't see the actual initial values
06:14And that is because we're not outputting our initial values
06:18But if we would imagine up here if we would imagine in the top of our chart
06:23What value we would have here we would have here 10 and 10 10 times 10 equals 100 and in the edge
06:30We have 20 times 20 on the bottom so 20 times 20 equals 400
06:36Now we could have added into our output that logic and to do that
06:41We would probably want to do is we would want to add a little bit more into our output such for
06:49example instead of
06:51Before we even start our before even start this for loop we could create one more for loop
06:57Right and that for loop
07:00Let's just I'm going to reuse I and we're going to see a problem when I do that which is perfect
07:06So I'm actually just going to grab everything here
07:10Because I want I have to run through that loop one time to create our first output
07:15because I want our first output to just be I and
07:23So our first output. I just want to put the value of I inside there
07:29Perfect
07:30So I'm first putting the set setting that value into the eye and then I'm just going to trace it out
07:35And I'm going to trace our output and we're resetting our output in a second anyways
07:40What would that do that would innate would give us one line and we're going to get a little error
07:43And we're going to look into that in a little second first of all we see that we are getting closer
07:49Right we have your only one problem is that we're starting off with a null and the reason is because there's no initial value
07:56Right so this time around we kind of have to give it an initial value of nothing
08:00Because we can't add into nothing something because it would add the null first
08:06So let's see if we're getting warmer
08:09And now we could see that we're getting warmer. We're getting the 10 through 11 to 20 which we want that
08:15Now the only problem that we have in this operation right here is we're still getting an error and what is that error?
08:21It's giving us a warning telling us that we're we have a duplicate variable definition
08:25That's true because we're defining I in the in line 13, and we're defining I in line 18 now
08:31We could reuse our I we don't have to redefine it
08:34So I could just delete that definition and just set that value I and get rid of that
08:40Redefinition by doing that I got rid of that warning message
08:44So we're getting warmer now the next step that we'd want to do is we'd want to add also a
08:50Chart from the left side right and from the left side
08:53What would we want to add first of all we'd want to add probably?
08:57We want to start off our output first of all with the tab
09:01Okay, because we're going to push everything a little bit
09:04So if I add a tab for the first one then only our type item is going to be pushed one item
09:08Forward which is good now. We want to start pushing all the rest
09:11To push all the rest we want to start our output with a specific value
09:15What value do we want to start with we want to start with the value of J?
09:21All right, and the value of J when it starts would be our I
09:26start and our I start
09:29well in
09:31Our scenario let me think one second actually I'm confusing myself
09:36Well actually we want to set we want to use for our output
09:39We want to use our J because actually our horizontal line is actually our J line when we're running inside of our loop
09:47right, so if I just set it to be our eye and
09:52This is might be a little bit confusing
09:53I'm using eye here for a different usage than it actually is using because our eye is using to create a line
10:00While in our double loop we're using J to create the line all right so in theory
10:06We probably would be better off to call it J if we wanted to be wanted it to be more legible
10:10But I think we understand what's going on
10:13So if I run the application one more time. I should now be getting the exact
10:18Items that I should that I wanted. Let me just make this a little bit bigger
10:22And now only problem we have is because our tab
10:25We're having a little problem because our tab is not aligning correctly
10:30Right so first of all we're having here an issue
10:33With our initial tab that it seems like we have two tabs in our in our
10:38Let's go back there
10:40First of all we're having a little issue here because in our output right here. We have tab and then tab again, right and that's
10:49Not really working for us
10:51So what we want to do is maybe we're going to take the second tab
10:53And we're going to add it after the number that way we could ignore the last tab
10:58Making sure that we're starting with the tab
11:01Then we're adding a value, and then we're adding another tab. Let's see if that will solve our problem and there we go
11:10So we solved our first problem
11:12Let's if we have any other problem everything seems to be aligning right now
11:16Alright, so if I take for example if we look at our application one more time
11:20And let's say we want to see how much is 20. Let's say how much is 18 times
11:2617 and that would be 300 and 6
11:30Alright, and we could then create as many different variations to our chart as we want as long as we start with a
11:37low value and a top value and
11:39that
11:41basically covers our
11:43discussion about
11:46Looping inside of a loop, so I'm just going to run it
11:48Let's say with 10 and 15 just to see a smaller chart and our smaller chart looks perfect
