6 Loops
6 Loops
Loops are used to execute the same block of code again and again, as long as a certain condition
is met. The basic idea behind a loop is to automate the repetitive tasks within a program to save
the time and effort.
Flow Chart
The flow chart of a for loop in JavaScript would be as follows −
Flow Chart
The flow chart of a do-while loop would be as follows –
<title>For Loop</title>
</head>
<body>
<script>
for(var i=1; i<=5; i++)
{
document.write("<p>The number is " + i + "</p>");
}
</script>
</body>
</html>
<html>
<head>
<title>While Loop</title>
</head>
<body>
<script>
var a=5;
var i = 25;
while(i >= 21)
{
document.write("<p>The number is " + a + "</p>");
a=a-1;
i--;
}
</script>
</body>
</html>
<html>
<head>
<title>Do-While Loop</title>
</head>
<body>
<script>
var i = 1;
do {
document.write("<p>The number is " + i + "</p>");
i++;
}while(i <= 1);
</script>
</body>
</html>
<html>
<head>
<title>Forin Loop</title>
</head>
<body>
<script>
</script>
</body>
</html>
<html>
<head>
<title>for...of Loop</title>
</head>
<body>
<script>
// Iterating over array
let letters = ["a", "b", "c", "d", "e", "f"];