Friday, 9 March 2018

Learn : Shows the use of "for Loop" in JavaScript

We can execute a group of statements multiple times based on a specific condition by using the for loop. Now create a Web page named ForLoopUse.html which shows the use of for loop.



####Showing the for Code for the ForLoopUse.html file #######

<!DOCTYPE html>
<html>
<head>
<title>Using the for loop</title>
</head>
<body>
<h1>Using the for loop in the Script</h1>
<SCRIPT type="text/javascript">
var number;
document.write("Even Numbers From 0 to 10 <br/>");
for(number=0;number<=10;number=number+2)
{
document.write(number+"<br/>");
}
</SCRIPT>
</body>
</html>

Here: The number variable is declared to store an even number from 0 to 10. In the for loop the number variable is used as loop control variable and is assigned a value of 0. The condition number<=10 is then evaluated. This condition is true in the first iteration as a result the value of the number variable is displayed. After this the number variable is incremented by 2 and the condition is evaluated again. In this way the for loop iterates six times until the value of the number variable becomes.

######Showing the output of the ForloopUse.html######


We can observe that the for loop is executed until the number variable is less than or equal to 10.


No comments:

Post a Comment