For Loop in JavaScript

For Loop in JavaScript

For Loop in JavaScript

Example1:

for (b=1; b<=5; b++){

document. Write(b+”<br/>”);

}

Output:

1

2

3

4

5

Example2:

var g=1;

for(; g<=5; g++){

document.write(i+”<br/>”);

}

Output:

1

2

3

4

5

we can also use more than one value in statement 1 by using commas to separate them

for(g=1, text = ” “; g<=5; g++){

text = g;

document.write(g+”<br/>”);

}

if statement 2 returns true, the loop will start over again, if it returns false, the loop will end.

statement 3 is used to change the initial variable. it can do anything, including negative increment (g–),

and positive increment(g=g+15) statement 3 is also optional, but if you increment your values inside the Loop.

Example:

var g = 0;

for( ; g<100; ){

document.write(g);

g++;

}

Print Even Value 0 to 20 in JavaScript:

Var b = 0;

for(; b<=20; b+=2){

document.write(b);

}

Output: 0 2 4 6 8 10 12 14 16 18 20

While Loop in JavaScript:

Time to now on to the second of our three loop statements while. the while loop repeats through a block of code, but only as long as a specified condition is true.

Example:

var b= 0;

while(g<=10){

document.write(g+”<br/>”);

}

Example:

var b=1;

while(b<=5){

document.write(b+”<br/>”);

b=b+1;

}

Endless loops are not good. and one way this happens is if we forget to increase the variable used in the condition

Note: Make sure that the condition in the while loop eventually becomes false.

Do While Loop in JavaScript:

if is a variant of a while loop but with one important difference. this loop will execute the code block once, before checking if the condition is true, and then it will repeat the loop as long as the condition is true.

do{

code block

}

while(condition);

Example:

var count = 1;

do{

document.write(“hello” “<br/>”);

count++;

}

while(count<=10);

Break in JavaScript:

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

if(i==5){

break;

}

document.write(i+ “<br/>”);

}

Output:

0

1

2

3

4

Continue in JavaScript:

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

if(i==5){

continue;

}

document.write(i+”<br/>”);

}

Output:

0

1

2

3

4

5

6

7

8

9

10

Website | + posts

Technical content writer with data scientist, artificial intelligence, programming language, database. He has a bachelor’s degree in IT and a certificate in digital marketing, Digital transformation web development android app development He has written for website like Boomi techie, tech mantra, information hub, Tech all

amit verma

Technical content writer with data scientist, artificial intelligence, programming language, database. He has a bachelor’s degree in IT and a certificate in digital marketing, Digital transformation web development android app development He has written for website like Boomi techie, tech mantra, information hub, Tech all