C_41 Break statement in C | C Language Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the series of learning programming in c we have discussed loops in c four while and do i loop in the previous video i have talked about proper the differences among these three loops four while and do while loop right now we are left with two most important topics or you can say that control statements in c break and continue these are very important in c these are control statements or you can say these are break and continue these are what keywords in c and keywords we always write in small letters that is why we write break and continue in small letters if you write in capital letter then it will give you error these are also known as control statements right why we use now in this video i will talk about break statements and see everything about break statement why we use break statement in our program when to use these statements right and we will see with the help of a program also like when you use break statement in your program then then what what process what how the program would be executed what output you will get right and we will also see some important points about this break statement and this video is brought to you by an academy an academy has launched plus an iconic subscription now what is this plus subscription this will give you the full full access to all the courses by the top educators for your specific goal here you will get structured classes interactive live sessions doubt clearing sessions as well as quizzes just to track your performance and if you are not able to take live classes then it's fine you can also watch the recorded session for that class and you can enroll as many courses as you want under a particular goal and what is that an academy iconic subscription in this subscription you will get everything which is in plus subscription as well as some extra what you will get now what is that extra personal coach study planner and bi-weekly reviews dedicated doubt clearing space personalized test analysis study booster classes and study material but apart from this on an academy you will get many amazing free classes special classes so you must take these amazing free classes which is one of them is an academy combat which is a fortnightly battle so use my code jkl10 to unlock this battle and academy combat as well as if you go for any paid subscription then use the code jkl10 to get 10 extra discount on your paid subscription so the details and the link i'll put in the description box of this video you can go and check out so now what is break in c it is a control statement in c or you can say break is a key word in c that is why we're writing in small letters right and if you want to exit from the loop early then we use break statement that whatever i am saying uh that you will come to know that you will get it better when i'll write a program right now you need to take care one more thing about break you can use this break with loops and switch statement this thing i have discussed you can watch that video in the side button how to use break in switch statement and when you use break and switch then what is the meaning of using break in switch statement this thing we have already discussed now we will discuss how to use this break in loops see we can use break within loop or switch it means if you in any program if you use it uh suppose within if else block no loop and no switch is there then it will give you error if in a simple program suppose addition of two numbers and there you are writing just break then it then it will give you error right so you can use this within loops either within loops or switch that you need to take care right and small letter you have to write it in small letters now have to use it in loops when whenever a break keyword will be encountered in that loop then control will automatically exit from the loop right and what statement would be executed the statement you write immediately after the loop those statements would be executed right now whatever i am saying let us discuss it with the help of a program suppose a program is i want to enter some integers right like it will ask you the program will ask you enter integer i mean entering 1 then 2 then 5 then 6 then 7 right suppose i want to execute this program only five times means i want to you know i want that user should enter only five numbers and then it will print some of all these five numbers right here it will print some of these five numbers right and i want one condition i want to put if the user enter any negative number suppose minus 1 minus 2 or any negative number at that time it is going to exit from the loop means after that once you enter negative number see i want to you know that user should enter 5 numbers suppose i am entering 1 then 2 then minus 5 then it will not ask for two other entries it will exit from the loop and it will print some of these two numbers only 2 plus 1 3 right so this i want to implement in a program i know how many iterations exactly five iterations i want means exactly i want user should enter five numbers so i know exactly how many iterations five iterations so here we can use four loop right we can we will also write up this program with while loop first of all write down this with for loop see now first we will take any number any variable where we are going to take these numbers and i variable for this loop now i am writing a for loop for i is equal to 1 initialization i less than equal to 5 and i plus plus 5 number i want now here in this i am asking from the user enter a number and i am take i am going to take the input with scanf i hope you know percentage d what do you like percentage d and address of where i want i'll store this in this variable a right now if the user enter any negative number you have to put this condition also so now what i can write here if this a is because number is going to store in this variable so now the a is negative now so a is less than 0 if a is less than 0 then simply write down break within this if you can use curly braces also but i am only writing within this if i am only writing break that is one statement so no need to write down these curly braces just for readability purpose i am writing curly braces break is within this if otherwise it will print sum so one more variable i am taking sum and sum plus a right otherwise it is it is going to repeat till 5 right and after that i will the sum percentage d and sum that's it right and here also if you will not declare this sum it will give you error and at starting i am initializing this with the 0 because sometimes it will take some garbage value at starting and after that it will add these numbers or it will give you some other output so better to initialize the sum with zero right now i hope you can write down those uh header files and void main i'm not writing the complete program now control will go the first line to the main function so in memory we have declared three variables right and in sum i have zero within these we don't have anything now for loop i is equal to 1 so i is 1 i less than equal to 5 yes condition is true control will enter into this loop now on output screen suppose this is your output screen what you will get the message enter a number now enter a number suppose i am entering 1 right so now scanf i have entered number 1 it is going to check this condition is this a because in a now i have 1 is 1 less than 0 no so it is not going to enter into this for loop so it will go to this statement sum is equal to sum plus a now sum is 0 0 plus 1 is 1 so now in sum that value would be assigned to sum now sum becomes 1 right now we we are not going to exit from the loop now what i plus plus now i becomes 2 condition is true again enter into this loop again it will ask enter a number now suppose you are entering number 2 or i am entering number 5. is this condition true no so again sum plus a is would be assigned to sum now sum is 1 now a is 5 now 5 plus 1 is 6 so that would be assigned to sum that is sum becomes now 6 again i plus plus right now again i plus plus is now 3 so 3 is less than 5 so it will ask again enter a number and now suppose i am entering number 2 right so 2 is not less than 0 so program will run till 5. so 5 time it will ask again suppose i value is 5 so 5 times it will ask right and we have again i plus plus 6 6 is not less than 5 so it will out of the loop it will go out of the loop out of the loop we have written printf sum sum it is going to print now what are some of these numbers 18 so now it will print what 18 right now see suppose here after entering these three numbers whenever it will ask enter a number i am entering minus 10 now see minus 10 it is going to check this condition a is less than 0 in a i have now minus 10 right is this less than 0 yes it is true now it is going to enter into this if statement now in if we have written break break means it will show you out from the loop from any loop you have you have written this in for loop so it is going to throw you out from this four loop so here once break is encountered directly control will go out of the loop it is not going to execute any other statements which is written after this break in this loop so now it is going to print just some some is what some of these three numbers only right and what is this 8 8 is it it is going to print so now i hope you know i want that loop should be executed till 5 times but this is what early exit from the loop only three times this is going to be executed after that it is going to be out of the loop that is why if you want early exit from the loop then we use break statement one uses this one second one is we don't know how many times i want to execute this loop in finite times maybe at one time i just want to enter five numbers and print the sum maybe second time i want to sum ten numbers maybe third time i want to some 15 numbers i don't want i don't uh you know know the exactly the number of iterations right so in that case we cannot use for loop it's better to use while loop right yeah we can use for loop no need to put any condition then it would be at what infinite loop and using break you can ah break this loop at any time but in this case we use better to use while loop so let us see that program so here i am taking two variable because this would be an infinite loop so i'm not taking any uh that variable for the loop like i or i plus plus i'm not doing any i plus plus or i minus minus because it should be an infinite loop right while one this condition is true so it will ask from the user enter a number so i'm going to enter suppose 1 now in av i have 1 is a less than 0 no so it is sum plus a sum is 0 this is 1 0 plus 1 is 1 so it is going to assign into this sum that is sum becomes now 1 again again we will check the condition while 1 because it is always true again it will ask a number and suppose i am entering suppose 2 is this condition true no so sum plus a now a is 2 2 plus 1 3 so that would be a sign is in sum right in finite time it is going to ask you 10 15 20 100 times until you enter a negative number suppose i am i am after entering 2 third time it will ask you enter a number and there i am entering minus five this condition is true a is less than zero yes now it will execute this break statement break means it will throw you out from the loop this is body of the loop so the control will immediately will go where the statements which is written immediately after the loop we have written this printf sum is equal to percentage d sum so it is going to print 3 next time maybe you can enter 10 numbers and after that you can enter negative number if you will not enter negative number then it is not going to end right it is going to be an infinite loop because you have done hard coding here while 1 this is always true so to break this one you have to enter a negative number at that time it will print the sum of the numbers you have entered 5 10 or 15 numbers right you can write down this with do while also that says that is what exercise for you you have to write down this program with do while loop and you can write down that program you can copy paste that program in comment box section or you can uh send me that program on my instagram account also instagram messages also right so now i hope you got what is this meaning of using break statement in c right in some programs when we write programs later then we will ask after executing something we will ask do you want to continue and if continue then press y if no don't want to continue press n and if you have pressed n or if suppose you have pressed 0 then it will go out of the loop it will exit from the loop in that case you will we will use break statement right so what is the meaning of using break statement i hope you know now it is clear to you we can only use this with loops or switch we can we cannot use with if and else here see don't say that within this if you are using break yeah with this if i'm using break but ultimately this break is what within this loop if you will not write any loop and if you are simply writing if and there if you're writing break then it will give you error so i think that's it about break if you have any doubt in this break then you can ask me in comment section so now in the next video i will talk about continue statement in c so now i'll see in the next video till then bye bye you
Info
Channel: Jenny's lectures CS/IT NET&JRF
Views: 20,237
Rating: 4.9083471 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, jenny lecture, c language, break in C, break in C language, jennys lectures vlog, jenny lecture vlog
Id: PLgJsMEPKec
Channel Id: undefined
Length: 15min 15sec (915 seconds)
Published: Sun Jan 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.