0% found this document useful (0 votes)
10 views8 pages

6 Loops

Uploaded by

vjjaish9569
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

6 Loops

Uploaded by

vjjaish9569
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JavaScript 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.

 JavaScript now supports four different types of loops:


 for — loops through a block of code until the counter reaches a specified number.
 while — loops through a block of code as long as the condition specified evaluates to true.
 do…while — loops through a block of code once; then the condition is evaluated. If the
condition is true, the statement is repeated as long as the specified condition is true.
 for…in — loops through the properties of an object.

The for Loop


 The for loop repeats a block of code as long as a certain condition is met. It is typically used to
execute a block of code for certain number of times.
 Its syntax is:
for (initialization; condition; increment)
{
// Code to be executed
}
 The parameters of the for loop statement have following meanings:
 initialization — it is used to initialize the counter variables, and evaluated once
unconditionally before the first execution of the body of the loop.
 condition — it is evaluated at the beginning of each iteration. If it evaluates to true, the
loop statements execute. If it evaluates to false, the execution of the loop ends.
 increment — it updates the loop counter with a new value each time the loop runs.
 The for loop is particularly useful for iterating over an array

Flow Chart
 The flow chart of a for loop in JavaScript would be as follows −

By: Anuj Gupta


The while Loop
 The while loop loops through a block of code as long as the specified condition evaluates to true.
As soon as the condition fails, the loop is stopped.
The flow chart of while loop looks as follows −

The generic syntax of the while loop is:


while(condition)
{
// Code to be executed
}

The do...while Loop


 The do-while loop is a variant of the while loop, which evaluates the condition at the end of each
loop iteration.
 With a do-while loop the block of code executed once, and then the condition is evaluated, if the
condition is true, the statement is repeated as long as the specified condition evaluated to is true.

Flow Chart
 The flow chart of a do-while loop would be as follows –

By: Anuj Gupta


Syntax
 The generic syntax of the do-while loop is:
do
{
// Code to be executed
}
while(condition);

Difference Between while and do...while Loop


 The while loop differs from the do-while loop in one important way — with a while loop, the
condition to be evaluated is tested at the beginning of each loop iteration, so if the conditional
expression evaluates to false, the loop will never be executed.
 With a do-while loop, on the other hand, the loop will always be executed once even if the
conditional expression evaluates to false, because unlike the while loop, the condition is
evaluated at the end of the loop iteration rather than the beginning.

The for...in Loop


 The for-in loop is a special type of a loop that iterates over the properties of an object, or the
elements of an array.
 The generic syntax of the for-in loop is:
for(variable in object)
{
// Code to be executed
}
 The loop counter i.e. variable in the for-in loop is a string, not a number. It contains the name of
current property or the index of the current array element.
Note: The for-in loop should not be used to iterate over an array where the index order is
important. You should better use a for loop with a numeric index.

By: Anuj Gupta


<html >
<head>

<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);

document.write("<p>The number after loop is " + i + "</p>");

</script>
</body>
</html>
<html>
<head>
<title>Forin Loop</title>
</head>
<body>
<script>

// An object with some properties

var person = {"fname": "ANUJ", "surname": "GUPTA", "MOB": "1234567890"};

// Loop through all the properties in the object


for(var abc in person)
{
document.write("<p>" + abc + " = " + person[abc] + "</p>");
}

// An array with some elements


var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya"];

// Loop through all the elements in the array


for(var i in fruits)
{
document.write("<p>" + fruits[i] + "</p>");
}
</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"];

for(let letter of letters) {


document.write(letter + "<br>"); // a,b,c,d,e,f
}
document.write("<hr>");

// Iterating over string


let greet = "Hello World!";

for(let character of greet) {


document.write(character + "<br>"); // H,e,l,l,o, ,W,o,r,l,d,!
}
</script>
</body>
</html>

You might also like