 Hello, everyone. I am Sanjay Gupta. I welcome you all on Sanjay Gupta Tech School. So today we are having day 12 of CNC++ coding bootcamp. And in today's session, I will be discussing about Do-Why Loop. So in last two sessions, we discussed about For Loop and Why Loop. So basically in C programming, there are total three types of loop available. We already discussed two types. So those were For Loop and Why Loop. So today let's complete third one as well. So today's session will be a little bit short because Do-Why Loop generally we use for very rare situation. So with the help of example, I will try to explain in what scenario you will be using Do-Why Loop. Okay. So moving forward, so this is about, this slide is about me. So if you are watching any video on this YouTube channel for the first time, and if you want to know about me so you can go through with this slide. Everything, whatever I am doing, whatever experiences I have, everything is written on this slide. So quickly moving forward. So be consistent because consistency is the key and do regular practice. So you can see in every session I am doing some practice, I am implementing solutions at my own. So you can also follow all the sessions and do some practice. And you can join this telegram group and follow Sanjay Gupta Tech School on YouTube, LinkedIn, Instagram and Telegram and all the links are available in the video description. And session tracker link that is very important is available in the description. Okay. So now let's talk about Do-Why Loop in detail. So first of all, we need to understand the syntax of Do-Why Loop. And it is quite interesting because you will see like initially we have initialization in the top and then we have do then inside curly basis we have we have to write all the statements. And then we need to write increment and decrement. And at the bottom, we will be writing the condition. So that makes it different from for loop and while loop. So basically in for and while loop you saw we we used to put a termination condition in the beginning. But in case of Do-Why Loop, this termination condition we need to write at the end. So what will happen? This Do-Why Loop will be executing at least once because once the execution is completed, then only loop will be able to test the condition. Right. So that's why it is a little bit different with while and for loop. Okay. But the three things that we used in for loop and while loop, those are same here as well. You need to initialize, you need to increment and decrement the loop. And then you need to apply the condition. The only difference is condition will be available at the bottom. So now you might be thinking like what is the benefit of this Do-Why Loop? So the benefit of this Do-Why Loop is like in any scenario, you if you are already sure like if loop will be executing at least once, then you don't have any problem. And second time onwards, loop rotation will depend on the termination condition. Right. So in such scenario, like wherever you want to execute statements at least once and then next time onwards, statement execution will depend on the condition. So in that scenarios, you can prefer Do-Why Loop. So here you can see I have an example for you that I will be implementing as well. So first let's go through with this example. So here you can see I have two variables s and n. So s is initially zero and n is declared. Then ch, which is sorry, we have a variable of type can name is ch. Right. So this is another variable. Now we have do while loop implemented. So what we are doing inside Do-Why Loop? We are using printf scanf. So basically printf will be displaying the masses enter a number. Okay. So with this message, you will be entering a number and that number will go into n variable. Then that n variable will be added into s. So whenever you will execute this code, so at least one number you will be entering and that number will be added into s. Now next time onwards, like if you want to add one more number, so for that you are asking like add another number. So user will be entering y or n. If you want to add another number, you will press y. If you don't want to add another number, you will press n and that input will be stored in this ch variable. Right. Now what will happen once the one rotation is completed for second rotation? We need to check this condition while ch double equals to y. If value of ch is equals to y, it means condition is true and the user want to add one more number. So then only this loop will repeat. Otherwise it will be terminated. So in the in such scenarios where you want to execute your loop at least once, no matter whatever is the condition. So in that scenario, you can prefer Do-Why Loop and for second rotation condition will be tested and if condition is true, then only your loop will repeat its next rotation. Okay. So now this loops termination is dependent on the user. So if you are if user is entering y always, so it means user will be entering numbers and it will be added into s. And if user enters, no n if user center n. So in that case, this y condition will be false. So loop will be terminated and at the end, you can see print of statement is available. So some will be displayed on the output screen. So this way, you will be able to implement Do-Why Loop. So let's implement it in the compiler so that you can understand the execution. So first of all, I am creating variables, right? So I just created s and n in these are variable, then CH is off type car. Then inside do I'm writing the statement enter a number. So that number will be stored inside n variable, right? Now, after inputting that number, what we need to do, we just need to add that number with s. So initially s is zero. So that number will be added into s. Now, we need to ask, do you want to add another number? So here I am writing yes or no. Then we can scan it, right? So what will happen? You will be entering capital y or capital n. So it is it is the limitation. If you enter capital y or capital n, accordingly, it will run, right? Now here you can write while CH double equals to capital y and then semicolon. Semicolon is mandatory, right? In this syntax, because here you are ending this loop. So in for loop and while loop, we don't use this semicolon. But in case of Do-Why Loop, you need to use semicolon sign, right? And then what you can do, you can just display some equals to percent d and s. So this way you will be able to implement for loop. Sorry, Do-Why Loop. So at least one time this loop will execute and second rotation will depend on the value whatever you are providing yes or no. Okay. So now I'm going to run this code. So it is asking for a number I entered 10. So it didn't ask for y or n. So here one catch. So basically what happens whenever you are entering 10 and pressing enter key so that enter key is basically received by this scanf. So what you can do here you can write backslash n in scanf so that it will receive enter and then whatever character you will be typing it will receive. So let's see whether it will execute or not. So I'm just trying it now. So I'm entering 10 then enter key. Yeah. So now you can see it is asking for y or n. So I'm entering y and pressing enter. So it is asking one more number. So I'm entering 20. So again you can see it is asking for yes or no. So I'm entering yes. This time I am entering 30. Again it is asking for yes or no. So I'm entering capital N so it will terminate and showing the sum. So I entered three values 10 then 20 and then 30. So that is why it is showing some as 60. So you saw like first time loop is executing. It is reading a number then it is asking for choice. But second time onwards if you are entering capital Y then only loop is repeating if you are entering capital N so loop is getting terminated and once loop is terminated. So with the help of this printf some will be displayed on screen and whenever you are reading character just after integer. So in that case in scanf you can apply this backslash n because after entering 10 you are pressing enter key. So enter key is like treated as a character. So before reading y or n you can just write backslash n so that this code works properly. So I hope with this explanation you are able to understand how do while loop basically works. Now if I move forward so we need to understand the differences between while and do while. So with this comparison you will be able to understand like in which case we need to use while and in which case we can use do while right. So it is very simple. So while is basically known as top tested loop and you can say entry control loop as well. But in case of do while it is bottom tested and exit control loop. So I think with these words you are able to understand the difference. So whenever you want to test condition first and then you want to enter in the loop you can prefer while loop and in the cases like you want to enter in the loop you want to execute it for one time and then you want to test the condition so you can go with do while loop. So in case of while loop if the condition is false first time then loop will not execute. In case of do while if the condition is false first time then also loop will execute at least once because whether condition is true or false it is checked at the end of the loop. So no matter condition is true or false at least one time your do while loop will execute. Okay. So these are the differences. So if you are BTEC first year student and you are learning C programming for the first time. So this is very important for you to understand like in which scenario we need to use while loop or in which scenario we need to use do while. Right. Now moving forward. So this was about do why now we have two more keywords. Those are important if we talk about loop. So first keyword we have break. So if you want to terminate execution of a loop in between so we can use break keyword. So here you can see we have a code snippet which will execute this loop five times because loop is starting from one and termination condition is I less than equals to five. So it is a for loop and inside for loop we have a condition if I double equals to three and then we have a break with if we don't have curly braces. So if we'll have break with it right and print f is not along with if so if this condition is true if I double equals to three then loop will terminate because of break. Otherwise this print f will execute. So let me implement this code here so that you can understand the execution. So we need to declare I then I equals to one I less than equals to five. Now inside this for loop we have a condition I double equals to three and we don't have any curly braces and break is with if then here I'm going to display value of I. So if value of I is three then loop will terminate. Otherwise value of I will display on output screen. So if I run this code so output will be one two because when I is one so this if condition is false. So nothing will happen and this print f will execute. Then next time I will increment by one so I will become two. So if I is two so again this condition is false. So break will not execute and this print f will display two on output screen. Next time I plus plus will perform and I will become three. So in this case I is equals to three. So break will execute and loop will be terminated. So anytime if you want to terminate execution of the loop. So inside loop you can use break with if condition. So whenever that if condition will be true. So your loop will be terminated automatically. Right. So this is about break keyword. Now next we have continue keyword. So it is also important. So what happens here in this case if this condition is true. So continue will shift your loop rotation to the increment part. Right. So if I use continue here instead of break. So what will happen. So when I is one so continue won't execute. So I will be displayed on output screen. So one two both will be displayed when I will be three. So this continue will execute. So what this continue will do it will shift the loop execution to this increment part. So in case of I equals to three this print f won't execute and I will become four when I is four. So this condition is false. In that case four will be displayed. So basically continue won't terminate execution of the loop. Instead it will skip one rotation of the loop. So output of this code snippet will be one two four and five three won't be displayed. So if I run this code so you will see the output. So you can see output is having one two four and five. Right. It is not displaying three because in case of three continue is true and continue automatically shifts loop rotation to this increment part. Right. So this is about break and continue which are very much important if you are learning loop. Right. So this is all about today's session that I have planned. So in these three sessions those I delivered this week. So I just delivered how we can implement for loop how we can implement why loop and how we can implement do value in next week sessions I will be explaining you how we can implement nested loop and understanding nested loop is very much important because in most of the programming languages we use nesting as well. Right. So I will be explaining those nested loops as well in next week sessions. Right. So thank you so much for joining today's session and if you are watching the recording so I hope whatever I explained in today's session will be fruitful for you. So just try and practice that and I will be updating the practice sheet as well practice doc. I will copy all the examples which I demoed in the sessions so that if you are following that document you can also practice. Okay. Thank you so much for joining today's session. See you next week with some new topics.