JavaScript-Exercise 09- For Loop

The “For Loop” is the most popular loop in many popular programming languages. Like any other loop, it’s task is to repeat a given statement inside the loop until the given condition in the loop becomes false.

It’s easier to understand with an example. In the following example web page we have a web page that repeats a given statement using “for loop”.

JavaScript

<script type="text/javascript">
var i=0;
for(i=0;i<=5;i++)
{
    document.write("The Number is"+ i);
    document.write("<br/>");
}
</script>

The above is an example of internal JavaScript and it repeatedly prints the value of  i  till the value of i >= 5. We will embed this code with HTML page called the loop1.html.

HTML File-“loop1.HTML”

<html>
<head><title>For Loop</title>
<script type="text/javascript">
var i=0;
for(i=0;i<=5;i++)
{
    document.write("The Number is"+ i);
    document.write("<br/>");
}
</script>
</head>
<body>
<p>THIS IS TEST USING INTERNAL JAVA SCRIPT</p>
</body>
</html>

When you open this web page in a browser that understands the JavaScript, then you will get the following output.

Output

Output - For Loop
Output – For Loop
Advertisements