Java script Basics
Java script do/while Statement.
Hello guys welcome to our series Java script. In our last segment we saw about switch statement in Java script, we saw switch statement is better than if…else…if statement. I hope you have practiced the practical which we performed during switch statement’s segment, if you have any questions or queries during your practical or our last segment then tell us about it on forums.learnvern.com we will revert back with solutions to your questions and queries.
Let’s move further in our today’s segment, our today’s segment is based on Loops in Java script. Basically speaking or in laymen’s term if we consider something in circular motion we can call that as loop, for example if you have a long wire, if we arrange that wire in circular motion then that wire becomes collective and it will occupy less space but if we again open it then it will again come in a line format.
When we move it in a circular motion the shape which it attains is called as loop, loop means onecircular motion then another circular motion then again circular motion, means the motion keeps on going round and round, the wire creates one circle after another circle like this it moves in multiple circular motion. So if we consider some other example other than wire, let’s say if we consider a point and we move in a circle we come at the same point again we reach the same point after moving , if we are reaching the same point again and again, the shape which is getting created we call it as loop, means we are moving in loop
We have same concept in Java script also, in laymen’s term loop means we start from one point and again reach at that point only and again and again we repeat the same process. Now we will discuss what does loop means in Java script or any programming language.
So let’s say basically, Java script loops are used to iterate the piece of code using for, while, do while , for-in and there is one more for-of. So here what is happening, we have a piece of code means some lines are written, iterating means repeating that line again and again, if we are executing any piece of code again and again so that is called loop.
Here it is written like for loop, while loop, do-while loop, for-in loop, for-of loop, these 5 techniques helps us in moving loop. Usually in other programming languages only for loop, while loop, do-while loop are available. Instead of for-in in python or Java we have for-each loop. In Java script we have for, while, do-while and two extra loops are provided by Java script i.e. for-in loop and for-of loop.
So in our today’s segment we are going to see total 5 loops, today’s topic is very important because whenever you are creating website apart from that if we are developing a web application, then our iteration increases. For example, if we are creating an application for bank, so we don’t know how many customers bank is having, if I have to write for one customer then I can write a 5 to 6 line code but if I want it to execute for every customer, then I won’t write that line of code for each and every customer, I will write it one time and it should be executed for each and every customer, to make that possible loops come very handy.
With help of loops we can make this possible that one thing should be executed again and again, we will start with for loop, understanding for loop is very easy, in for loop iteration, as it is written in first line- the Java script for loop iterates the elements for the fixed number of times means in Java script our iteration executes for fixed number of times in Java script. Now question is what is fixed number of time? For that we have written a syntax, which is like this- firstly, for after for in parameters, here in total three parameters are passing, first parameter is initialization, means this for loop will be executed how many times, for that we will keep an initial condition here.
So for keeping condition we need a variable or starting value, let’s say I want my loop to start from 1 and move till 10, means it should go in rounds 10 times, or start from 0 and move till 9 means again go in rounds for 10 times. If I want to print A B C D then it should go in rounds from A to Z.
If I want to do something like this then my very first value should be initialized, so in for, in bracket first will be initialization, after that condition comes, means this loop should be executed how many times? Means as many times my condition proves to be true those many times my code to be executed will execute.
Means, of course in any loop the most important thing is condition because loop will execute that many times only as many times condition will be true. Let’ say I have written such condition in which the loop should never break, in that case my loop will go in infinite loop means that will run for infinite times. So what will happen then?
My web application will come at for loop and it will not execute further codes only that much portion will keep on executing until and unless we break it or system breaks it. So we should be careful that such condition should never come as a programmer when we work when we write for loop or any loop we should make sure that condition should be false at one moment so that will break, in order to break the loop being false of condition minimum once is necessary. So after initializing, the condition should be proved for fixed number of times, it is written here also for the fixed number of times, after that at one point my condition should be false and from there it should come out.
So this was the condition part, after condition next is increment portion, means I have already initialized, after that I have to do increment. If I have taken a value, then I have to perform increment or decrement with that value, in some cases we perform decrement also. So after initializing it is not necessary that we will always perform increment.
Let’s say, if I want to run loop 10 times, running it for 10 times means I have initialized the loop from 1 after that once the code is executed I want it to execute for one more time means I need replace 1 by 2, for that which condition will I use? I will use increment, initialize from 1, after initialization code will not be executed, always condition and increment will only get executed. Guys, one thing to be noted is in for initialization will be executed only one time whereas condition and increment will be executed multiple times till our condition proves false, increment will also be executed till condition proves false.
Let’s say I have written here 1, after increment it will be 2, execute code once then increment so 1 will be increased to 2, check condition and execute code, after increment 2 will increase to 3 again condition and execute code. Till condition is true this process will go on, once the condition is proved false then loop will break and the code after curly braces will start executing. So this was loop, we saw loop’s concept as well as for loop concept also.
We will understand this with an example, here we have taken an example, I have to run this loop 5 times that’s why the condition is written in this manner. Here I will open sublime text, in it we will open html, example of for loop, save as for. Now in this program what I have to do? First I will open script, this is Java script, in program I want, as it is our for loop example, so I want to print number from 1 to 5.
So for that I can use two technique, first I will write document.write,(15 seconds gap) okay, instead of write If we write write Ln then every time whatever new we will write it will start from new line. So till now we used to write only, document.write but by writing writeln the write will jump on to next line. So let’s say I have written this program here we can see Example of for Loop.
In this I will consider for loop, here it will be like this, as soon as I wrote for loop, you can see suggestion appeared. Now if I press enter then this becomes my for loop’s syntax. Sublime text by default generates syntax, here you can see first portion is var which means I initialization of variable, I equals to things dot length minus 1. Means If we consider an array of things then decrease the length of my array by 1 because in array we saw array always start from 0th position, so array.length is a method of array to find the length.
So if in array we have 5 elements then my array.length will return me value 5, if I want my loop to run 5 times then here things.length should be minus 1 because whatever execution is it starts from 0. So if length is 5 then I have to run it how many times? 0 to 4, that’s why here we always use minus 1. But it is not necessary we define variable in this way only, we can also do it by removing this, where I equals to 0 or I equals to 1, okay, we can give whatever initialization we want, it is not necessary we are going to work with array every time.
But sublime text suggests the first way as default method, initially we will take var I equal to 1 where I is greater than equal to 0 is given by our sublime text, but my condition is different. I want, I will remove this decrement operator also, I want my condition to currently print 1 to 5 but I don’t want it to print one by one but I want it to print with help of for loop, so what will I do? Printing from 1to 5 means printing less than 6 or else print till 5.
So what condition will I write? I is less or equal to 5, what will happen because of this? Value of I will be 1 so yes I is less or equal to 1 means 1 is less or equal to 5, our condition is true, whatever code I have written will be executed. After its execution I will increase the value of I so here I will write I plus plus, so my value has increased by 1 after this statement, now again the condition will be checked so I equals to 1 which will be increased to 2, so 2 is less or equal to 5, yes condition will be true, again there will be increment in I, it will change from 2 to 3, 3 is less or equal to 5, this will continue till 5, 5 is less or equal to 5.
This will be my last time for my condition being true, as soon as the value of I increases to 6 here 6 is less or equal to 5 makes my condition false, because 6 is greater than 5, in this case my execution will reach here means my for loop execution will stop. So this was for loop, I want print elements from 1 to 5 so I will write here, document.writeln, here I will simply write I that’s it, save and reload, so you can see our output is 1 2 3 4 5.
So we have in total run this loop for 5 times so our loop is running 5 times. As soon as the condition increases to 6 then condition proves to be false and the execution comes out of curly braces. So here what happened? Here our elements 1to 5 have been printed, here due to writeln you can see space between two elements, so we can generate this space with help of writeln.
Let’s say instead of writeln I write only write so you can see the space has been removed. So to add space what is useful ln, so here with help of for loop I have printed numbers from 1 to 5 by only one statement. Otherwise the second method is (10 seconds gap), so here we will write 1 2 3 4 5 so you can see even by this method the output is same here on reloading you can see same output is generated.
What is the difference in writing in normal way and using for loop? For loop decreases our time as well as line of code. Here we can see I have written total 5 lines and in this case we have written only 3 lines, ideally looking this is only one line and this is my for loop. So I have finished my code in one line only, so the difference is visible here it is done in 1 line and there it took me 5 lines, so these are our advantages of using loop.
Currently we are printing from 1 to 5 but if we want to print from 1 to 100, then I will simply write 100 instead of 5, so here we can see it is getting printed from 1 to 100, if I do this same thing by document.writeln then almost I will have to type this 100 times. So this was our advantage of using loop, with help of loop we can simply the large tasks. So this was our for loop.
Next loop is while loop which is quite similar to for loop, now what is the difference between for loop and while loop? In for loop, in parenthesis we write initialization, condition and increment, we write these three things together. In while loop this is not the case, syntax in while loop allows us to write only condition. So here it depends on programmer that how he is handling the while loop.
If we have to execute any elements for infinite times then in that case we use while loop, whereas for loop is used in case of executing piece of code for finite times, because in for loop initialize and increment both re given, that’s why our for loop cannot be used for infinite loop, but if we are using while loop then we have large possibility that our loop should be converted in infinite loop because in syntax you can see after while a condition is added, so the loop runs till this condition proves to be true so if I have to break the while loop then I have to write the condition in such way that the condition should be proved false and it should break after a finite times of execution.
So while loop is used for executing something infinite times. Let’s see an example for while loop, we will copy this same program and paste it. Example of while loop, save and this is my code for while loop, now we will write example of while loop, for that we will write while and this is my bracket, (3 seconds gap) okay, so in while we will write a condition, if in this we have to print numbers from 1 to 5, so our for condition which was I less or equals to 5 will be same so initialization will be done outside while, so it will be like this, var I equal to 1 and in condition it will be I less or equal to 5 so here we will write our document.write portion, document.writeln(i), after this in while loop we have to place our increment operator i.e. I plus plus, okay so you can see in for loop we wrote all this things in one line, initialization, condition and after semicolon our increment operator, in while we took initialization outside while, condition is written in parenthesis of while, and increment operator we have placed in while, so if I save and reload this then I am getting exactly same output as for loop.
There will be no difference in output, difference can be seen in our method, like which method we have used. So when you work on a large web applications that time you have to make the judgment call that which loop you are going to use which will be more feasible for you. Most of the programmers use for loop because we can define all the three things at one place one time but if requirement occurs in which one of the three things is not required then we can use while loop. So this was our while loop.
In while loop the execution of piece of code will happen as many times as we have placed condition, but we need to initialize the variable and increment should be done inside while loop. If some requirement comes in which we don’t need to initialize or we don’t need increment then using while loop will be more useful in comparison with for loop.
Next is do while loop, there is only one difference between while loop and do while loop, while loop will execute our piece of code till infinite loop and do while loop also execute our piece of code till infinite loop. But if we look in syntax then you can see do then brackets are opening and after closing of bracket condition comes. In while we saw while comes first, condition comes first and after that our piece of code is executed.
Here you can see this difference, if we have to execute while loop, then condition is executed first and after that piece of code executes, means being true of condition is very necessary for executing our piece of code. But in do while loop our piece of code will be executed once and then condition will be checked if condition is true then condition will be executed once again. So difference between do while loop and while loop is that to execute while’s piece of code condition should be at least true once. Whereas in do while loop piece of code will anyways get executed once but to again execute it condition should be true.
So if you have any requirement where anyhow you have to execute piece of code at least once but after that execution depends upon that piece of code only then you can use do while. Now we will see how we can use do while, we will copy this while program and paste it and I will this much portion from here. Here we will write do…while loop and save it as do…while, okay so this is our do…while loop. In this also we have to initialize var, var I equals to 5 and now after writing do we will open parenthesis, we will write document.writeln(i) and now we will write our increment operator and after parenthesis we will add while and our condition I is less or equal to 5, here I is equal to 1 and our program starts and I reload it, so here output is exactly same as while and for loop, so in each case there is no difference in output.
But the method which we used to get output differs and that is important, the output which we got from for same output we are getting from while loop as well as do…while loop. We have in total covered three loops, for loop, while loop, do…while loop. All these loops are very important for our code execution when we have to repeat our piece of code again and again.
So guys that’s it for today’s segment, we still have two more loops remaining which is for…IN and for…OFF. Now for…IN and for….OFF we will see in our next segment in which we are going to continue loops only.
I request you all to practice all the three loops which we saw today and if you have any queries or questions then do tell us on forums.learnvern.com we will revert back with solutions to our questions. In next segment we will continue loops for…IN for...OFF.
(video duration 25 minutes and 49 seconds)
Share a personalized message with your friends.