Hello, and welcome. In this lecture, I want to talk about nested loops and loop control.
Nested loops, loops inside loops, loop control has to do with the break
and the continue statements.
So nested loops.
There are times when you want to place one loop inside of another loop.
It makes perfect sense.
So for example,
you might want to count the number of occupants in each house on a street.
So what you would do is you would have an outer loop where you step from house to house.
And then each house you're inside of, you would count the number of occupants.
So I hope you can visualize or understand how that could be a nested loop.
Again, the outer loop to go through all the houses.
And then for each iteration of the outer loop each house that you encounter,
you would go inside the house and have a loop to count all the people.
So that's kind of a little use case when you want to actually
utilize a nested loop in c.
Here's an example. It looks just like you think it'd look
a loop inside of a loop really not too complicated.
In this example, we're using for loops for.
Loops are nice because we can initialize the loop control variable,
have our condition check and our modification loop control variable all in one line.
So this outer loop and refer to it as the outer inner loop.
The outer loop is exactly what it implies. It's the loop on the outside.
And you can have many, many different layers of nesting. You could have an
many inner loops.
In this case, we're going through
and it's counter-controlled loop, we're starting at one and we're going all the way to count.
We don't know what count is here, it doesn't matter.
In the body of the for loop, we're saying sum equals 0. We’re initializing the sum for the inner loop.
And then inside the inner loop, we see another for loop for and j equals 1,
j is less than equal to I and plus plus j. We then say sum plus equals j.
We're calculating the sum of the integers from 1 to I.
So what this is actually doing is you're entering a number 5.
And for the first iteration of the loop,
you're summing 1 2 3 4 and 5.
For the next iteration of the loop,
you're uh summing 2 3 4 5.
For the third iteration of loop, you're summing 3 4 5 and so forth.
So inner loops are necessary especially just based on the use case,
just a loop inside of a loop perfectly.
You're going to see it quite a bit.
Don't shy away from it. Depending on what you need to do and what problem you're trying to solve,
it's often used quite a bit.
And to that point, it's not always a for loop inside of a for loop,
you can have a while loop inside of a for loop. You could have a do while loop inside of a for loop,
it doesn't matter.
So in this example,
same exact example except now inside the body of the for loop,
we have a nested loop an inner loop called using the while keyword.
So we're still calculating some of integers from 1 to I.
We have some uh initialization outside of the while loop.
We have a condition inside the while check,
and we have our sums inside the body of the while loop
where we're modified that's the loop control variable that we're modifying.
So sum plus equals and then we're modifying the loop control variable with plus plus j.
So if we didn't modify plus plus j, we would have an infinite loop for the while loop.
But it's doing the same output, it's just using a while loop as a nested loop instead of the for loop.
So the nesting isn't really any different from when you're nesting anything.
You nested if statements, if statements inside of if statements,
nested loop statements loops inside of loops.
To get away from the nested loops concept,
let's talk about the continue statement.
Up until this point when we've talked about loops,
we've always executed the loop a number of times based on the exit condition,
the Boolean expression that evaluates to false.
However, there are times when you want to specifically end a loop early
or you want to continue
on the next iteration of the loop without doing anything.
In the latter case continuing without
doing anything, skipping essentially,
you can use the continue statement to do this.
So the continue statement in the body of a loop does the following.
And first of all, if you want to initiate this, all you have to do is put the continue
keyword followed by a semicolon in the body of the loop.
But what it's going to do is it's going to basically
skip that iteration. So the continue
anything below the continue will not be executed if it's inside the body of the loop.
So continue a lot of times we'll want to be at the top of the loop
based on some condition, it's usually wrapped in an if statement.
If this happens special situation,
I don't want to execute any of the code in the body of the loop,
just skip this one iteration and check the next time.
And again, it's a little confusing because you're skipping an iteration and you're jumping around,
but it's valid, and it's a way to do multiple checks.
So even though you have your loop exit condition inside your Boolean expression,
you can have another check.
And that check is not going to jump out of the loop,
but it's going to skip an iteration.
So it's used again for the case when you want to skip iteration. The advantage of it
is you can use continue and it will eliminate a lot of nesting.
It will also eliminate a lot of additional if statements inside your block of loops.
So it can enhance the readability
when the statements are very long or you have deep nesting.
So if you find you're doing a lot of nesting.
you may want to say huh maybe I can use a continue instead.
Or if you find that you have a lot of if statements
that are doing certain things inside the body of the loop, maybe you want to do a continue.
Don't use the continue if it's complicates rather than simplifies the code.
Don't use the continue if you don't know what you're doing.
Here's an example of the continue.
In this example, we're creating a new enum of type day,
and we're saying that the valid values are Monday Tuesday Wednesday Thursday Friday Saturday Sunday.
And then we have a for loop where we're initializing
our enum to Monday,
and we're saying as long as day is less than equal to Sunday,
that's a condition to exit and then we're saying plus plus day.
You may ask yourself the question how we incrementing an enum?
Well, remember that an enum under the hood is an integer.
So you can say plus plus day on an enum and that's just going to increment it to the next one.
Remember, Monday is equal to 0, Tuesday is equal to 1,
Wednesday is equal to 2, it's all the order of the list.
So perfectly fine to increment enums.
In this for loop if it's Wednesday,
we want to skip. We don't want to do anything. We don't want to print.
So we say continue. If the day is equaled equal to Wednesday,
continue. It's going to skip the print line. It's going to go back up to the top of the loop,
and it's going to execute the next iteration.
And so it's then going to be Thursday.
It's not going to be equal to Wednesday, so it's not going to skip.
It's going to go right down and print out it's not Wednesday.
So this is a common use case, common example of using continue, very easy to use syntax,
just a simple keyword followed by the semicolon.
The other case where you may want to do something weird in the loop
is if you want to exit early.
So normally after the body the loop has been entered,
a program will execute all the statements in the body of the loop
and for each iteration and then it will check and jump out.
And we know how the continue works which is doing a little different skipping.
There's another statement that can alter the execution of all the statements in the loop,
it's called the break.
And this should be somewhat familiar because we use the break keyword when we talked about
the switch statement.
Remember, when we had our cases, we would break out of cases that way.
Well, you can also use the break keyword inside of a loop.
And what this will do is it will just jump out of the loop.
So the break statement is going to cause the program to immediately exit from the loop, it is executing.
Statements in the loop are skipped and the execution of the loop is terminated.
So you're just jumping out of the loop.
You're executing early. If the break statement is inside nested loops, it's only going to affect the innermost loop,
containing it. You're only going to jump out of the loop that you're in.
And to do this you use the keyword break followed by a semicolon.
Break is often used to leave a loop when there are 2 separate reasons to leave.
So earlier I mentioned that you may have another reason where you want to skip an iteration,
but if there's 2 reasons, 2 conditions where you want to jump out of a loop,
you can use a break.
If you only have onecondition,
then it's just the condition, inside the loop itself the inside the parenthesesof the while
or before the second semicolon in the for loop.
Break is also used in switch statements, as I mentioned.
So I just wanted to remind you that. Don't get them confused.
They essentially do the same things though. They jump out of code.
Breaking a switch statement will jump out of that
switch statement entirely, breaking a loop will jump out of that loop.
Well, here's an example. We have a while loop.
We're printing out some integers.
We're reading in some integers. We have a nested while loop.
Inside the nested while loop,
we check what was entered. And if it's greater than 100,
we want to jump out of that inner loop. So we do.
Then in the outer loop, we have to do another check. If it's greater than 100,
we break out of that outer loop.
If you didn't have the second break, you would you would never jump out of the second outer loop.
So in this case, this reminds me of a place where you could use a go to.
Imagine if you had 5 nesting, 5 layers of nesting,
while loop inside a while loop inside a while loop inside of while loop inside of while loop.
If you would just use the break to try to jump out of loops,
you would have all these if statements in each loop to jump out.
Instead of doing all those if statements,
you could have a go to. The go to would jump out of all the loop state loops in one single command.
So that's a use case for a go to. But in this case,
we're just demonstrating breaks and make sure you understand
you're just breaking out of that loop that you're inside of.
And in this case, we're breaking out of 2 loops.
So these are the 2 examples, the 2 keywords where you can essentially
not repeat code of what you expect it to.
So if you ever see a break or continue in the loop,
make sure you understand what's going on because
you may not be executing certain code that you think is going to be executed.
Again, usually wrapped inside of an if condition. So
you can look at it that way, but you also want to provide good comments when you use break or continues.
Hope this helps. In the next lecture, we're going to do a challenge
where we're going to be able to utilize these loops
and it will demonstrate and help you learn all about while for loops
and break and continue. Thank you.