OK.
Welcome back, everybody.
So in this lecture, we are going to be discussing a new topic which is nested loops, so nested loops
are pretty much just the idea of having a loop or more inside of a another loop as many layers as you
could think of.
So having a loop inside of a loop or a loop inside of a loop, inside of a loop, inside of a loop,
you know, continuing.
This is the idea of nested loops in this lecture.
We're just solely going to be covering a singly nested loop, which is just a loop inside of a loop.
No more layers than that.
And this can initially be quite a confusing topic, but an important thing to remember is that you can
always go back to the execution flow of this and draw it out step by step so you can go through a loop
and its inner loop and keep track of the variables that are changing for four loops and stuff like that.
And it'll help you kind of figure out what is going on.
So if it gets confusing, pull out a pen and paper kind of draw out a table, keep track of the values,
make some drawings step by step to the code, and that'll be a good way to remind yourself of what's
going on.
If, after this lecture, you forget and become a little confused at what's really going on inside these
nested loops loops.
So one thing we do have to become comfortable with is the idea of for each time something happens.
Another thing will happen multiple times.
So when I say for each time, something happens that something is the outer loop and the another thing
that happens multiple times is the inner loop.
So I think the best way to explain this is to use an analogy.
And in this analogy, what I am using is a sandwich shop, so you can imagine something like Subway.
If you've ever been there, that's an international chain.
So maybe it's a good idea to use that.
And at this particular specific sandwich shop, we only have one employee working right now.
So we have a line of clients here, and each client basically needs to be able to go through the sandwich
bar and choose each one of the toppings and then pay before the next client is allowed to start that
process.
So this is the type of live control structure I'm talking about the turquoise or teal, kind of.
Circle here is representing the outer loop, so everything that's in between.
So I should say everything that is actually inside of this turquoise shape, but outside of the purple
shape.
So in this region, I'm actually going to trace it.
So this looks this here and this area in this dollar sign over here and all of this that is considered
the scope of the outer loop.
Specifically, just the outer loop.
Although the inner loop is still inside of the scope of the outer loop, the inner loop is represented
by the purple.
So everything inside the purple, which is the same which path that is the inner loop.
So what we're going to do is have one client go into the inner loop and choose each one of these topping
categories, which is a b for bread and for meat, and B for veggies.
So they will iterate over each one of these and then they'll go back out into the outer loop and they
will have to pay and then they'll be done and we can then start processing the next client.
So let's go ahead and walk through this first client gets processed here in the outer loop and we then
head into the inner loop with that client.
This client chooses a bread, so that's one iteration of the inner loop.
Then they go through and they choose the meat that is, say, next iterations of the second iteration
of the Loop.
And then they choose a type of veggies or multiple veggies, and that is the third iteration of the
inner loop.
They then exit the inner loop and go back into the outer loop to do one last thing, which is pay.
And then they are done and we move.
We process the second client and then bring them into the inner loop.
They choose their bread, they choose their meat, they choose their veggies.
They go out of that in a loop and they then pay the register.
We then process the third client.
This client chooses bread and then meat and then veggies, and then they go to the register and then
we do the last client.
They come into the inner loop and they iterate over bread, meat and then veggies and then pay.
And then we are done.
So I am now going to go over this with a code example that is pretty much the same as what we drew right
here.
So we're going to chase this code example of the sandwich bar here in code just to give you an overview.
I start out by defining, declaring, initializing a variable here called line length.
I say equal to four.
I then have an array of type char that size three that's called toppings, and it has those symbols
right B, M and V for bread, meat and vegetables, respectively.
So then we start our loops, and this is where it gets interesting, so.
We're starting out with a person here, and we're saying that our person starts out at person zero.
We want to go as long as the person number is less than the line length.
So that will be zero one to three.
That is four times one for each of the four clientele of the four clients.
We do a person plus plus because we're going to increment from one client to the other, right?
So I'm going to move to this next line and I'm going to kind of now from this point on, keep track
of these really important variables.
So we are on person.
Zero person equals zero to start with.
We have now hit the line of our inner loop here, where we have top in position is equal to zero.
You notice that we're also doing a tapping position less than three top position plus plus because there
are three toppings and we care about going from zero one two, but not to three because that's sufficient.
Zero one two two cycle across our top end positions.
So now that we've had this line and we have started out with Typekit position on zero, we can go to
this inner line inside the inner loop.
We say we print out person, what is person zero?
So Person Zero chose topping, then we see this array.
We ask ourselves what is top position that is zero, so what is at top position index zero?
It is B.
Therefore, this whole sentence will be person zero chose topping B.
Right, and then a space we now have finished with this iteration of the inner loop, so we go back
up to the top and now we do a top in position plus plus, which brings us from top position was zero
to top in position is now one.
We go back into the loop and we say person zero, because person has not changed, we're still on person.
The first person which is represented as persons zero, right, and that's what we started out on.
This is still the first client in terms of just ordering, but the computer science position, you know,
is zero because we're starting zero.
So we say Person Zero chose topping, but now we're on one with position one inside here it's M.
So we're saying person zero choice topping M and then a space.
We go back up here now it's two.
So we can say Person zero now chose topping V.
It's the final topping right.
We go back up here, we increment in position to three.
But what we said is that we're not going to run the code inside this loop.
If top position is not less than three, because this was our loop running condition, it has to be
less than three since that condition is not satisfied.
We are now finished with our loop.
We will hit this bracket here and continue on to the code.
Here we are now going to print out a new line character.
So if you haven't seen this yet, it's similar to end line.
It just prints out a new line so we can move on to a next to the next line of the console.
And then we say Person zero.
Paid and then we print out and in line, right?
So they paid.
Now we have finished with the outer loop because we hit this close bracket here, right?
So we're done with everything inside the outer loop, so we go back to the top for the next iteration
of the outer.
Now that we're back up here, we can increment the person so person goes from zero to one.
This condition says that will go as long as a person is less than line length, line length is four,
one is less than four.
We continue, of course, so we did that person plus plus.
So we're going to go into here now top in position.
You notice it just was a three.
It gets reset to zero.
Top position is now zero again and we can start cycling through our toppings for this person, which
is person one.
Technically, the second person, right?
Because we already did the first person, that person was considered person zero.
So we cycled through the toppings again, incrementing as we go, we say person one chose topping B
because topping B is the position zero right.
Increment it to the next topping now.
Person one just topping M now person one chose topping we we go up here.
Three is not less than three.
So now we're done with the inner loop.
We go to this last line of the outer loop and we say Person one has paid.
Now we go to back to the outer loop, we go to the next person.
We are now on person number two, which we consider the third person, right because we did zero and
one already.
And now we're on the third person, but we're calling it person two because it started zero.
We go here and start doing the toppings for person two, so we reset top in position to zero.
Person to choose this top and be person two to chose topping in person, to chose to be done with this
loop.
Person to paid person three.
We go in here person three.
So we in Typekit position back to zero.
We say person three chose topping be person three chose topping in person.
Three chose topping the.
We're done with this person to be paid.
We go back up here.
We say person is for him is for less than four line like this.
Four is for less than four.
No, it's not.
We're now done with our outer loop and we now go past this closing bracket and we just do it here.
Return zero.
We are now finished.
So I'm going to escape out of here, go into the visual studio where I have this code.
I am going to compile this code
and I'm just going to say
NFL easy.
I will now run an AFL AFC and we can check out the results, so let's bring this up.
I'm going to scroll up a bit and I bring this down just a bit.
So as you can see here, oops, sorry about that.
You can see that we said.
Person zero showstopping, be person zero, just tapping in person, zero showstopping V. I didn't put
a space between those, I should probably have done that.
That would be a lot nicer.
So let's say.
We do that, and then we put like a comma or something like that.
Let's go ahead and say this and make the output nicer.
So I'm going to compile again and run again.
All right.
That looks a little nicer.
Actually, person zero, let's put a space here as well, because that looks still looks kind of gross.
All right.
Here we go, so person zero, just tapping the person's, you know, just tapping in person through
just tapping we and we say persons who are still not a space there should put a space there, right?
So.
Let's go ahead and do that, make all of our little corrections, so the outfit looks nice and then
you notice that says each person chooses these toppings, then they pay.
Then the next person chooses their toppings, then they pay, then the next person chooses their toppings
and they pay and so on.
Until all four people starting at zero going to one and two, then three, they all get to choose their
toppings and pay in the correct order in which they should be moving through the sandwich shop.
Right?
OK, so pretty neat concept.
You can also loop some other things, though, so we can have a for loop inside of a while loop and
a while loop inside of a while loop.
So let's go ahead and check out some of those examples, too.
So I'm going to go up here and I'm going to go ahead and just delete this.
And then I'm going to say I'm going to put a while loop.
So I'll just say, let's say and.
The number of people equals four, and I will just say that we will do some sort of while loop where
we can continue getting some input or something like that.
So I could say, um, let's say.
Uh.
A name, right, let's do like a string.
Name equals.
We'll just say a string name, right?
No industry I'll get and then what we will say after that.
We all see in to name.
And then what we will do is we will say.
While name not equal to quit and what we will do is.
We will add the name, let's see, let's see, we'll just see out.
So we could say see out name
and then we'll just say.
For.
And equals zero, I is less than and we will just say a number of people plus plus and we will just
say in here that this is just kind of some random thing, you know, some hypothetical situation, but
we'll just say our name.
Helps person
I.
How about that?
And then we will do a space and something like that, and then we will just see in and we will get another
name right?
So this is just someone that maybe works somewhere.
Maybe the person at the shop counter what he does in this time, though, is it's just a bunch of people
coming by.
Let's just say that this is like people coming to get vaccinated for COVID 19.
It seemed kind of appropriate for this point in time.
Where am making this course?
At least, you know, it is, as you can see, June and twenty twenty one.
So we still have a little bit of a COVID problem now.
So let's just say that there's this guy or there's multiple people and they help four people each.
So we have some qualified people giving vaccinations and each one of these people, we're going to enter
their name and they're going to help their assigned people, which will be like person one person to
person to person or.
So let's go ahead and compile this and run this, it's still called nested for.
But we can, of course, uh, rename that.
We can do actually do something in here while I can say move nested for.
And I will now call it moving it.
You can actually change the name or you can move it to a different location.
But I'm going to do is change the name in this same directory, and I'm just going to call this nested.
I call it Nest Wild for CBD.
So now I move that it says that this is deleted here.
But if but we could totally open it up and it would be there.
So I'm scrolling through all of the gross stuff that I have in this single directory, which should
be organized.
You should organize it in a nice way and keep it like me.
So now I open this and it's totally fine and I actually can compile this and, you know, just allow
for show.
Well, for Dot C or easy so I can compile that.
Oops.
We get a lot of.
Errors here, so.
Let's see what the problem is.
See, that name helps, person.
It's not derived.
From we put the wrong
operators here.
So I actually put the wrong operators throughout this entire expression.
So let's go ahead and change that.
And it needs to be changed here as well.
So I put the input operator four instead of the the output one.
So now that is changed, I'll go ahead and save this and that should fix it.
All right, that fixes it, so I'm going to go ahead and run it now, so you notice nothing prints out
because we're just asking for the name without printing anything out.
The initial time.
So I'm going to go ahead and put a name, and I'm just going to say Bob.
And so you notice that it prints out Bob.
So I entered Bob and it says, OK, well, I'm Bob.
Bob helps person zero.
Bob helps person one.
Bob helps person to bob.
That was person three.
I did not do another in line after this, so it's kind of gross, but now I can say, you know, Mary
or something like that, and it says Mary Mary helps all of these people.
So you notice that what we're doing is it's waiting for us to enter a new name and it's going to keep
asking for a name over and over until we do quit.
And each time we enter a name, it is thing going to run this inner for loop, and it's going to say
that that person helps each of these other people.
You know, a number of people person, zero person, one person, two in person, three.
Right.
So each of those four people.
So I see Jeff now and it says, OK, we're on Jeff.
Jeff helps person zero.
Jeff helps person one.
And it's just going to keep going until we Typekit, in which case the program quits, right?
So just wanted to give another example there that you can put the four inside of the wall and you can
also put a while inside of me while here as well, I don't think that I need to demonstrate that.
I think you can figure that out.
You just simply put another well in there and you should be able to trace it on your own.
But I think that I'm going to leave off the lecture at this point because in the next lecture, we're
going to have to get into looking into.
Not just a raise, but we're going to look into matrices which are two dimensional arrays.
At least we're going to be looking at two dimensions, and they're not going to be looking at anything
more than two dimensions.
But we're going to need to use nested loops to be able to iterate over these two dimensional arrays,
and so I wanted to introduce the concept of nested loops so you could solidify that.
And then afterwards, you would understand more when we're introducing the matrices.
So with that, I will leave you to it, go ahead and practice some of this stuff here.
Once we get into the matrices, I will have some more practice problems for you to get used to all this.
But until then, I will see you in the next lecture.