welcome back to program logic and design in this section we're going to be discussing nested Loop so when do we actually need nested Loops nested Loops are needed when we have values of two or more variables that keep repeating to produce every combination of values in some programming languages they requir some sort of block indicators like Curly braces in C++ and Java so what does Ned Loops comprise of they're comprised of Loops within Loops so we get an outer loop and the loop that contains the other loop and the inner loop which is the loop that is contained within the outer loop so if I had to give you an example here would be my example we initialize our outer loop variable and then our outer loop variable is tested then we initialize the inner loop variable and the inner loop variable is tested and then we have our inner loop statements using the outer and inner loop variables and then of course within our inner loop we have to alter our inner loop variable and then we Ender in a loop once our tester has reached false then our outer loop variable is altered and then we repeat the process until we have falsified our outer loop variable and then we exit our outer loop I must remind you that like nested F statements the nested inner loop must be completely contained within the outer loop so if I had to draw a little box here you would see that it is complet contained within the outer loop that is why I've made it blue the inner loop as and the outer loop is red so you can see that it's completely self-contained it is very important that each Loop has its separate counters to control each Loop so our our outer loop variable here that would be its outer loop counter for the to show how many times the Loop's going to repeat itself the inner loop has its own counter so therefore they must have each Loop must have their own control counter for each Loop nested Loops are used to handle any spreadsheet type set of data in which the outer loop just manages the rows while the inner loop manages the columns as per se now you guys will use nested Loops in the next chapter with twood dimensional rays but for now I'm going to walk you through an example where we use nested Loops to create an output such as this for our answer sheet so what we're going to do is the outer loop is going to counter is going to control the ones for our parts 1 2 3 4 5 where our inner loop is going to control these numbers that are going to be changed the numbers for the questions if you have a look at this example we've got our quiz name and our quiz name will correspond to chapter one quiz so that will be our first quiz our first loop as per se so quit quit here which will be triple Z will end our quizzes Which which we're going to be creating in our answer sheets for our quizzes so if quit is entered zzz like after this third answer sheet that was produced we will exit the program our housekeeping will be where we input our quiz name so our first quiz name we always need an priming read we need to initialize quiz name and the user will be initializing quiz name before we go into our decision here our end of job over here is null but I would like you guys in future sure to use end of program as an output statement within end of job now within the quiz name over here within the the production of our answer sheets is our detailed Loop and what's actually happening in our detailed Loop is we are putting our quiz name which is this section here then we are having our part counter equal to one so what we're doing here we initializing part counter so initializing our part counter to be one so when we need to Output it which is over here output part label and part counter it's going to part label is part and our part counter is one so we're outputting part one you'll see we'll exit our part Loop over here when we reach less than and equal to five because we have five parts so that is only when we're going to be exiting this Loop so we're going to first first loop our first Loop is going to go when now we initialize our question cter to be one and what we're going to produce is we're going to produce question counter which is one and our line and our line will just be the space so what we're going to do is on our first loop we're going to Output part one and we're going to go into the part counter Loop and output one Space 2 line three line then what we're going to do is we going to now that we have increased there is our altering of our question cter from 1 to three for every loop we're going to exit because now we've reached three questions and we're going to change or alter our part counter to 1 + 1 which is 2 so now we're going to Output part two and we're going to repeat this Inner Loop and inner loop is going to question so it's one remember our in a loop here our question counter will be changed back to one when we've looped for the second time when we've changed Parts counted to be two you'll see when we loop back you'll see we output part part label which is part count part counter which is two now and then we start with one again we have to change it back to one that is why outside our Loop we always initialize our counter variable then we Loop question 1 2 3 and once we reach to three we go out of that Loop we change our part counter to three and then we Loop right back up to part counter and we do this again for part three go into our nested loop our inner loop outer loop inner loop Outer Loop in a loop and once we have reached our parts counter to the end of five we're going to Loop only once more we've at we output our questions one line one line one line so one line two line three line and then we exit that Loop we increase our part counted to six and six is less than equal to five no then we enter our new quiz name or quick to enter and in this case our new quiz name is now going to be extra credit quiz and then we're just going to repeat the process again of outputting our part counter using the outer loop first inner loop out Loop first in a loop outter Loop first in a loop outter Loop first in a loop out a loop in a loop hence we're going to repeat the process again quiz name our new quiz name is going to be makeup quiz so in other words we can create answer sheet after answer sheet after answer sheet until the user Alters our quiz name to zzz saying that we have made enough answer sheets let's end of job let's exit the program and stop here is the sud code for our answer sheet program you'll have a look here is that we initialize here and go into our outer loop can you see the indentation very important that you have indentation because it eliminates errors in your logic as well as you'll know that after your end well here or after this end well for our inner loop we have to alter our part counter here are some nested Loop facts the nested Loops never overlap so an inner loop is always completely contained within an outer loop an inner loop goes through all of its iterations each time its outer loop goes through just one iteration now the total number of iterations executed by a nested Loop is the number of inner loop iterations times the number of outer loop iterations hence if we to work out the total number of iterations for the answer sheet program we have five parts and our inner loop has three lines so three multiply by 5 is 15 so in other words if we had to have to count this will be 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 so this will be one Loop here's my inner Loops here and my inner loop and my inner loop and my inner loop and my in a loop therefore 15 times will be the iterations for this program with a nested Loop please join me in the next video in which I'll be discussing avoiding common Loop mistakes