JavaScript – Loops For While Do-While


What is a looping statement ?

Looping statements are the statements executed piece of code repeatedly until the condition is reached.

Why does JavaScript use looping statements ?

When there is a need to execute the code several number of times, javascript uses looping statements.

What are the advantages of looping statements ?

1. Reduces the length of the code.

2. Uses less memory space.

What are the looping statements in Javascript ?

1. For Loop

2. While Loop

3. Do-While Loop

Javascript interview questions basic !!!

Click to Learn More about – Javascript online learning

1. For – Loop

For loop is a control flow statement which allows the code to be repeatedly executed until the iteration numbers are reached.

What are the types of For loop ?

There are 3 types of for loops


1. For Loop (Simple)

2. For-In Loop

3. Infinite For Loop

1. For loop (Simple) :

For loop works on index basics.

Syntax :

for(initialisation; condition; increment/decrement){  
	//Piece of code to be executed  
}  

For loop is divided in to 3 parts

1. Initialisation part

2. Conditional part

3. Increment/ Decrement part

1. Initialisation part

Here we can declare and initialise the loop control variable.

2. Conditional part

Here the conditions are checked, if the condition is true the loop is executed, and if the condition is false then the loop does not execute and the control flow goes outside of the for loop.

3. Increment/ Decrement part

Here the Increment / Decrement part is executed for updating the loop control variable.

Explanation :

For loop is executed about the initial value mentioned in the Initialisation part and executes the condition in the conditional part, then uses the Increment/ Decrement part to satisfy the condition by iterating the rows until the condition is satisfied.

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var a = 5;
			for(i=0;i<a;i++){
				document.write("The value of i = " + i + "<br/>");
			}
		</script>
	</head>
	<body></body>

</html>

Output :

The value of i = 0

The value of i = 1

The value of i = 2

The value of i = 3

The value of i = 4

2. For-In Loop

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var names = {n1:"Antony", n2:"Rahul",n3:"Robert", n4:"Rocky" };
			var a;
			for(a in names){
				document.write("The iteration value from variable names = " + names[a] + "<br/>");
			}
		</script>
	</head>
	<body></body>
</html>

Output :

The iteration value from variable names = Antony

The iteration value from variable names = Rahul

The iteration value from variable names = Robert

The iteration value from variable names = Rocky

3. Infinite For Loop

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var i;
			for(;;){
				document.write("The value of i = " + i + "<br/>");
			}
		</script>
	</head>
	<body></body>
</html>

Output :

The output will be printed infinite times.

2. While Loop

While loop is also a control flow statement which executes the code repeatedly until the boolean condition is satisfied.

It checks first the boolean condition and based on that success, the flow of the code is controlled.

1. Simple While Loop :

Syntax :

while (boolean condition)
{
   // Piece of code to be executed.
}

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var a = 0;
			while(a<5){
				document.write("Printing the value a = " +a+ "<br/>");
				a++;
			}
		</script>
	</head>
	<body></body>
</html>

Output :

Printing the value a = 0

Printing the value a = 1

Printing the value a = 2

Printing the value a = 3

Printing the value a = 4

2. Infinite While Loop :

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			while(true){
				document.write("Printing the values infinite times <br/>");
			}
		</script>
	</head>
	<body></body>
</html>

Output :

This will prints the value infinite times.

3. Do – While Loop

Similar to while statement but first it will prints the statements and then it checks for the boolean condition.

Prints the statements and then checks for the boolean condition.


Syntax :

do
{
   // Piece of code to be executed
}
while (boolean condition);

1. Simple do-while loop :

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var a = 0;
			do{
				document.write("Printing the value a = " +a+ "<br/>");
				a++;
			}while(a<5)
		</script>
	</head>
	<body></body>
</html>

Output :


Printing the value a = 0

Printing the value a = 1

Printing the value a = 2

Printing the value a = 3

Printing the value a = 4

2. Infinite do-while loop :

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var a = 0;
			do{
				document.write("Printing the value a = " +a+ "<br/>");
				a++;
			}while(true)
		</script>
	</head>
	<body></body>
</html>

Output :

This will print the values of a in infinite times.