Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Loop Javascript

Looping in javascript

Uploaded by

Mohd Shayaan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Loop Javascript

Looping in javascript

Uploaded by

Mohd Shayaan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Section-6: Looping Structure

Loops: Loops can be used to execute the same code over and over again, particularly useful when
dealing with arrays as shown in figure 3.3. JavaScript supports different kinds of loops described below:
• for - loops through a block of code a number of times

• for/in - loops through the properties of an object

• while - loops through a block of code while a specified condition is true

• do/while - also loops through a block of code while a specified condition is true

*For Loop:- The for loop is best suited when you already know the number of times the
statements should be executed. the loop executes until the condition becomes false.

Syntax: for(initialization; condition; increment)

//statements

When a for loop executes:

1. The initializing expression is get executed and this expression usually initializes one or more loop
variables.

2. The condition expression is evaluated. if the value of condition is true, the loop statements are
executed. if the value of condition is false, for loop terminates.

3. The increment expression executes and increments the value by the specified step value.

4. The statements execute, and control returns to step 2.

Example1:

<!DOCTYPE html>

<html><body> <p> For loop Example</p> <script>

for (i = 0; i < 5; i++) {

document.write("LITTLE FLOWER SCHOOL, GIDA GORAKHPUR " +"<br>");

</script> </body> </html>

Example2:

for (i = 0, y=5; i <y; i++)

{
document.write("LITTLE FLOWER SCHOOL, GIDA GORAKHPUR " +"<br>");

Note: We can omit statement 1 when the values are set before the loop starts:

Example3:

var i=0;

for (; i <5; i++)

document.write("LITTLE FLOWER SCHOOL, GIDA GORAKHPUR " +"<br>");

Example4 :

for (i=5; i >0;i--)

document.write("LITTLE FLOWER SCHOOL, GIDA GORAKHPUR " +"<br>");

Example5:

var i=0;

for (; i <5;)

document.write("LITTLE FLOWER SCHOOL, GIDA GORAKHPUR " +"<br>");

i++

}
*While loop: The While loop is another commonly used loop in JavaScript. the purpose of the
while loop is to execute a block of statements over and over again until the condition fails. it is best
suited in a scenario where we don’t know in advanced as to how many times the loop will be executed.

Syntax for while loop:

while (condition)

Statement-1;

Statement-2; }
Example1: Print Little Flower School Gida, Gorakhpur 5 times.

<!DOCTYPE html>

<html> <body> <p>While loop Example</p> <script>

var i=0;

while( i < 5)

document.write("Little Flower School Gida, Gorakhpur" +"<br>");

i++;

</script> </body> </html>

Example2: Print the number from 1 to 5.

<!DOCTYPE html>

<html> <body> <p>While loop Example</p> <script>

var i=1;

while( i <=5)

document.write(i+"," +"<br>");

i++;

</script> </body> </html>

Example3: Print the number from 5 to 1.

<!DOCTYPE html>

<html> <body> <p>While loop Example</p> <script>

var i=5;

while( i >=5)

document.write(i+"," +"<br>");

i--;

}
</script> </body> </html>

*do While loop This is another kind of loop and different from the for loop and the while loop.
this loop will execute the statement at least once that is the statements inside the loop will always get
executed at least once, even if the condition is false. the condition is checked happens after the loop has
been executed. the loop will continue to execute or will terminate according to on the condition.

Syntax for while loop:

do

Statement1;

Statement2

} while (Condition)

Example1: Print Little Flower School Gida, Gorakhpur 5 times.

<!DOCTYPE html>

<html> <body> <p>While loop Example</p> <script>

var i=0;

do

document.write("Little Flower School Gida, Gorakhpur" +"<br>");

i++;

} while( i < 5)

</script> </body> </html>

Example2: Print the number from 1 to 5.

<!DOCTYPE html>

<html> <body> <p>While loop Example</p> <script>

var i=1;

do

document.write(i+"," +"<br>");

i++;
} while( i < 5)

</script> </body> </html>

Example3: Print the number from 5 to 1.

<!DOCTYPE html>

<html> <body> <p>While loop Example</p> <script>

var i=5;

do

document.write(i+"," +"<br>");

i--;

} while( i >=5)

</script> </body> </html>

For..in Loop:The for/in loop: the JavaScript for/in statement loops through the properties of
an object explain below.

<!DOCTYPE html>

<html> <body> <p> For..in Loop Example></p> <script>

var person = {fname:"Aman", lname:"Maan", age:35};

var text = "";

var x;

for (x in person) {

text += person[x] + " ";

document.write(text);

</script> </body> </html>

You might also like