hey what’s going on guys tan Maya for Telesco learnings and in this video tutorial we are going to be seeing a use case of the do-while loop so this is the third basic looping controller statement that we are going to be seeing in this video in the previous couple of videos we’ve seen the for loop and we’ve seen the while loop and the do-while loop is different from them because it is an exit control statement wherein the condition is checked after the first time the entire loop is executed so you’ll understand that in a minute right now
let’s just start off with the syntax so the syntax goes as follows you write and do the new open and close the curly braces that are the body of the do-while loop and at the end of the curly braces wherein you have the closing curly brace you write while and then you open and close the round brackets which will contain the condition and then you give a semicolon so don’t forget to give the
semicolon over here and what we are going to do is we’re going to print my name or print any text five times so this is just to display or display the working of the do-while loop so what I’m going to do is I’m going to say where x equals to five okay and inside the
the condition part of the do-while loop what I’m going to say is while X greater than five okay so the condition is X should be greater than Phi if X is greater than five only then this loop is going to be executed but we know that our X is 5 let’s say if X is one okay so X is 1 and X is not greater than Phi right so this loop should not execute however as I mentioned since this is a do-while loop
this condition is gonna be checked after the first iteration so if I see a document not right and in the h2 tag if
I print my name this will run one time okay so let me just see you this and if I refresh this there you go you can see we got the output done by once even when the condition was false and the reason is that this is an exit control statement wherein the condition is checked after the first time or first iteration now if I make this six and if I reference this will go in an infinite loop because we are not performing increment or decrement over your right so we need to do that so here what I’m going to do is I’m
going to say X minus so what will happen the first time X is 6 and the first time the body of the do-while loop will execute even without checking the condition so this will get printed now X will get minus which means X will become 5 so the fire is not greater than fire right so it will not be executed so
let’s just make this seven and let’s see if our name gets printed two times and there you can see our name got printed
two times now if I make this zero still one time it is going to be executed anyhow because as I mentioned the condition is checked after the first iteration so this is the only specialty of the do-while loop and the variation compared to four and while loop so if
you are wondering where exactly the do-while loop is used sometimes what happens is even though the condition is false for
the first time you have to execute that command ok so let’s say you are displaying a menu wherein you will be giving the user some options and depending upon what option the user selects you will again loop through the different sub-options or subtypes so in that case
we have to show that menu first-rate so depending upon whether the condition is true or false at least you need to show that menu so that can be done in a do-while loop wherein you at least display the first screen and then if the condition is false or true or depending upon what user enters then you can exit the loop or you can continue in the loop so again this was just a syntax delusion this was not a program which was specifically for do-while loop we rarely come across such programs when in the do-while loop is very essential so I just wanted to show you the syntax and what exactly do while loop does how it looks and how it differs from other loops ok so that’s it for this video guy I hope you understood the syntax of the do-while loop
and how it is different from the other two loops if you liked this video please give it a thumbs up do share it with your friends and see you guys in the next video peace