Definition: For Loop
Syntax:
for (variable=startvalue;variable<=endvalue;variable=variable+increment)
{
code to be executed
}
{
code to be executed
}
startvalue = from which value your loop will start. here it is starting from 0.
endvalue = upto how much time loop to execute.
The example below defines a loop that starts with i=0. The loop will continue to run as long as i is less than, or equal to
5. i
will increase by 1 each time the loop runs.
It is same as "For Loop" in our C language.
Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>For Loop</title>
<script type="text/javascript">
var i=0;
for (i=0;i<=5;i++)
{
document.write("Hello World " + i);
document.write("<br />");
}
</script>
</head>
<body>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>For Loop</title>
<script type="text/javascript">
var i=0;
for (i=0;i<=5;i++)
{
document.write("Hello World " + i);
document.write("<br />");
}
</script>
</head>
<body>
</body>
</html>
Output:
Hello World 0
Hello World 1
Hello World 2
Hello World 3
Hello World 4
Hello World 5
Hello World 1
Hello World 2
Hello World 3
Hello World 4
Hello World 5
No comments:
Post a Comment