In this video, we'll go over using nested loops.
Just like we nested if statements one inside the other, the same
technique can be used to nest loops.
We can nest loops as many levels deep as we need.
I'll be using just 2 levels in the examples I'll show you that
is one loop inside another.
Nested loops have many uses, but they're especially useful for
multi-dimensional structures, like 2D 3D arrays and 2D and 3D vectors.
When we talk about nested loops, it's common to refer to the loops as
the outer loop and the inner loop.
Let's see some examples.
Here's a pretty generic example of a nested loop.
In this case, we have a for loop with another for loop within it.
The outer loop is incrementing outer valve from 1 to 2 by 1.
The inner loop is incrementing inner vowel from 1 to 3 by 1.
Notice the output.
The integer to the left of the comma is the outer val.
And the integer to the right of the comma is the inner val.
Notice how inner val increments from 1 to 3 while outer val remains at 1.
Then outer val increments to 2, and we do it all over again.
You can think of this as the outer loop iterating 2 times and each time
the inner loop iterates 3 times.
That's why we say that the inner loop loops faster than the outer loop.
Let's see another example.
In this example, we can take advantage of a nested loop to display the
multiplication table from 1 to 10.
A nested loop is perfect since we want 1 x 1, 1 x 2,
1 x 3, all the way to 1 x 10.
Then we want to do 2 x 1, 2 x 2 and so forth.
So we want an outer loop iterating from 1 to 10 and an inner loop
also iterating from 1 to 10.
And the body of the inner loop simply multiplies the outer loops
variables x the inner loops variable.
Notice the output statement after the inner loop that
displays a line of dashes.
This executes each time the inner loop completes.
We'll execute this loop in the IDE in a minute so you can see it work.
Let's see an example of a nested loop using a 2D array.
In this example, we have a 2-dimensional array
of integers named grid.
Grid has 5 rows and 3 columns.
Suppose we want to assign 1000 to each element in the grid.
We can do this easily with nested for loops.
The outer loop iterates for rows 0 to 4 inclusive, and the
inner loop iterates the columns for each from 1 to 2 inclusive.
Then in the body of the loop we simply assign 1000
to grid sub-row sub-column.
Notice I'm using block statements for the body of these loops.
I didn't have to here, but it's good practice to do so since it
makes adding code later much easier.
Now that we've initialized the grid values to a 1000, we can use a similar
nested loop to display the values.
In this example, we're assuming that grid has been initialized
as in the previous slide.
At each iteration of the inner, loop we display the element at grid
sub-row, sub-column followed by space.
So we're displaying these elements across the screen.
When we complete the inner loop, we print a new line and then
execute the outer loop again.
You can see the pattern is the same as with initialization.
This is a very common pattern for working with
multi-dimensional structures.
Let's see how we can use nested loops with vectors.
In this final example, we declare a vector of vector of integer.
So we have a vector whose elements are vectors of integers, very
similar to a 2D array of integers with the advantage of vectors.
With a vector of vectors, we can allow each vector within the
vector 2D to be of different size.
In this case, we have 3 vectors of vectors.
And each of those vectors is a vector of integers.
I know it sounds complicated.
But if you look at the example, it should be pretty clear.
The first vector has 3 integers: 1 2 3.
The second vector has 4 integers: 10 20 30 40.
The third vector has 5 integers: 100 200 300 400 and 500.
If we wanted to display the contents of vector 2D, we could do it easily
with nested range-based for loops.
And if we use the auto keyword to allow the compiler to deduce
the type of each loop element, it makes it dead simple.
Notice the outer loop in this case, the compiler deduces vect
to be a vector of integers.
And in the inner loop, it reduces val to be an integer.
In the body of the inner loop, we simply display the
value followed by a space.
As we did in the previous example, we display a new line after we're done
displaying the inner vector values.
You can see how little code and how simple the code is.
You can see the output from this loop at the right on the screen.
In these examples I've used 4 loops within 4 loops, but you can use any
combination of loops within any other loop to achieve your desired behavior.
Let's head over to CodeLite and see some of these examples live.
Okay, so I'm in the CodeLite IDE.
I mean it's still in the section 9 workspace, and I'm in the
multiplication table project.
This is the piece of code that executes and displays a multiplication
table for up to 10 x 10.
So let's go over this really carefully because this is a
classic example of a nested loop.
Notice we've got the 2 for loops, 1 within the other.
And the outer loop is looping on the num1 variable.
The inner loop is looping on the num2 variable.
So we're going to start the outer loop.
And we're going to assign right here we're going to initialize num1 to 1.
And now we check is num1 less than or equal to 10, it is.
So we go into the body of the loop.
Well, the body of this loop is another loop.
And in this case, we're initializing num2 to 1 right here.
And we check is num2 less than or equal to 10, it is.
So we execute the body of the inner loop, which is this
output statement right here.
So what we do here is we display num1, which is 1.
We display the *, string right there.
We display num2 which is 1.
We display that equal sign right here.
And then we multiply num1 num2, that gives us 1.
And we display that value.
That's it.
We display a new line, and now we loop again.
So we come back up to the loop.
We do the increment.
So now num2 is 2.
We check is num2 less than or equal to 10, it is.
Execute the body of the loop again.
This time through we've got num1 is still 1 right here.
Num2 is now 2, and we display the output and so forth.
So we're going to do this until we get to 1 x 10 equals 10.
And num1 will still be 1.
At this point, num2 is 10.
Now we're going to increment num2 to 11.
We come over here, and we check is num2 less than
or equal to 10, it's not.
So we fail.
Now we terminate the inner loop.
We execute this statement right here so it's going to display
a bunch of dashed lines here.
And then remember, we're still inside the outer loops.
We have to go back out to the outer loop and do the increment.
So now num1 becomes 2, is 2 less than or equal to 10, yes.
We go back into the inner loop again.
So think of this as here's your outer loop, and it's looping.
And each time it loops, you're going to do the inner loop 10 times.
Then you're going to go back out, outer loop, and you're going to
do the inner loop again 10 times.
Okay, my art is not pretty, but hopefully, you will understand
what I'm talking about. All right, so
that's what's really going on here.
Let me clear this and run it so you can see this in action.
So there's the output.
Let me scroll up.
So you can see here 1 x 1 is 1, 1 x 2 is 2 and so forth.
Notice in this case, let me move this just over here so
you can see the variables.
Over here, this left 1 is num1.
And then you can see that num2 is looping faster.
That's why we say the inner loop loops faster than the outer loop.
We go through all 10 of them, then we display the dashed line, and then
we go back out to the outer loop.
Now num1 is 2 and then 3 and then all the way to the bottom
where we end up with the 10s.
And we end up with 10 x 10.
At this point, the outer loop fails.
And we're done.
Okay, so hopefully that makes sense.
And what we'll do next is we'll do an example that's a
little bit more complicated.
We'll actually grab some user input, we'll grab some numbers
put them in a vector and display a histogram based on those numbers.
Okay, so I'll close this, and I will open up the histogram project.
I'll double click on that to make it the active project.
Right now there's not much in here.
It's just a really, really simple basic main that does
nothing just prints a new line.
So what do we want to do here.
We want to ask the user how many data items they have.
Let's say they type 6.
Then we ask them for each one of those data items, which is just an integer.
And then when we're finished with that, we'll output all that
information using a histogram.
So that's pretty straightforward.
We need to ask the user how many items they've got.
So let's create an integer variable there, called num items.
And we'll just initialize that to 0.
And then we'll ask them how many items do you have.
Let's say data items.
Okay, and we'll read that integer into num items.
So now we know how many data items they have.
What we want to do next is loop that many times.
So if I've got 5 data items, well, I want you to ask me 5
times to enter those that data.
So it's really makes sense to use a for loop here because we know exactly
how many times we want to iterate.
We also need to store this information somewhere, right.
So all that information they type in, we'd like to store it somewhere.
Since we really don't know what number they're going to type in, an array
would not be a good idea here because you don't know how big to make it.
You'd have to make it really big just to be sure that everything will fit.
So we'll use a vector.
First thing we'll do is we'll include vector here.
So we're good to go there.
And let's declare the vector right now.
We'll call it vector of integers in this case.
And let's just call the data because that's what it is.
And we'll initialize the vector it's empty.
All right, remember, a vector is a dynamic data structure so
we'll be able to read data from the user and put it into that
vector using its pushback method.
So let's loop.
How many times are we going to loop?
We're going to loop from 1 to that number they enter, right.
So if they enter 10.
We're going to go from 1 to 10.
And we're going to grab 10 data items.
So let's do that right now.
4 int I, we'll use I and we'll initialize I to 1 because
we want to start here at 1.
and I want to loop while I is less than or equal to the
number of items that they want to enter into the vector.
And I will increment I by 1 each time.
Now I'm going to use a block statement here.
Now what do you think is going on in here.
Well, I need to ask them for the data, right.
I'm going to loop let's say I typed in 10 items.
This is going to loop 10 times.
So we need somewhere to read the data into that sounds
like an integer to me.
So we'll say data item.
We'll initialize that to 0.
And then we'll just say cout you know enter data item and we may
as well tell them which 1 it is because we know it right it's I.
We do something like that.
We'll put a colon so it looks nice for them.
So this will say enter data item 1, enter data item 2.
All the way up to whatever they decided they wanted.
What we'll do now is we'll read that integer that they type in into that
data item variable we just created.
So we've got the data.
What do we do.
We need to put it into that vector now.
Remember, how to do that.
The vector's name is data.
So we say data.pushback, and we push that data item in there.
So it's going to put the data item in the back of the vector.
That's it.
That's pretty straightforward.
So at this point, now what we've got is a vector that contains
whatever data they typed in.
So if they typed in 5 integers, that vector has 5 integers in there.
So what do we want to do now, well, we want to display the histogram.
But before we do that, let's just display the data and
make sure we have it, right.
It's always good to test as you go.
So let's do that.
We'll say 4, we'll use a range-based for loop here, auto val, that's the
value, and the collection is data.
Okay, so for each element in that data collection, let's
just output it now just to test.
Okay, that's it.
Let's run this.
How many data items do you have.
Well, I have 5 data items.
Enter data item 1 10 to 20.
Let's just put them in 10s that way we know what we're getting 30 40,
it should ask me 1 more time 50.
Now it should be done.
And I'm displaying the data items.
So everything's looking good.
Let's run it again with 3 data items.
Let's put a 1000 in here, let's put a 10 in there, let's put a 1200 in here.
1000, 10, 1200.
Perfect. So that's exactly what's going on right now is
right where we want to be.
How many items do you have, and enter data for each one of those values.
Good.
So it's always a good idea to test as you go because it just makes life
much, much easier to test as you go rather than trying to test this
big monolithic thing at the end.
All right, so let me just do a quick statement here and
I'll put a new line in the end just to give a little space.
I'm just going to say displaying histogram.
Now what we need to do is pretty much what we did here right except
we don't want to display the value, we want to display a dash
for each one of those values.
So for example if the value is 10, we want to display 10 dashes.
If the value is 100, we'll display 100 dashes.
That's what a histogram looks like.
So how do I do that.
Well, I need to have another loop in here.
And I'm just going to get rid of that right.
Now so we still have this loop right here, it's looping
through the collection.
So how what do I do here.
Well, I know how many times I need to loop, right.
I need to loop val time.
So a for loop is good.
We can use any loop we want really.
But let's use a for loop because it's really simple.
So we're going to say for int and let's use I again.
And we're going to start at 1, and we're going to loop while
I is less than or equal to val.
That's the number we just read.
Okay, now what.
Well, the increment is by 1, we need to increment by 1.
So that's my inner loop.
What do I want to do in here.
All I want to do is just type a dash.
And we'll use dashes for a reason.
You could use an asterisk, but I'm going to use an asterisk in a second.
That's it.
So if the... let me fix that typo... if the user typed in 5 integers and the first 1 is 6
then val is going to be 6.
And we're going to loop 6 times displaying 6 dashes.
If the next one is 12, we're going to display 12 dashes.
After we're done displaying those dashes, we need to display a new line.
Otherwise, everything's just going to go right across the screen.
So I'm just going to say end line right here.
And we need to close off this -- let's do that for loop right here on a
block as well since we're probably going to add a little bit more code.
Okay, so that's where we're at now.
You can see pretty straightforward.
Let's run this.
And I forgot a semicolon right there so I put that in there.
Okay, how many items do you have.
Let's just start simple, 3 items.
What's the first data item.
5 10 15.
What I expect now is 3 rows: one with 5 dashes, one with 10 dashes
and one with fifteen dashes.
Awesome.
That's what we've got.
Displaying histogram 1 2 3 4 5 dashes, 10 dashes, 15 dashes.
You see that, that was pretty simple, and it's pretty powerful too because
we've got dynamic data structure that's growing as we need it.
Let's run this one more time with a little bit more data.
Let's say I have 10 data items and I've got a 10 34 and I'm just going
to put some really any numbers in here 7, I don't want to do a 90,
let's do a 30, let's do a 50 and let's do a 1 and a 2 down at the bottom.
Okay, so there we go.
There's my histogram.
You can see the last two should be 1 And 2 and they are right here 1 and 2.
And you could count them if you like.
But I think they're correct.
Obviously, you would want to count them if you were going to test them
to be sure that everything is correct.
So there's my histogram, pretty straightforward.
This is kind of hard to see though I mean how many dashes
are here, I can't really tell.
So why don't we say that every fifth dash, let's display an asterisk
instead that'll make it easier to see.
Okay, so how do we do that.
We're right in here, so what could do is we could modify this code slightly.
We could say if that specific I my counting variable, right, mod
5 is equal to 0, so if that means I'm on a fifth number, right.
In this case, I want to display an asterisk.
Otherwise, so I need an else here.
I just want to display the dash.
And let me indent this correctly.
There we go.
So notice the indentation here.
It's really important.
If you look at that piece of code right there, you can see that
I have a for loop and I've got another for loop nested within it.
And that for loop has an if else statement.
So every time that loop executes, I'm executing that if else statement.
Okay, so you can see the structure of the code by just looking at the code.
You don't have to try to figure it out.
If everything here was indented to the left flush here, it'd be
really hard to see the structure.
So let's run this and see -- okay, let's say I've got 10 data items and
I've got 34 32 24 5 10 and just some other numbers that I can put in and
let's do 2 and 1 again at the end.
There we go.
So now you can see that every fifth number is being
displayed with an asterisk.
So in this case it's really easy I can tell that's 10.
I can tell that's 19.
I can tell that's 5 10 15 20 25 34 and so forth.
So it makes it much easier to interpret the data.
Here's a really easy simple example of using a nested loop.
In this case, the 4 4 makes sense because we know how far
we're going but we could easily rewrite this with whiles or
do whiles or anything we like.
Remember, at the end of the day, a loop is a loop is a loop, right.
So they all do the same thing.
They all iterate.
It's just a matter of trying to choose the right one for the application.
So hope that helps.
These kinds of examples are going to be really useful in the
challenge exercise which is next.