#16 While loop in JavaScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is david reddy and let's continue with this series on javascript in this video we'll talk about loops now see we'll divide this into two parts in the first video we'll talk about what is loop and what are different types of loops available and in the next video we'll see which loop to use at what situation okay because that's a tricky part we have three different loops to work with okay so again we'll talk about what is loop first but then we have three different loops and people do get confused between which one to use when and that we'll see in the next video so if you know loops and you just want to know which one to use you can directly jump to the next video but if you want to understand how loop works let's get started so basically in javascript we have three different type of loops we have a while loop we have a do while loop and then we have a for loop again what is loop we'll talk about it as of now let's start with what is loop so let's say i want to do something repeatedly and that's what loop means right you have to do something repeatedly example every day you take a bath every day you work every day you eat twice in a day but you do that repeatedly right every day that is what we call as a loop right so let's say i have a log statement here and in this lock statement i'm printing hi now if i want to print high only once i can simply write it once right and then when you run this code of course you will get high once what if i want to print i want to execute this statement three times or five times okay three times is easy you can simply copy paste this you can say copy paste and paste of course you will get this three times okay so we got three times right what if you want to do this five times of course you will say hey copy paste two more times what if you want to do this 100 times 200 times thousand times of course this doesn't make any sense right and then we have a very famous principle called as dry which is do not repeat yourself don't write the same statement multiple times can we just use one statement and run it multiple times we can simply say hey you know we can just say let's keep that in a block okay so that's how you get a block right so let's put that in a block and say hey repeat this statement five times that's it if you do this and if you run this code you got high only once you know what happened is because we have created the block but then we have it a comment here javascript will not even execute this comment but if you write it without commit of course this is not logical statement or a syntactically correct statement right you have to write the same thing but in a proper format which your javascript understands so to write this we have to use a type of loop so the first type of loop we are going to use is a while loop now see when you say repeat this statement five times you need certain things here first how do you know how many times you have done that so first we need a counter right so once two times three times four times five times right we need a counter here and every time you have to increment this counter you have to say one two three so you have to go ahead right and then you have to also check what is the current value because you don't want to go beyond five you don't want to go to six or seven eight times you want it only five times so we need three things the counter you have to check for the condition and we have to also increment it right okay so three steps so we call that as initialize and then the next step would be condition and the third step would be increment right so we have these three steps so the first step is you have to first create a variable which will act as a counter so we'll say we can use any variable name doesn't matter actually but logically let's go with i because i means increment okay so we'll say i equal to initially one that's how you start okay and then uh if you remember last time we mentioned we'll use semicolon at the end now and then we also need to check for the condition now can we use condition like if yes we can do that right because we want to just for the check for the condition so we can use if but then if we'll exit only once right that block executes only one so if you say if i should be less than equal to five because that's the limit i want to go for but then if will exit only one so instead of using f we will be using while because while is a loop it will get repeated so you can see that while english word which means repeat you have to check for the condition okay it will execute this every time this condition is true but then we have to also make sure that you also increment so example when you print high once you have to make sure that the i value goes to 2 so that it will again print high then to make sure that the i value goes ahead with three that's how you do right you say hi hi hi hi hi so what are you doing here you're checking for the you're incrementing it you're checking for the condition why i stopped it here if i have not went for the sixth number because condition okay so we'll say i plus plus remember we can increment a value with the help of i plus plus you can also say i equal to i plus 1 but then we have seen this right this is amazing shortcut any guess what this is called use that comment section and answer that what is this called okay and the answer is it is post increment we can also use pre-increment here because when you only have increment not assignment both behave the same way okay will this work let's try so let's go back here and run this and you can see we got high five times it's working right that's how your while loop works so basically we have three things here we have initial value we have a condition and we have a increment or decrement see there is no compulsion that there should be always a increment okay there is a different way you can do that example if i don't do increment here that's fine you just have to change this condition maybe there has to be some other way of checking for the condition because if you don't increment it will not go ahead and it will simply say true for the entire lifetime in fact we'll make sure that your machine hangs if you do that so let's not do that let's do this three steps so that's how your while loop works right next we have one more loop which is called do while loop so what do while loop does is we have okay there's one more thing i want to show you let's say the value of i initially is six now what will happen when your value of i is six and then it will check for the condition right is six less than five no it's false the minute you get false it will directly jump out which means the block will not get executed even once let me show you that so if you go back and if you say run we have not got any output because the block is not getting executed okay so if you go back to one now let's see what is happening uh let me just run this code you can see high five times the i value is one here it will go inside it will check for the condition is one less than five yes it will go inside it will print high so we got high once and then it will say i plus plus that means it will increment the value of i now the value of i is two so it will check is two less than five yes it will print high and then it will go ahead it will make i value three again it will repeat uh it will make it four and then five at one point the condition will be false and it will come out that's how it works now the question for everyone and the question is what if i print the value of i here what is your guess what will be the value of i so again pause the video and since i'm asking multiple questions in each video now you can specify the timeline so that it will be helpful for others so we are printing i what will be the value of i here i hope you have answered that and the value is 6. now that's weird right we are saying the value of i should be stopped at 5. see what is happening is after several increments the value of i would be let's say 4 okay in this case it will print high it will increment the value the value of i becomes 5 it will go up is 5 is less than equal to 5 yes it is equal to 5 it will print high it will make the value of i six okay then it will check is six less than equal to five no that's false so we got false but what's the value of i it is six right and that's why we got six cool okay one thing to remember we have three steps here initialization condition and increment okay you can also do reverse here okay example if you want to start from six or you can start from five so this time we are doing reverse in fact what i will also do is before doing that let me also print the value of i so it will print high one high two and let's do that and you can see we got high one high two high three high four and high five oh that's one that's amazing high five okay but then what if you do reverse so i can say i equal to five and then i is greater than equal to 0 because this time we are going in reverse order so i will go from 5 4 3 to 1 i count down and then instead of using increment you have to say decrement because every time you want to reduce the value of i five four three two one let's try and we got it so you can see we got five four three two one zero that's tricky we got six it's because which is five four three two one zero okay so we should not be going till zero we should be going till one so ever you can make this as one that's one way of doing it or keep it zero remove this equal to symbol so when you say i should be greater than zero it will stop at one okay so you have two choice let's run this and you can see we got five four three two one cool so that's how we can do increment and decrement okay but one thing to observe what if you say uh let's say this is zero okay okay let's keep it is five and we'll go back to our original way which is less than equal to five and we'll say plus plus so in this case of course we are going for increment basis now and let me set this value of i here as 10. now what do you think it will even print it once and then says no it will not print you one once right because that's how while loop works so while loop will first check for the condition then only it will go ahead so what if you want to just execute it and then check for the condition so in that case you can use do while so in do well what you do is you check for the condition later so after the curly brackets at the end you will check for the condition now what it will do is it will say okay the value of i is 10 that's fine do says okay go inside it will check okay we are printing the value of i and then we are saying i plus plus it will make the value of i as 11. it will come down and say okay 11 is less than equal to 5 no just go out of the loop that's it but it will print the output at least once that's the thing behind do while but yes if you start with one there is no problem it will behave the way it was behaving before so do while and while they're almost same the only difference is so in while you first check for the condition then you print the block or you execute the block in do while it is reverse first it will execute the block then it will check for the condition so that was do while and do while we have one more right which is for loop so let's do that in the next video it's already lengthy video so let's do the for loop in the next video again we have a separate video where we talk about which is the best one to use at what condition so that's it for this video bye-bye [Music]
Info
Channel: Telusko
Views: 11,440
Rating: 4.9106145 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, blockchain, django
Id: h84MlHv6g4Q
Channel Id: undefined
Length: 11min 24sec (684 seconds)
Published: Sun May 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.