hey what’s going on guys tan Maya for Telesco learnings and in this video tutorial of JavaScript we are gonna be taking a look at the while loop control statement so in the previous video we saw the for loop and we saw a practical program based on for loop so if you have missed that video you can check it out in the previous video of this playlist so with that being said let’s start off with while loop now while loop is again similar to for loop because it is an entry controlled looping control statement which means that the condition is checked
First however as I mentioned in the previous video for loop for loop is used when you know the number of iterations okay so when you know specifically how many times you want to perform an activity you use the for loop so the while loop is preferred or used when you don’t know the number of hydrations but you know that a particular condition is going to be true for a certain number of times so I’ll explain to you in detail but let’s just first see the question number one and we first type in the code for the while loop so you can see we have to print fire numbers using while loop okay so this is a very simple question let’s start off with the syntax so you type in Y so that’s the keyword opening and closing round brackets and the body of the while in curly braces so inside this unlike for loop we don’t have three different parts we only have one single condition and until that condition is going to be true this while loop will execute itself so now we have to print fire numbers first fired numbers using while loop right so I’m just going to initialize one variable over here I’m gonna see where X and I’m going to give the value of one so one is our very first odd number right so that’s why I give it one and now in the conditionwhat I’m gonna say is X less than equal to ten or i can also say X less than ten because the first five numbers are going to be one three five seven and nine right so they are going to be very well under ten so that’s why I give X less than ten now inside the while loop what I’m going to do is I’m just gonna print out the x value so I’m going to say document dot right in the h1 tag andthen
I’m just going to append the value of X close the h1 tag and see you this so what I did over your is I’m just printing out this x value but now you can notice that if we just try to print this and if you just try to run this much code our program is gonna go into an infinite loop and the reason is because we are not doing anything to increment the x value so in for loop remember we had initialization then we had the condition and then we had increment or decrement part also right so that was taking care of the implementation but here how do we have to manually do that so below this I’m just gonna say X is equal to X plus 2 now the reason why I did X plus 2 is because you want to go from 1 to 3 3 to 5 5 to 7 so the increment is of two units right not just one you can also write X plus equal to 2 over your that is also totally fine because it is one in the same thing and now if I save this and if i refresh our browser you can see we got the output 1 3 5 7 9 so let me just comment this out and you’ll understand how it goes an infinite loop if i refresh this you can see that it is refreshing forever and this is basically your program going in infinite loop and this will crash your program so nothing will work on the website so this is another case of infinite loop so you have to completely stop this open a new tab and again you have to run the entire file and they will you get back your original answer so now coming back to the difference of while loop and for loop so the program that we did over
you can also be done using for loop right so where is why loop going to be used to consider an example where you want to load some data so you have some data coming in and you don’t know the number of lines or number of rows of data that is going to come in so every time is going to be different so in that scenario, you don’t know how many times you want to print that data you want to store it somewhere oh you have to display it so, in that case, you can use while loop wherein you can give in the condition that till the rows or till the data is coming in keep on executing or keep on displaying it another example can be let’s say a user is inputting some value and depending upon what he inputs you want to print something or you want to keep on doing something so let’s say he is inputting an alphabet And when he clicks a
you want to do something but when he clicks X you want to stop the process so in the while loop you can see that when that character that Ian does becomes X only then you stop the process otherwise you keep on going so that can be one example right now since we haven’t still
seen the Dom manipulations where we take input from the user I am NOT able to give you a very particular example very only while loop will work but as we move ahead in this series we might see a program where an only while loop will work and then you’ll understand the difference between for and why otherwise as I mentioned both of them are entry controlled looping control statements well in the condition is first checked so if this X less than 10 was not true this statements wouldn’t have been executed at all but in exit control loop statement that is the do-while loop which we will see in the next video what happens is the loop executes one time for sure even if the condition is false so you’ll understand that in the next field so yeah that’s it for this video guys this was a very short video on while loop I just wanted to show you the syntax as we move ahead we take more complex examples we’ll combine all the fundamentals that we learned and we’ll write more programs so the idea will be
more clear so that’s it for this video guys if you like this video please give it a thumbs up to let me know in the comments of this video was mu share it with your friends and if you haven’t yet subscribed to this channel make sure you subscribe so that you get notified whenever I upload a new video so thanks for watching see you guys in the next video peace.