<html>
<head>
<title>For Loop</title>
</head>
<body>
<script type="text/javascript">
for (i=1; i<=5; i++)
{
document.write(i + "<br/>")
}
</script>
</body>
</html>
1
2
3
4
5
Syntax :
while (condition)
{
code to be executed
}
<html>
<head>
<title>While Loop</title>
</head>
<body>
<script type="text/javascript">
var i=5;
while (i<=10)
{
document.write(i + "<br/>");
i++;
}
</script>
</body>
</html>
5
6
7
8
9
10
Syntax:
do{
code to be executed
}while (condition);
<html>
<head>
<title>Do While Loop</title>
</head>
<body>
<script type="text/javascript">
var i=10;
do{
document.write(i + "<br/>");
i++;
}while (i<=15);
</script>
</body>
</html>
10
11
12
13
14
15
<html>
<head>
<title>For In Loop</title>
</head>
<body>
<script type="text/javascript">
var student = { name:"VVRR", age: 26, degree: "MCA" };
for (var item in student) {
document.write(student[item]+"<br />"); // => "Bill", then 25, then "Masters"
}
</script>
</body>
</html>
VVRR
26
MCA
<html>
<head>
<title>Break Statement</title>
</head>
<body>
<p id="my_demo"></p>
<script type="text/javascript">
var text = "";
var i;
for (i = 0; i < 10; i++) {
if (i === 3) { break; }
text += "The number is " + i + "<br>";
}
document.getElementById("my_demo").innerHTML = text;
</script>
</body>
</html>
The number is 0
The number is 1
The number is 2
<html>
<head>
<title>Continue Statement</title>
</head>
<body>
<p id="demo_continue"></p>
<script type="text/javascript">
var text = "";
var i;
for (i = 0; i < 10; i++) {
if (i === 3) { continue; }
text += "The number is " + i + "<br>";
}
document.getElementById("demo_continue").innerHTML = text;
</script>
</body>
</html>
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9