C_43 Need of Nested Loops in C | Nested For loop in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the series of learning programming in c we have discussed loops four while and do while loop as well as break and continue statements right now in this video i'm going to talk about nested loops basically what is need of using nested loops what is this nesting of loops why you need why you know do a nesting of loops in your program right and basically we will uh see nesting of for loop in this video right we will also see nesting of while loop nesting of do i look but in later videos so nested for loop i'll be talking about in this video everything about nesting of loop nesting of for loop with their syntax general syntax with working off that nested for loop with the help of flowchart as well as with the help of a program right and some other important points about nesting of loops that also will discuss right this video is brought to you by an academy we all know how important is to have a great teacher in our life so here i want to introduce you to few great teachers at an academy you can see the list here their name and their achievements their credentials these are the people who have some of the things that you want to and some of them are doing what you want to do so they are the best people who you can learn from and they have also started a batch for replacement preparation to create coding interviews named placement 2021 it's a six month loans placement centric batch with live sessions and here you will get unlimited access to programming content as well as practice problems this program is going to cover every topic in detail and after successful completion of this course you will get an industry accepted code chef certification also in this box they are going to cover all the topics which are very important for placement point of view so it's time to get serious and start learning as you can see the complete schedule here as well as you can see the batch preview so so if you are ready to get a good placement then you should check this batch thoroughly and remember to use my code jkl10 to unlock free classes as well as to get 10 extra discount on your paid subscription so if you are interested you can go for it all the details and the link i'll put in the description box of this video you can go and check out so now let us discuss nested loops in c first of all we will discuss what is need of you these nesting of loops in c the reason right the requirement of these nested loops then we will see then we will discuss what is nested loops for nested for loop nested while loop nested while loop right then you will get it better so see let us take one example if on a screen you want to print print suppose a star one star simply what you will write here you will simply write printf and this one obviously you know you write the complete program header files void main and within main function you let this uh this thing this printf it's not like that just write down this this line and you will uh you'll be able to print the star right now okay that's it now i say i want to print five stars in this line now how you will print now maybe you are smart enough what you will do in printf we will write five stars okay fine you can print five star by this method also now i say print for 50 stars 100 stars thousand stars in this line then i think it would not be you know good to write down 1000 stars in this print f and you you cannot write right and if you write then it's not a good practice right so now what you can do here we you we will use loops if you want to repeat something then we use loops i have already discussed introduction to loops in that video what is need of loops if you want to execute some set of statements repeatedly multiple times then put those statements in loop rather than just writing multiple times those statements right here i want to print 5 or 50 stars then maybe you you can write one printf and 50 stars here or maybe you can write 5 50 printf statements but that is not a good idea right so better to put this printf because i want to repeat this printf this statement multiple times or here five times so better to put this statement in a loop here i am using for loop because i love for loop you can do it with while or do while also so simply we take i hope you know the syntax of for loop here initialization one variable will take i less than equal to five five times i want to repeat i want to repeat the statement and then i plus plus right and that's it if you run this then you can print this star five times 1050 times then you can see you can write down here 50. just you need to update this thing 500 times then you can write down here 500 that's it right or if you don't want to you know update this thing in your program at compile time so what you can do another method is here we can take i less than equal to n and at the run time you can ask from the user enter the value of n and if you enter 50 then it would be repeated 50 times if you enter 500 then it would be repeated 500 times right that is another way but here suppose i am doing that hard coding i am simply here i am writing 5 right now see ok now you can print this using this i have used a loop here now i want to print these stars these lines should be 5 or 10 or 6 suppose like this now i want to print these five stars how many times one two three four five six times six line now what you will do one method is we can see using this for loop using this process i can print this line five star right now maybe you can do for this line this loop for this line another 4 loop or this line another 4 loop maybe you can write 6 4 loop right but that is also not a good practice suppose i want to print i want the number of lines should be 50. so it's not like that you will write 54 loop here right that is not a good idea so now what you can do here see pay attention here what you are doing you are going to repeat this line this line six times one two three four five six right so here also you are repeating something repeating means you can again use a loop for repetition right now for printing this means you want to repeat this process the printing of these five stars this process you want to repeat six time and this is what means this process you want to repeat six times because using this process only i can print these five stars in in this line in one line right so i want to repeat this process six times so put these statements all these statements this process in a loop and that's it because obviously we put what whatever you want to repeat what statements you want to repeat we put those statements within a loop and i want to repeat these statements so put these statements within a loop again right so now what you can do here simply write so this was the process so put this process in another four another loop i'm using a again for loop so here also the what is the syntax you have to take another variable i we have taken so we cannot take i again take another variable name you can say j or anything abc but mainly with the loops we use i j k because i j k these are three girlfriend of girlfriends of this loop right now i is equal to 1 i less than equal to now how many times you want to repeat this process 1 2 3 4 5 6 less than equal to 6 sorry here we have j j less than equal to 6 and j plus plus and put this complete process into this outer for loop this one right so this is what nesting of loops what is nesting we are using loop inside another loop so loop inside loop that is what known as nested loop or nesting of loops so basically now you you are clear why we are using this nesting of loops basically when you want to print these type of patterns or with matrix or 2d arrays multiple dimensional arrays we are going to use these loops nesting of loops right so now what does the process see now process c first of all i am not writing the complete program j we have a j value is 1 right 1 is less than equal to 6 true control will enter here first step is this one then we are going to check the condition then third step is we are going to enter now in this again we have one variable i i is 1 right is 1 less than equal to 5 yes condition is true again we are going to enter here right i guess you can write down the numbering in those steps i am just explaining the working now here what is their printf string now star will be printed right now what we'll do what is the working of loop we are not going to go to this j plus plus why because we are inside this loop now so now control will go to i plus plus now i becomes 2 condition trio yes again enter again print star again i plus plus 3 condition to you again it will print i 4 condition 2 again it will print i 5 condition tree again it will print i 6 is this condition true no now control is going to exit from the loop now where control will go we are still inside the outer loop so now control will go to here j plus plus now j becomes 2 is this condition true yes again control will enter into the slope now again i becomes 1 right again i would be initialized to 1 right 1 is less than equal to 5 yes condition true again it will enter here right and it will print c it will print star here only but i want to print this in separate line so what you can do here after this this for loop inner for loop here you can just write down printf for a new line we write slash n and that's it now once control will exit from this i plus plus after printing this 5 star control will go to this line the statement which which is written immediately after this for loop that is slash n slash and means cursor will come here right now j plus plus right now j becomes 2 again condition 2 i becomes 1 again this process would be repeated right now again this would be printed here the second line right after exiting this again slashing now control will come here in the third line now j plus plus now j becomes 3 then 4 then 5 then 6 6 is also true so six times when i becomes one sorry the j becomes one then this line would be printed then j becomes two then j is three four five and j is 6 right now this this outer loop is what i hope you are getting outer loop is what to print these lines how many lines 6 lines but actually you are printing this star within this inner loop now how many times you have to print star within this line five times so that is why that will do the inner loop will do the printing of the star if you want to print the star ten times so here 10 times then you have to update here i less than equal to 10. if you want to print these stars in a line 50 times then here 50 but here suppose you want to print the number of lines should be 10 then in which loop you are going to update in outer loop because these lines number of lines are taken care by this outer loop and actual printing would be done with this inner loop now i hope you got it right now see if j is one then how many times then this inner loop would be executed five times j is two again it would be executed five times right j is one one two three four five five times j is two one two three four four five inner loop would be again five times so now after loop would be executed here six times and inner loop would be executed how many times six into five that is thirty times right i hope you now got the working of this nesting of loops right now see the flowchart of this working of this loop we will see many programs printing the pattern program we will see using that id also now what is the working here you can say here you can write start also this is what this is outer loop right and this is what inner loop or you can say this is external loop this is what inner loop right or general syntax if you write let me write down first of all the general syntax four here i think you can write initialization condition and then that increment or decrement or update or modify this upper arrow means increment or down arrow means decrement i hope you can write down it you you should write down it proper i am just writing writing down uh you know small forms because i don't have space now it's not like that directly you have to write down other another loop here it depends on your requirement right here you can write down some set of statements also right maybe some printf statements or if else block anything you can write down after that you can write down for loop that is also nesting of loops it is not compulsory after within this loop just directly you have to start another for loop no before this for loop you can write down some block of statements then you can write down this form right now suppose i am writing here inner for loop initialization condition increment or decrement right this is what inner loop statement block right and here suppose after this i am writing some statements that i am i am calling outer loop statement block and that's it this is what general syntax of nesting of four loop now what is the flowchart of this thing see this is what first of all we are going to check outer loop loop condition if outer loop condition is not true then we are not going to enter into this loop and this one loop will never get executed right if this condition is false then it is not like that we are going to exit from all the loops no if this condition is false inner loop condition is false then we are going to exit from inner loop only still we are left with outer loop that increment or decrement outer loop iterations we are still left if the outer loop condition is false then only we are going to exit from both the loops right so this is first of all we'll check after loop condition if this is true then maybe you can write here some statement of blocks or i am not writing these here so first of all now we will check inner loop condition right if in the loop condition is also true then here what inner loop statement block s b means statement block this would be executed right after that after executing of this what will happen control will go to increment or decrement or here you can say i am just writing update expression after updating again check what inner loop condition not outer loop condition we are still in inner loop right and if inner loop condition is false then where control will go if this condition is false then control will go to outer loop statement block first of all so now here what outer loop statement blocks should be executed whatever you write there right after this one what now this update what if updated modify expression of outer loop so here update expression of outer loop here you can write update expression of inner loop right after updation now what will happen now it is going to check the outer loop condition again if this condition is true again this process would be followed if outer loop condition is false if this is false then we are going to exit from both the loops here suppose after this i am writing after these loops i am writing statement abc so now control will go to here to the statement abc or statement xor and you can write here so this is what i go i guess you got the working of this loop with the help of flowchart i can i guess you can draw this flowchart now right now see some more important points about nesting of loop is what nesting can be up to many levels four within this another four within this four one more four one more four four four four you can do nesting up to multiple levels many levels as per your requirement second point is it's not like that you can you can do nesting of same loops no you can do nesting of different loops also like if you are writing here 4 within this 4 i can write while loop within again this while loop i can write do y loop right so one type of loop we can do nesting of different different types of loops right i hope you are getting my point third is what it is also you know a case that in this for loop i can write down one for loop is this another four loop another four loop another four loop one two three four five as many for loop as you want that is not nesting means that is not multiple nesting one for loop here writing here i'm writing one for loop and another four the third for loop i'm writing i'm not writing that for within this inner one i'm writing here this is one block then second block for second for loop third block for third for loop this can also be a case this can also you can write down here also you can write down some statement here also you can write down some statements right so now i hope you got what is need of nested loops in c and you got nested for loop here the working right now in next video i will be talking about nested while loop in c and we'll see that thing you know with a proper example with the proper program right so now i'll see in the next video till then bye take care
Info
Channel: Jenny's lectures CS/IT NET&JRF
Views: 63,054
Rating: 4.9418387 out of 5
Keywords: data structure tutorials, operating system, data structure and algorithms, jayanti khatri lamba, jennys lectures, jenny data structures, jennys lectures DS, jenny lamba, jennys baby, jennys lectures baby, data structures
Id: 5v1w0V4GPrE
Channel Id: undefined
Length: 19min 50sec (1190 seconds)
Published: Tue Jan 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.