Introduction:-
In this tutorial I will show you the two types of loops there are in JavaScript.
Explanation:-
You might say where is the code? He is already explaining without the code?
Well this time I think that its better if the Explanation comes before the Code
There are 2 types of loops:-
For Loops:-
The For Loops are used in this way
Code:
for (something)
{
do something else
}
While Loops:-
The While Loops are used in this way:-
Code:
while (something)
{
Do something else
}
Solution:-
Now that you had an idea of the basic usage of the loops here are some practical codes:-
For Loops:-
Code:
<SCRIPT LANGUAGE="JavaScript">
var lp=1;
for (lp=0;lp<15;lp++)
{
document.write ("For Loop "+lp+"<BR>");
}
</SCRIPT>
Note: lp is a variable, and until that variable has reached the value of 14 (<15) the loop will go on
While Loops:-
Code:
<SCRIPT LANGUAGE="JavaScript">
var lp=0;
while (lp<15)
{
document.write ("While Loop "+lp+"<BR>");
lp++;
}
</SCRIPT>
Note: lp is a variable, and while that variable is <15 the loop will go on
A Preview:-
None
Conclusion:-
As Always Feedback is welcome! In the next tutorial I will tell you how to exit from the loop. I don't think that this needs any source code..