Nested Loops and loop Control - Break and Continue

Tim Buchalka's Learn Programming Academy
A free video tutorial from Tim Buchalka's Learn Programming Academy
Professional Programmers and Teachers - 990K+ students
4.5 instructor rating • 50 courses • 991,749 students

Learn more from the full course

C Programming For Beginners - Master the C Language

C Programming will increase career options. Become a better dev in other languages by learning C. Pointers explained

24:02:32 of on-demand video • Updated July 2020

  • Understand the fundamentals of the C Programming Language
  • Make yourself more marketable for entry level programming positions
  • Create your first C Application
  • Learn one of the most popular, widly used languages in the world
  • Understand variables and the different data types
  • Apply for real-time programming positions
  • Understand the core language that most modern languages are based on
  • Learn how to write high-quality code
English [Auto] 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 continue statements and nested loops just mentioned. There are times when you want to place one loop inside of another loop. Makes perfect sense. So for example you might want to count the number of arguments in each house on a street. So what you would do would you would have an outer loop where you start 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. Can the outer loop go through all the houses and then for each iteration of the outer loop each house that you encounter you 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 and see here's an example. It looks just like you think it look a loop inside a loop. Really not too complicated. In this example we're using four loops for loops are nice right because we can initialize loop control variable have our condition check and our modification loop control variable on 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 a loop on the outside and you can have many many different layers of nesting. You could have an outer inner many in her loops. In this case we're going through encounter control loop. We're starting at 1 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 zero initializing the sum for the inner loop and then inside the inner loop we see another for loop for NJ equals 1 j is less than equal and plus Ball State. We then say sum plus equals j. We're calculating some of the integers from 1 to i. So what this is actually doing is you're entering the number 5. And for the first iteration of the loop you're got your. You're summing 1 2 3 4 and 5 for the next that are the version of the loop. You're summing to three four or five for the third iteration a loop you're summing 3 4 or five and so forth. So in error loops are necessary especially just based on the use case. Just a loop inside of a loop. Perfectly fine news you're going to see it quite a bit. Don't shy away from it depending on what you need to do and what probably 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 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 and inner loop called using the while keyword. So we're still calculating some villagers from one. I we're just we have some initialization outside of the While loop. We have a condition inside the while check and we have our some's inside the body of the wall. Where we're modified That's the loop control variable that we're modifying. So some 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 a infinite loop for the while loop but it's doing the same output it's just using a while loop is a nested loop instead of the four. So the nesting isn't really any different from when you're nesting anything nested if statements if statements inside of his statements nested loops statements loops inside loops to get away from the nested loops concept. Let's talk about the continue statement up until this point when we talked about loops. We've always executed the loop a number of times based on the exit condition the boy and expressing any value to false. However there are times when you want to specifically end the loop early or you want to continue on the next iteration of the loop without doing anything. In the latter case continue without without doing anything skipping essentially. You can use the current continued 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 semi colon 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 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 loop just skip this one interation and check the next time. And again it's a little confusing because you're skipping in iteration 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 bool an 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 integration. So it's used again for the case when you want to skip erase the advantage of it as you can use continue and it will eliminate a lot of nesting. It will also eliminate a lot of additional If statements inside you block inside blocks 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 oh maybe I can use a continued stand. Or if you find that you have a lot of IF statements that are doing certain things inside the body loop maybe you want to continue Don't use to continue. If it's complicates rather it simplifies the code. Don't use the continue if you don't know what you're doing. Here's an example of that continue in this example recreating a new ENM type day. And are you saying that the valid values are Monday Tuesday Wednesday Thursday Friday Saturday Sunday and then we have a for loop we're were initialising are enough to Monday and we're saying as long as they 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 are we incrementing an ina or member that it enough under the hood is an integer so you can say plus plus day on the genome. And that's just going to Inkerman it to the next one member Monday is equal to zero. Tuesday is equal to one Wednesdays equal to two. It's all the order of the list so perfectly fine to increment and anime's 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 equal to 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. 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 Michael the other case where you may want to do something where it in the loop is if you want to exit early. So normally after the body the loop has been entered a program where X all of the statements in the body the loop and for each iteration and then it will Chacun 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 brachy word 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 brakes name is going to cause the program to medially exit from the loop. It is executing statements in the loop or skipped in the execution of the loop is terminated. So you're just jumping out of the loop you're asking an early if the bank's name 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 use the keyword break followed by a semicolon. Break is often used to leave a loop when there are two 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 two reasons two conditions where you want to jump out of the loop you can use a break if you only have one condition. Then it's just the condition inside the loop itself inside the parentheses of the while or the second before the second semi-colon in the four break is also used in switch statements. As I mentioned 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 break in 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 managers 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 to 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 break in the second break you would you will 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 five nesting five layers of nesting while loop inside the while loop and so on a while loop and set a while loop instead of wild if you will just use the break to try to jump out of loops you would have all these statements in each loop to jump out instead of doing all those estimates you could have a go to the go to would jump out of all the loops loops in one single command. So that's a use case for a goto. 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 where we're breaking out of two. So these are the two examples the two 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 makes 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 braker 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 breaking Kitty. Thank you.