 Hello friends, I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to explain how you can implement nested loops in C programming. So I will be explaining this design with the help of nested loop, how you can implement this design with the help of multiple loops in C programming. So before starting, I just want to share one information. If you go to the detailed description of this video, you will find links of various pages related to various programs. So you can watch them to learn different programs. Now coming on to the topic. So before moving to this five by five matrix kind of thing, which is implemented with the help of star sign. Let's say if you want to print a single star, if you want to print a single star. So what you do, you will implement this printf and here you can provide a single star. Now our requirement changes. We need to print this only. We need to print this five stars in a single row. So there are two options. Either you can use printf and put five stars. But if my requirement changes like I want to print 50 stars, then what you need to do, we need to change the printf again. So that is not a good practice. So the first method we cannot use because it requires changes every time, whenever number of stars in field. So to solve this problem, we have one solution. So that will be a second method. I am implementing a loop, which will repeat five times. And inside this loop, I am using printf. So if I want to print five stars, so this loop will repeat five times. If I want to print 50 stars, so I just need to change the termination condition of the loop. So this time it will repeat 50 times. So you saw I didn't change printf statement to print a number of stars. I just changed the condition of the loop. So this is the better way to print this row with the help of single loop. But if we pick this, so this can print only first row. Now our requirement is I want to print this five times. So what you can do, you can copy this and you can paste it five times. So you need to implement five separate four loops so that you can implement this. But again, that is not a good solution because if you have 50 rows, if you want to print 50 rows, So this is row number one, and two, then three, then four, and this way you need to print 50 rows. So is it possible to implement 50 separate loops? No. That is again not a good practice. So to resolve this problem, we need to repeat the process who implemented these five stars again and again. So it means to copy and paste this, to copy and paste this four loop will not be a better solution. So in place of copy and pasting, we will include this loop inside another loop. We will include this loop inside another loop. So that scenario is known as master loop. So friends, if you're putting a loop inside another loop, so that scenario is known as master loop that I'm going to explain now. So I'm removing this printf. I am implementing one more loop inside this and here I'm writing printf inside double quotes star sign. Then after completion of this fall loop, I'm going to write this and it is complete. So this way you can, so you can see this is outer fall loop and this is inner fall loop. So this is outer loop and this is inner loop. Right. So execution cycle will begin from outer loop, then it will go to inner loop, then again outer loop is incremented and control will transfer to inner loop. Now I'm going to explain the complete life cycle of master loop so that you can understand how we can print this kind of scenario. So first of all this will be performed. So beginning from i equals to 1 will be done. Then this condition will be checked. So right now it is true. Then control will be transferred to inner loop. So j will begin from 1. So this is third step. Then control will move to j less than equals to 5. That is condition fourth step. Then control will come here. If this condition is true, then this printf will be printed and from here we will move to j plus plus. And after this condition checking. So for this loop, this initialization will be done once and four, five steps will be repeated till this condition is true. When this condition is false, then this condition is false, then control will be transferred here. So after this, if it is false, control is transferred here, which is seven steps. So here you can see this loop will be five times so it will print five steps. Then we want to shift to new line. So after completion of this loop we are here. After this, we are moving here. That is eight steps. i plus plus. So initially i was one. Then i was one. So j repeated five times. So if outer loop is repeating one time, inner loop will complete its life cycle. Then outer loop will be incremented. It will check its condition and the same process will repeat again. So right now you can see value-wise two. Initially it was one. When i incremented it to two. So this time i is two. Now check this condition. It is again true. So again control will be transferred here and this life cycle will repeat. So starting from one, it will go to five. It will again print five steps. When this condition is false, it will come here and then again it will go here. So if outer loop is executing one time, so inner loop in this case is executing five times. So when i is one, j is repeating five times. When i is two, j again repeating five times. If i is three, j again repeating five times. So this way this nested scenario is working. So always remember when outer loop is executing one time, control will transfer to its inner loop. Then inner loop will complete its cycle and after when this condition is false, control will transfer to the increment part of outer loop. Then outer loop checks its condition. If it is true, then again inner loop initializes from the beginning and completes its life cycle. So in this scenario, if we count these, so in total i will repeat only five times. But j will repeat total for different times. Because when i is repeating one time, j is repeating five times. So if i is repeating five times, so j will be repeating for different times. And one more thing, this is outer loop. This is inner loop. Then this inner loop can have one more inner loop. Then that inner loop can have one more inner loop. So this cycle can go into any number of gaps. And one more thing is possible like one outer loop can have multiple inner loops. Let's say this outer loop is having this inner loop. After this it can have one more separate inner loop. So that is also possible. So it depends upon the requirement how you want to implement the combination of loop. So I hope with this explanation you understood the life cycle of master loop, how it works, how we can place one loop inside another loop in the seed programming. So with this, this pattern will be printed. And if you want to increase the dimension like 5 by 5 stars are printed right now. If you want to increase it, then you can change here. You can change i and you can change j. So this is row which is counted by i and these are columns. So if we go here, so these are columns. So whatever value you increase accordingly pattern will be printed. So I hope you understood how master loops works in seed programming. And I hope you understood whatever I explained in this video. If you want to watch more programming related videos, you can open my channel and go to playlist and watch related video. Thank you for watching this video.