18 - ITERATIVE STATEMENTS PART-1 - C PROGRAMMING

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome to our Channel so in the previous session we'll learn various decision control statements and multiple selection statement so in that we have seen simple if statement defense l simple adder and switch case now in this class let us have a look on it right two statements iterative statements so here the main concept is if we want to execute some set of statements continuously for n number of times that we call it as iterations and the other way for this a try to statements is loops so simply a loop is executing the same set of statements never friends and here there are mainly three loops one is while loop do while loop and followed so in every loop we will execute some set of statements continuously so that's why we call it as a loop so here before going to this loops individually the three important things to be executed in each and every loop so first one there must be a loop variable initialization so we have to initialize one loop variable one variable that is per loop executing a number of times next second one updating this loop or that means increment or decrement thing of loop variable incrementing or decrementing of loop variable and the third one is so how many times the question is how many times these statements must execute so there must be some termination for this execution if there is no termination automatically the loop will continues infinitely that means the control lever comes up from the loop it will continuously execute it that means it will go to the infinite loop so we have to specify a specific termination so further we require some condition some condition eliminating from so these three concepts are these three things we should follow while executing these rules so in each loop without execute these three things one is we have to initialize one loop variable second we have to update frequently this loop variable and the third we need to specify a condition where the loop must terminate so if this condition fails automatically the look we'll go to the infinite loop that means infinite number of ten there will be no stop of executing the similar set of statements so it will go to the infinite loop so for that purpose we need to give the condition now let us see one by one having to this while loop so before going to the while loop what is the importance of why we have to write the loop so let us let us take an example if I if the problem statement is printing off yet natural numbers that means I if I want to print some numbers from one to hundred so this is the problem statement given so I want to print the numbers from one to hundred so up to this situation we had a knowledge that we'll have to use a primitive statement for displaying the output or explicitly so for that purpose we need to implement printf statement hundred times for getting the result so in order to get the result we need to implement this printf 100 times that means 100 printers will have to write it in the program so hundred lines of code will be there lines of code hundred pips so in order to avert these lines of code we can write a sing printf statement single print of statement and executing the same statement hundred times hope you soon so our problem statement is printing their numbers from one to hundred so according to our knowledge till now in a C language so we know that printf is used to print the output on the display screen so for that purpose here we have to write hundred ten tips so hundred printers should be later so in order to avoid these number of lines we are just finding a printf statement and executing the same printf statement hundred times this is called loop for this purpose we need to use a loop variable update the loop variable and write the condition where the loop must be terminated hold on a suit the exact usage of loops now let us have a look on while you the syntax for my rupees why somebody's a keyword so here we have to write the condition so when the loop must be terminated so it is also a block of statements so we have to write curly braces whenever we are writing the block of statements we need to use curly braces right the statements to be executed use the increment and decrement operator given taking or decrementing loop variable and close the block of statements so this may be a single statement there are multiple statements depends upon the province chain next will be followed by some statements if required so this is not a part of the loop it is outside them so this set of statements so all these set of statements will be executed repeatedly until the condition becomes false so here we are writing the condition as we discussed that foundation is a relational operator usage of a relational operator will be two variables fine so if you use a common condition we have to use a logical operators the concept is same so the final result of this condition is either 1 or 0 right if it is a simple condition we have to use a relational operator so that the results may be 1 or 0 if it is a compound condition we have to use a logical operator even though the result will be 1 or 0 that means true or false if the condition is true the controller will enter into the block and all these statements will be executed and if we really after come to this again it will go to the condition and again once next time the condition will be checked and if the condition is also true then again the statement will be executed and immediately after coming to this closing process that again the control checks the condition if the condition is true then again the statements will be executed so this process will repeat until this condition becomes false so how this condition will becomes false that means based upon one increment or decrement of variable so incrementing a decrement of loop variable the condition will change by iteration by iteration so these number of problems these statements execute so number of rounds this block of statements execute each round we call it as intervention so if this lot of statements are executed five times executed five times so we call it as five iterations so previously I have said that example that printing a one two hundred numbers so for that if we use this while loop then the number of iterations are hundred iterations number of rounds number of times this block of statements executed so each time we call it as iteration so these loops are corn iterate two loops these loops are called iterative loops iterate two statements so hope you understood so whenever this condition is failing false immediately the control will not enter into this block of statement the control will come out from the loop and the next sequential execution start from the statements just below the room so that's why soon here the settlement X is not a quarter from a quarter of value this is outside the loop so if the condition fails then this statement will you from here the sequential execution starts so until this condition fails this block of statements will executed repeatedly now let us have an example so that you can clearly understood so let us take this common example there is printing of one two hundred natural numbers so if we want to take if you want to print one to n natural numbers let us have a loop variable as I given a decrement operator I plus plus and the condition so where the loop must stop so why here we have to write the condition so that up till the condition is true the block of statement will be executed so the condition will be I less than or equal to hundred so if s is our loop variable so let us take ideas have a loop variable I have initializes the loop variable is equal to 0 I have started the loop while I less than or equal to hundred so the car does the block of statement will execute until this condition becomes false so we have to play the I value percentage be /en high so that and now we have to increment and decrement the elevate loop it so this K minus I plus plus or I is equal to like respond so anything we can write anything any statement is equal to a plus 1 is turn out I plus plus is cut so closer now first I is equal to 0 check the condition 0 less than or equal to 100 to 0 less than or equal to 200 is true so the control will enter into the loop printf statement % HDI value will be printed as 0 now I press press so I becomes 1 and immediately whenever the control reaches the close of this it will check for the condition so here I value no 1 so 1 less than or equal to hundred condition is true again printed first and SD I know it will be printed as well next I plus place I becomes 2 now 2 less than or equal to hundred printf authenticity I so will be printed and now I plus plus 3x3 so write similar way these statements execute and I becomes hampered so let us take 99 is printed 99 is printed I is equal to 99 so I plus first that means hand write is are equal to 100 condition is true because we will have use this less than or equal to so again three to the hundred I plus plus 1 0 1 I value is 1 0 1 now 1 0 1 less than or equal to hundred now the condition phase now the condition fails now the control will doesn't enter into the loop the control will directly jumps out from the while so our output is from zero to maintain the high value from zero to hundred so this is the output we need according to the problem statement hope you understood this value after getting this example so we can restrict this Hamlet not only 100 we can take a variable yes en and we can read from the keyboard that means using the scanner function read a value for y N and write the N here if you are right there if you read the N value as 10 the output will be 0 2 and if you read the manga some read the output will be 0 200 if the N value we have read that is housing the output will be 0 mm right so hope you understood this a while loop and let us see the second loop that is doing the second loop is doing so this is also similar to the window here also the number of iterations will be done until the condition becomes false so there is a slight difference between a while and doing so now what is the difference so in the window first to the condition will be checked and if the condition is true then only the control will enter into the block whereas in this duel the name itself indicates first the control will enter into the loop at the end of the loop the condition will be checked so if the condition is true then again the control will enter into the block or it will come out from the loop it will not it doesn't enter into the loop that means either condition is true or false at least one time the condition that the block of statements will be executed the why will the do-while loop statements will be executed whereas in the while loop when the condition is true then only the block of statements will be executed that means then only the control will enter into the loop so at the beginning itself the condition will be checked and if the condition is true then only the control moves on to the row here at the end of the loop the condition will be checked so that's why it is called exit control loop and while loop is for entry control so entry will be the restriction and the entry level itself the restriction will be and here at the exit level the restriction will be so let us write the syntax so that you can clearly understand do so here do is also a keyword so please refer to the previous video keywords so that you can clearly get the different keywords do is a keyword and why it is also a keyword do statements interviewed the set of statements number of statements it may be singular statement or available lines of treatment the group of statements so increment or decrement loop variable so you know also we have to follow these three steps so we have to follow these three steps first we have to initialize the loop variable we have to update the tube variable incrementing and here we we have to check the condition or we have to place the foundation for the terminating them look exiting from the increment of the comments so here the increment is not only the I is equal to I plus 1 I plus plus so here we can update into 2 also that means we can replace this increment and decrement loop variable as updation update loop variable right don't confuse it so we can we have to update the loop variable so not by 1 it may be by 2 or it may be by 3 any any way we have to update the loop variable so simply we can write update am pleasure blue paper it may be updated by one or updated by two or updated by three any any any number of switch so so here also sorry no you have to check the condition so here we have to place semicolon after the very condition so this is the syntax fault so this is also similar to the while loop do fallen statement will be executed so that control we enter into the loop and it will execute these variables some statements and there's the loop variable and at the exit level the condition will be checked and if this condition is true then again it will go back to the statements that means if we again enter into the loop if the condition fails it will come out from the loop so carefully while condition semicolon this is very very important this is very very important how to place a semicolon in the conditions of my condition so here also the institution of these statements will be done until the condition becomes false but if the condition is false in the first iteration itself the statements will be executed it takes one time in the domain because first we are executing the block of statements and then at the exit level we are checking the condition so that's why at least once the block of statements will be executed in the do a statement now let us have an example the same example let us change the printing of n natural numbers that means 0 1 200 of 0 to 100 so that we can write the logic using the way so same do all right initialize I is equal to zero negative percentage D /n i now i Christmas why I less than or equal to 100 here see first is initialized to 0 next entering into the loop printf / t so i will be treated as 0 now i press press 1 1 less than or equal to hundred finish this truth so 1 will be printed again 2 2 less than or equal to hundred to printed 3 printed and so on 19 mine if it is 9999 is printed 99 incremented so hundred hundred less than or equal to 100 so how that will be drinking and so condition is true 100 less than or equal to and the condition is true again the loop will be the control will enter into the loop so printer i so hundred right so 9999 is printed now is press hundred so hundred is less than or equal to hundred so condition is true again without the control will enter in the loop 25 so hundred will be displayed 100 plus quest that means 1 0 1 so 1 0 1 less than or equal to hundred so condition fails so will come out from the so hope you understood this while and doing a simple difference between violent dubai that is so both loops will be executed until the condition becomes false but the difference is in the way you look at the integral the condition will be checked and if the condition is true only then only the block of statements will be executed in this divine first the control will enter into the loop and and they exit the condition will be changed and if the condition is true then again the cathode will move on to the loop statements so let us stop here hope you understood this session so if you are having any doubts please post your thoughts in the comment section so so that I will clarify your doubts and in this session we have seen two loops to iterate two statements that is while and Dubai so in the while loop and the beginning yourself we have checking the conditions and if the condition is true then only the control will enter into the block and in the blue end the condition will be checked at the exit level so the control will first the control will enter into the loop and there at the end of the loop the condition will be checked and if this condition is true again the control will enter into the loop if it is false then again it will come out from the blue so that is why while loop is called entry level loop entry control room and dual spot exit control loop so in the next session let us have a look on the one more record two statements that is Fargo so let us stop here and if you liked my video like it and share the video with your friends and subscribe to my channel for getting more videos thank you very much
Info
Channel: Sundeep Saradhi Kanthety
Views: 92,373
Rating: undefined out of 5
Keywords: sundeep, saradhi, kanthety, c language, c programming, cp, computer, computer programming, loops, iterations, iterative loops, while loop, do while loop, while, do while, for loop, loop variable, condition, rounds, condition termination, loops example, while exammple, do while example
Id: vPfu5lXnEl8
Channel Id: undefined
Length: 26min 4sec (1564 seconds)
Published: Thu Aug 31 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.