FOR LOOP CONCEPT:
For loop Syntax:
for loop example:
For loop Syntax:
Syntax:
The syntax of for loop is JavaScript is as follows −
for (initialization; test condition; iteration statement){
Statement(s) to be executed if test condition is true
}
for loop example:
<html>
<body>
<script type="text/javascript">
var count;
document.write("Starting Loop" + "<br />");
for(count = 0; count < 10; count++){
document.write("Current Count : " + count );
document.write("<br />");
}
document.write("Loop stopped!");
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
while loop syntax:
while loop example:
The syntax of while loop in JavaScript is as follows −
while (expression){ Statement(s) to be executed if expression is true }
while loop example:
<html>
<body>
<script type="text/javascript">
var count = 0;
document.write("Starting Loop ");
while (count < 10){
document.write("Current Count : " + count + "<br />");
count++;
}
document.write("Loop stopped!");
</script>
</body>
</html>
Do while syntax:
do while example:
Syntax
The syntax for do-while loop in JavaScript is as follows −
do{ Statement(s) to be executed; } while (expression);
Note − Don’t miss the semicolon used at the end of the do...while loop.
do while example:
<html>
<body>
<script type="text/javascript">
var count = 0;
document.write("Starting Loop" + "<br />");
do{
document.write("Current Count : " + count + "<br />");
count++;
}
while (count < 5);
document.write ("Loop stopped!");
</script>
</body>
</html>
Comments
Post a Comment