C_38 While Loop in C (part-2) | C Language Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the series of learning programming in c in the previous video i have discussed while loop basics about why loop the general syntax of while loop or the working of while loop with the help of flowchart as well as we have seen a program the basics right now we can do many things with while loop it's not like that whatever syntax we have written in the previous video that is the only way to write while loop no in many ways you can write while loop and see this whatever i'm going to discuss in this video that would be really helpful for you if you want to explore if you want to you know learn in deep about these looping statements about while loop right because from this part whatever i'm going to discuss in this video generally they ask questions in placements in interview and any competitive exam right those kind of questions sorry the program would be given a code would be given small code four or five lines and you are asked to tell output of that code so in that thing in that in those questions they will not write down the you know simple while loop this is the general syntax and just write down while loop and ask you what is the output of this program no they will do some other kind of you know you can say those questions would be tricky they will write while loop in different different ways and you think that this is not syntax of while loop so this must be an error but no that will not give you error that will definitely give you some output maybe not that output you want but maybe some abnormal behavior that will show but it will not give you error so those things i will discuss in this video right so please watch this video till the end but before that just want to tell you one thing if you want to improve your coding skills clear your fundamentals or get placed then an academy with code chef has brought you the best of resources to help you achieve these goals here you can access free classes from the top educators who themselves have done the journey you want to do you can see the list of the educators here and their achievements i think these are the best people you can who you can learn from and here you have access to many free classes that would be helpful for everyone from for for beginners as well as for those who are who know some coding and want to be expert in coding as you can see here the list of batches one more thing one more batch pinnacle 2021 is going to be started from 18th of january it is going to be very helpful for all level of coders in this batch they'll start from the discussing the data structure topics the most important topics of data structure then they'll move to the advanced topic like grady algorithm dynamic programming discrete mathematics recursion these kind of things and this is one year long batch after successful completion of the batch you will get industry accepted code chef certification so here you can see the full schedule and the batch preview so it's right time to invest smartly in structure learning free courses as well as paid subscription by an academy and code chef so watch these amazing free lectures first and then if you like it go for their paid subscription and remember to use the code jkl10 to get 10 percent extra discount on your paid subscription all the details and all the links i'll put in the description box of this video you can go and check out so now let us discuss in how many ways we can write while loop see first of all what was general syntax of while loop we write while this is a keyword here we write some condition termination condition here this is what here i am going to write some statements or you can say this is body of loop right and here only we write what that modify or update expression you can say that increment or decrement and after that that's it the initialization is before the condition right all the three things which is important for any lupus initialization condition termination condition and modify and update expression or increment or decrement in for loop we write all the three things at one place but in while loop we write different different places here initialization then condition then within this body of the loop we write that incremental decrement right this is general syntax of while now see if we write something like this i starting from 1 i less than 10 printf i am going to print i and then i plus plus then what output you you will get here 1 from 1 it is going to start 1 2 3 up to 9 it will print it will not print 10 because condition is i less than 10 not i less than equal to 10 so 1 to 9 it is going to print right this is what according to the syntax i am going everything fine here according to the syntax but suppose if i write something like this now condition is i am putting i equal to equal to 1 so first see i would be initialized as 1 in the memory that this is i condition is i equal to equal to 1 this is the equality condition i am going to check is i equal to 1 yes condition is true then control will enter here and i value would be printed on output screen you will get 1 now plus plus i you can write i plus plus or plus plus i here in this case both are same means i becomes now 2 again going to check the condition right i equal to equal to 1 is 2 equal to 1 no condition is false directly control will go out of the loop and what would be printed now end of program that would be on your screen one and end of program right now this is what equality condition but some somewhere it is also written something like this i equal to one now what output you will get c at starting i is 1 here i equal to 1 this is what assignment operator you are using this is not any condition assignment operator means the value of the right hand side whatever the value that would be assigned to the variable that is on left hand side now 1 would be assigned to i right now i becomes 1 again it is 1 but again it is assigning the value so it is 1. now here we have 1 means true always if here other than 0 you write anything 1 2 3 minus 11 minus 12 anything then that would be considered as true if you write 0 then only it will be false so now 1 means true it will print i that is 1 would be printed now i plus plus i means i becomes 2 now again check the condition condition is what i equal to 1 again assignment operator so in i i am going to assign again the value 1 so y value becomes again 1 right condition is true again it will print 1 i plus plus means 2 again check the condition but it is i is equal to 1 again it will become what 1 right so now again it will print one so now what do you think is it going to end no it would be in finite loop because condition will always be true so it will print in finite time one one one one on your screen right so please take care of this thing double equal to and single equal to if you want to check the equality condition double equal to you have to write here right and if you write here something like this see same if i write here something like this while one you are doing you are you are doing what hard coding the while one you are writing in your program only this condition will always be true right if you write here like 10 or 1 suppose 10 is here so while 1 condition is 2 10 would be printed on your screen plus plus i it becomes 11 right now while 1 condition is true now 11 would be printed on your screen i plus plus 12 while one condition is true you are not giving any termination condition it is not going to end so it will print what 10 11 12 like this it will print so it would be an infinite loop and see what it will print it's not like that it will it is going to print till infinity yeah it will print within a range within which range the range of the integer range of integer is what minus three two seven six eight two three two seven six seven so once it will reach till three two seven six seven it is not going to print three two seven six eight three two seven six nine then what it will print see this is what circle of signed in so once value becomes 3 2 7 6 7 it will print what minus values minus 3 to 7 6 8 minus 3 to 7 6 7 like this minus 1 then again 0 then again this this would be an infinite loop but values would be printed within this integer range only right you can see the output now after running this program but but single difference is what i am putting here i not 1 i am just putting i here now you think that you are not giving any condition here so it would be an infinite loop but not it would not be an infinite loop so now where it is going to end see here i is 10 suppose i is 10 it's starting while i i means 10 anything other than 0 considered as true 10 is true so 10 would be printed i plus plus i means it becomes 11 now again 11 that is also true 11 would be printed 12 again 12 is also 2 12 would be printed 13 and this is going to uh you know it is going to like this 10 11 12 13 14 but hey what where it is going to end once the value would be reached till 3 2 7 6 7 like it is going to print if you want to print vertical then you have to use slash n here otherwise it will print horizontal so now after that some time it will print at one time it will print three two seven six seven after that i plus plus means it will become minus three two seven six eight so minus three two seven six eight it would be printed again minus three two seven six seven and till like this minus three minus two minus one now at one time value of i becomes minus one plus plus i now it becomes 0 so now here the value of i is 0 now check while i is 0 0 means condition is false now it is going to stop means now it will print what end of program till minus 1 it is going to print right so it is not in finite loop please take care of this thing still you are not giving any condition here you are not giving any condition but still it is not infinite loop it is already a condition because at one time definitely i value would become zero and once i value becomes 0 it is going to end i hope you are getting these concepts right next thing what you can do with this is suppose i am not writing here this plus plus i i am not writing any modify expression here so now what will happen i is 10 so now first what value would be printed here i am writing i only so 10 would be printed 10 10 is true 10 would be printed no plus plus or minus minus i right again control will go here i is what again 10 10 is true again 10 would be printed i is what again 10 10 would be printed so now it would be an infinite loop here also i am using i only difference is what i am not incrementing that i if you will not increment that i then definitely i value would be remain same that is 10 10 is always true so now it would be an infinite loop and it is going to paint 10 10 10 infinite number of times right next is what suppose i am writing here this condition i less than equal to 10 and i am not initializing this i here i am doing i plus plus here condition i am writing modify expression i am writing i am not initializing this value of i so now i don't know what is value 5 right so now what it will print i less than equal to 10 we do we don't have any i value starting value so it is not going to print anything it will just print end of program now in this case i have some doubt so you run this program and tell me in comment box what output you are getting here if you are writing something like this no initialization i less than equal to 10 condition is given and modify expression is also given right so now here just only you will get end of program right next thing what you can do here suppose you don't you don't write any condition here i is equal to 1 but no condition here but here it will give you error it is not a valid syntax right it it it will give you compile time error right next is what you can do suppose here only i am writing i plus plus and i am removing this i plus plus from here and i is i am writing i 0 right now what output you will get here see at first time i is 0 right now you will think i plus plus means i becomes 1 and 1 is true so it will print 1 no it is what post increment post increment means what is the funda first the original value of the x of that variable would be used in the expression after that value will be modified original value of i is 0 0 would be used here for the condition now 0 is what falls so it is not going to enter into this loop it is directly exit from the loop and what it will print end of program just end of program right in case i am writing here ah this this plus plus i now now it is what pre-increment pre-increment means first the value of that variable would be incremented after that the incremented or the updated value would be used either it's incremented or decremented whether you are using here plus plus i or minus minus i right now i am using here plus plus i so now value becomes 1 first now 1 would be used for this expression now 1 is what true so it is going to print 1. now again here i am not writing anything so again it will check the condition right plus plus i means it becomes 2 now 2 is also true 2 would be printed again plus plus i means 3 3 is also true 3 would be printed right now tell me is it in finite loop or not no because this is is integer variable we have some range of integer in that range we will somewhere sometime at one time we will also definitely going to reach to 0 because i am incrementing i so 1 2 3 up to it will print this then minus values then till minus 3 minus 2 minus 1 after that plus plus i means it becomes 0 it is going to stop till minus 1 it is going to print so you can see the output after running this program right now next is what um you can also use while loop with cad data types if you use character data type here then what output you will get see and see tell me one thing this is your assignment if i write semicolon after this while loop in that case what output you are getting you have to tell me in comment box right now i am doing this with character i am using while loop with character i hope you know the range of the character that is from minus 1 to 8 to 1 to 7 right this is the program right now character value that is a first of all the variable name is this one and a is stored not exactly a is stored here the sky value phase 97 that would be converted into binary form and that would be stored in memory right but here just for the understanding purpose i am writing a here while ch ch is a a is 97 that is true ch would be printed here so means percentage d ch so 97 would be printed here sky value phase 97 right right next is cs plus plus means value becomes 98 so now here you can say 98 would be stored 98 98 or you can say b that is also true so now percentage d we are using so 98 would be printed then 99 something like this but it is not infinite loop right why so because range of integer is what the range of character is what 1 to 7 then after that 1 minus 128 minus 127 minus 126 something like this right this is signed character right am not using any new signed or unsigned keyword here so that is considered considered as signed character so now value becomes now at one time value becomes 127 okay 127 would be printed after that plus plus means it is not going to be 128 it is going to be what minus 128 minus 127 minus 126 and minus 1 till minus 1 right now after that cs plus plus minus 1 after that value becomes a 0 so now here zero zero means false and now control is going to exit from the loop and that this is output and this is the output you are getting it is not infinite loop here right but suppose here if you are writing percentage c then tell me what output you will get this is your second assignment you can consider it as an assignment write percentage c and tell me what output you are getting in this case right again if you are doing something like this and it is not always that you have to do a plus plus here you can also write minus minus here same case suppose you are storing a here a means 97 97 is true 97 would be printed suppose i am using here percentage d 97 is printing here ch minus minus 97 now it becomes 96 not 98 96 is also true 96 would be printed here now it becomes 95 95 would be printed here something like this right and now at one time till 1 till 1 it is going to print right now minus minus 1 is 0 so now it will print till 1 97 to 1 in reverse order that's it and here we have an error after get set you have to use semicolon right you have to take care of these things and here also you can write down some constant positive or negative like minus 11 i'm writing 11 i'm writing or simply 0 or 1 i am writing or here you can use any condition like relational operator and as well as you can use that logical operator also two conditions you can use right so here i am writing logical end ch less than one one 1 127 and i equal to equal to 0 i is one more variable i am taking their their integer i right so if both the conditions are true then only it will consider it as a true if i am writing here i is equal to is equal to 1 but i is zero here so this condition will always be false so if this is false then ultimately it is false and it is not going to enter into this while blue body not even uh one time right because condition is at first time only i can the condition is false so it is going to exit from this loop so here you can use two or more conditions using some connector logical connectors also right relational operator you can use or any variable you can write here simple variable any constant value you can write here right and you can also do what nesting of while loop one while loop within this we can you use another while loop another while loop that that is nesting right or you can use any if else statement within this while loop like here i am writing some condition while within this while i am rooting writing some if or else conditions based on the output you want based off based on your requirement right that that thing we can also discuss when we will discuss some programs when you write some programs using while loop or for for loop right so now this is what i guess that said but you can also do some experiments i i cannot tell you everything here this is what i can just give you and give you an overview these kind of things you can also do so try out different different things with while loop and see what output you are getting right and if in any questions you are getting any problem then you can ask me in comment box or you can uh ask me on your my instagram account you can message me there right so now i will see you in the next video till then bye bye take care you
Info
Channel: Jenny's lectures CS/IT NET&JRF
Views: 21,055
Rating: 4.9626751 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, C language tutorials, C jennys lectures, programming in C lectures, loops in C programming, while loop in c, introduction to c language, data types in C, history of c, arrays in c, how to write c program, preprocessor in C
Id: ocX2FN1nguA
Channel Id: undefined
Length: 20min 7sec (1207 seconds)
Published: Mon Jan 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.