 Hello everyone. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So today we are having day 13 of the C and C++ coding bootcamp. And in this session, I will be explaining you how we can implement nested loop in C programming, right? So in past sessions, I demonstrated you the working of for loop, while loop, do while loop. But if you want to implement more than one loop together, so how we can implement that in form of nested loop, so that we are going to discuss in today's session, right? So I welcome everyone who is watching this session live, or if you're watching recording, so you are also welcomed. And let's start with the session. So if you are attending any of the session on this YouTube channel for the first time, and if you want to know about me, so you can go through with this slide. And everything about me is written over here, right? So moving forward. So in every session, I say like consistency is the key. So if you want to become a good programmer, so you need to learn particular coding language and coding, you can learn through consistency only. So be consistent if you want to learn C and C++ properly. And you can join this telegram group. And if you have any doubt related to C programming, so C or C++ programming, you can ask questions here. And you can follow Sanjay Gupta Tech School on YouTube, LinkedIn, Instagram and Telegram. And all the important links are available in the video description. So today's topic is nested loop as I discussed with you. So before starting this, I can see one question in the chat. So someone is asking how close is C to C++? So basically, C is function oriented programming. So as of now, like whatever we have done in last 12 sessions, so you might have noticed everything we are implementing in the main function. So C is basically function oriented. So in a function, we can implement all the logic. And as per the requirement, we can execute that. But C plus plus is basically object oriented programming language. So in C++, if you want to implement any solution, so you need to implement classes inside classes, you will be creating some variables and functions, right? So implementation approach is totally different in C++. So C is basically considered as function oriented programming language, whereas C++ is known as object oriented programming language. So I hope this help you this will help you to understand the difference between C and C++. And once all the sessions of C programming will be completed, so I will be explaining you the difference between C and C++ when we'll be starting C++ sessions, right? So I think if you see in the session tracker, so it is already mentioned. So here we will see features of C++ and similarities and differences between C and C++. So to understand this first, we need to complete everything that is available in C programming. And whenever I will be starting sessions related to C++, so first we'll do all the comparison and then only we'll be starting implementation. So let's jump on to the nested loop. So if you want to see a nested loop, so this is the good example for that. So here in this example, what we have done, we are using two loops altogether. So first loop is basically known as outer loop. So here first loop is having a variable I so that we can consider as consider as outer loop. And inside the outer loop, we have inner loop. So second for loop, which is having a counter variable as J will be known as inner loop. So here we have two loops. So outer loop is having inner loop. And inside inner loop, we have a printf statement, right? So we just need to calculate the result of this code snippet. So what will happen? This outer loop, which is starting with I equals to one and condition is I less than equals to five. So whenever this outer loop is executing first time, so control will transfer to this inner loop. So in first rotation, when I is one, control will transfer to J loop. In that case, J will complete its execution, right? So when I is one, so J will execute five times. Okay, so what will happen? This this printf will be printing one, one, one, two, one, three, one, four, one, five. So I is always one five times, but J is incrementing its value by one every time because J is part of this inner loop, right? So first five outputs will be one, one, one, two, one, three, one, four and one, five. When this inner loop will be terminated, then I will be incremented by one. So I will become two, right? Then this condition will be checked to less than equals to five. So it is true. Then again, control will shift to this inner loop, that is for J equals to one and J less than equals to five. So what will happen? This time value of I is two and J is again starting from one. So next round of output will be two, one, two, two, three, two, four and two, five, right? So this way, when outer loop is iterating one time, so inner loop will complete its execution five times. Okay, so now I'm going to implement this code in our online compiler so that we can see like whether the result which I'm telling you verbally will be printed or not. Okay, so practically we'll implement this solution and we'll see the result. So here I'm going to create two variables, I and J, right? Now here I'm going to implement first loop, I equals to one, I less than equals to five and I plus plus, right? So this is basically our outer loop. So you can say it is outer loop. Now inside this, we are going to implement an inner loop. So this you can say inner loop and inside this inner loop, we are having a printf statement which will display two results. So here we are going to print value of I as well as value of J. Okay, so this is the code that I explained to you earlier. Now I'm just clicking on one and you will see the results. So here you can see the results. So let me scroll it. Yep. So now you can see the result. It is showing one, one, one, two, one, three, one, four, one, five. If I scroll down, so it is showing two, one, two, two, two, three, two, four, two, five, then three, one, three, two, three, three, four, three, five and so on. So when I loop is iterating one time, so J loop is iterating five times, right? So in total, I loop will execute five times and J will be executing 25 times. So this is the working process of nested loop. So if you have any doubt, you can ask your question in the chat so that if you join this session live, so I will be able to answer. And if you're watching the recording, so in the comment section, you can ask any doubt that is related to this code. Okay, so I hope with this output you are able to understand how these code snippets will execute. So whenever you have a requirement to implement any nested loop, so remember there will be an outer loop and there will be inner loop as well. Now moving forward, I have one more example for you, right? So here we have, if else, conditions also. So we have outer loop, which is having loop counter as i, then we have inner loop, which is having loop counter as j. And inside inner loop, we have if condition as well. Right? So what is happening here? If this condition is true, then one will be displayed, otherwise, zero will be displayed. Right? So after termination of inner loop, then we have backslash n. Right? So it will be showing different results. So in case of equal value, one will be there. In case of unequal value, zero will be there. But both the loops are executing five times. So a kind of matrix will be displayed because in this inner loop, you can see printf is not having backslash n. So these one, zero will display five times in same line. And when this inner loop gets completed, inner loop gets completed its execution. So after that printf backslash n will change the line. So when inner loop will be displaying all the values, then only your cursor will be shifted to the new line. Okay? So now I'm going to implement this solution. So basically, I'm going to modify this code so that we can understand the result of this code. So if it is true, I just need to display one. And if it is false, then I need to display zero. Right? So this way, this code is implemented. So outer loop is iterating five times. Inner loop is also iterating five times. Then we have a condition. If this condition is true, then one will display if condition is false, then zero will display. Now, once this inner loop is completed, so here I'm going to display backslash n. Right? So what is what is happening? Once inner loop gets completed, so this backslash n will change the cursor position. And in the new line, next row will get displayed. Right? So let me just show you the results so that you can visualize how the result will be available. So this, this will be the output. So you will have five by five matrix. And whenever I is equals to J, so at that position, one will display. And wherever I is not equals to J, so at that position zero will be displayed. Right? So this way in C programming, if you want to implement nested loop, so you can apply and inside loop, we can apply any conditional statements as well. So here in this example, I used if else as conditional statement. And whatever logic you want to apply, so inside loops, you will be able to do that. Right? So I hope with this explanation, you are able to understand how in C programming, we can implement nested loop. Right? Now, let's implement some patterns as well through this. So basically, here you can see we have a triangle. And in this triangle, we need to display asterisk. Right? So in this, in this example, what is happening in the first row, one star in the second row, two stars in third row, three star in fourth row, four star and in fifth row, five stars are available. So basically, in in previous example, what we did, we started both the loops with one and we we are having termination condition as less than equals to five for both the loop. If we go ahead and see this example. So here you can see total number of rows are five. So that's why outer loop will be repeating five times. But inner loop will depend on outer loop like in first row, one as one asterisk in second row, two asterisk or you can say star in third row, three in fourth row, four star in fifth row, five stars. So whatever number of row you are printing that much stars you need to print. So if I is one, so J loop will execute one time. If I is two, J loop will execute two times. So here you can see carefully J loop termination condition is J less than equals to I. So J is totally dependent on I. If value of I is one, so J loop will execute one time. If value of I sorry, if value of I is one, so J loop will execute one time. If value of I is two, so J loop will execute two times. Right. So totally J loop is dependent on I. And with the J loop, we are printing asterisk. Once J loop is completed, then this backslash and will execute and cursor will shift to the new line. Right. So now I'm going to implement this solution so that you can understand. So basically here instead of five, I'm using I and here I'm going to use printf and then asterisk. Start. So we just need to print start. Right. So outer loop is iterating five times when I is one. So this J loop will be executing one time because here you can see condition is J less than equals to I. So I is initially one. So this loop will execute one time and one star will display then inner loop is completed. Then printf will display this backslash and then I will be incremented. So I will become two. Now in this case, this J loop will be executing two times. So J loop will start from one and it will go till two, like I is two. So two times two asterisk or two stars will be displayed. Once this inner loop is completed, then cursor will shift to new line. And then I will be incremented to three. So next time this inner loop will be executing three times. So three stars will display on output screen. So if I execute this code, so here you can see the result. So in first row one star in second row two star, then three, then four, and then five. So this way, if you want to implement nested loop, so you can apply these logical things. So I hope you are able to understand how I changed these conditions. So outer loop is executing five times, but inner loop is totally dependent on value of i. So whatever value of i is, that many times your inner loop will be executed. Okay, so this was the triangle where we are starting one, two, three, four, five, and so on. Now if you want to print its reverse, like in first row, you want to display five stars and four, three, two, one. So basically, if you see this code, so here only one line is modified. First outer loop, or you can say outer loop, right? So outer loop is modified. So what we need to do, we just need to start this i with five, then i greater than equals to one, and then i minus minus. So if you modify this outer loop, if you modify this outer loop and you start this outer loop from five and decrease it to one, then your reverse asterisk pattern will be displayed. So if I run this code, so you will see the result, five, four, three, two, one. Now what is happening here? So initially i is five, five is greater than greater than equals to one. So condition is true. Then control will shift to this inner loop, which is j. So j is starting with one and it will go till i. i is basically five. So initially this for loop will execute, inner for loop will execute five times because i is five. Once five stars are printed, then new line cursor will move to new line, then i minus minus. So i will become four. So next time again, this j loop will start with one and it will go till four. So total four times this loop will execute. So asterisk will be displayed four times. Right. So this way, if you want to implement nested loop, so you just need to implement outer loop and decremented order and rest of the things will remain same as the previous example. Right. So this is the easiest way to understand this. Like if you want to print reverse triangle for any triangle that you have implemented. So try to modify only outer loop and you will be able to see the results. Right. So this way I hope you understood the concept like whenever you want to implement any loop. So it depends like whatever you are doing increment or decrement operation. So there is no foundation. You can do anything and there can be more than one logic available to implement solution of a particular problem. Right. So this way, basically we implement nested loop and C programming. So I just explained you two examples. Right. So this this was the first example in which stars are increasing. This is the second example where stars are decreasing. Now I have one more example where we have triangle but before triangle we just need to print some spaces as well. So if you see at the bottom there is no space for second last row we have one space for third last row we have two then of here three and then for first row we have four blank spaces and then a stress. Right. So here what we need to do initially we need to print some spaces and then we need to print star. So here basically you will be using three loops. So first loop is I which is counting row quantity like which row you want to display then J loop is basically for space and K loop is basically for asterisk. So here you can see J loop is basically starting with one and it will be terminating with five minus I so initially I is one initially I is one so five minus one is four. So it means what will happen this J loop will be executing four times it will start with one and it will terminate till four. So four times it will execute and four spaces will be displayed once four spaces are displayed and this inner loop is terminated then we have one more loop which is K. So once execution cycle of J is completed then only K will start its execution and it is having termination condition as J less than equals to I. So I is one so initially first time K loop will be executing one time. So what will happen four spaces will be displayed and then one asterisk. Once K loop is terminated so printf will display backslash and then control will shift to I plus plus so I will become two then you can see again this J loop will execute. So this time condition is five minus I and incremented value of I is two. So the result will be three. So it means J loop will be executing three times. So three spaces will be there and value of I is two. So this time K loop will execute two times. So after three spaces two stars will be displayed. So in the second row we want three spaces and then two stars. Right. So this way this code is working. So what we need to do here I'm going to add one more for loop. So it is J equals to one J less than equals to five minus I and J plus plus and inside this loop we just need to display a space and I'm creating one more variable which is K and here instead of J we are going to use K. Right. Rest of the logic will remain same. So initially I loop will start its execution. So it is starting from one condition will be less than equals to five and then I plus plus. So I is one so this then this loop will start its execution. So five minus one is four. So in first go this loop will execute four times and this K loop will execute one time because I is one. Right. Once it is terminated then cursor will shift to new line. So in first rotation four space and one start will display then I will be incremented to two. So here we have follow where five minus two means three. So three spaces and then I is two. So two stars. So in second rotation three spaces and two starts will be displayed. And remember these two for loops are not nested. These are independent but these two loops are nested for the first one outer loop. So here in this case we are having two inner loops. So first inner loop is J and second inner loop is for K. Right. So I hope with this way you are able to understand the logic. Now if I click on run so you will be able to see the result and you can see this is the result which we were expecting. So we need to display this and so there is some issues with this compiler. Okay. So I executed it again. So here you can see the result. So this time it is showing the correct results. Yep. So here you can see the output is shown correctly in decreasing order. So in first row we have five star then in second row we have one star. Sorry one space and four star then two space three star four space two star and sorry three space two star and four space and one star. So this way if you want to display your results of pattern of a stress in reverse order. So you just need to modify this first loop and accordingly your result will be displayed. Right. So I hope with this with these four examples you are able to understand the complexity of nested loop. So in C++ in Java in apex programming also sometimes we need to implement nested loops. So for that purpose you should understand how basically nested loop works in C programming. Right. So I hope with this explanation you are able to understand how nested loops are implemented in C programming. And if you have any question, so you have different platforms to ask. So we have telegram group. We have this YouTube where you can write your questions in the comment section and do practice whatever I demonstrated in this session so that you can become export in implementing nested loops. And like after the session I will be modifying the exercise sheet and session tracker. So I will give you some reference videos those those are already available as prerecorded. So you can just go through that as well if you want to understand things in detail. Right. So this was about nested loop that I shared with you. So do practice whatever I discussed so that you can also understand the implementation of nested loop. And please share a review or feedback about the boot camp whatever efforts I'm doing for you. So it will give me some motivation and like right now three boot camps are running one is for Salesforce, one is for C and C++ programming and another one is for Appian. Right. From next week onwards, I'm going to start one more boot camp which will be on Omni Studio and it is related to Salesforce. Right. So in that boot camp you will be learning everything about Omni Studio that is basically used for industry clouds. So I think with these four different paths you are able to understand whoever is attending whatever boot camp which is giving benefit to you. So right now four and soon a few more will be available. So I'm in discussion with the experts because everything I don't know. So I'm discussing with the experts so that if someone is available and he or she can deliver some content. So let's see how things goes. And if I have some experts, so surely they will be live on this platform and will be sharing their knowledge. Okay. So with this note I take your leave. Thank you so much for joining this session and if you're watching the recording. So thank you for watching and do regular practice whatever I am explaining in these sessions and these sessions are very much important for folks who are not familiar with the programming and those who are starting their BTEC program. So for BTEC students these all C and C++ boot camps will be helpful. So if you are anywhere connected with anyone who is starting their BTEC journey, so you can just share this YouTube channel link and I'm sure like they will be benefited with this. With this note, thank you so much for joining this session. See you in the next session.