Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




JAVASCRIPT - NESTED LOOPS


So far we have learnt, while loops and for loops.


Well! They can be combined together, one inside the other. And this is called Nested Loops.A loop inside a loop.


Let us understand with the below example.


Say, we want to write a program that prints the below output:


1 John
1 Sam
2 John
2 Sam

Let us write the code for the same using a for loop inside while loop i.e. Nested Loops.


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
		
	var x = 1 
	var names = ["John", "Sam"]
	while (x <= 2) {
		for (y of names) {
			document.write(x, " ", y, " :: ")
        }
		x++ 
    }
    
</script>      
</body>
</html>


Output :



  1 John :: 1 Sam :: 2 John :: 2 Sam ::

So, in the above code we have two loops.

  1. The while loop

  2. And the for loop

Let us see in detail, how the loops work.


So, we have declared a variable names x to maintain the while loop.


var x = 1

That stores the initial value as 1.

java_Collections

Then we have declared a Array named names for the for loop.


var names = ["John", "Sam"]

Which has the names, John and Sam.

java_Collections

Then we have the while loop,


while (x <= 2)

And the control checks, if the value of x is less than or equal to 2. In this case the value of x is 1.


So, the control enters the while loop.


1st Iteration of while loop


During the first iteration of while loop, we find the for loop inside the while loop.


for (y of names)

And we start the iteration of for loop inside the 1st Iteration of while loop.


1st Iteration of for loop inside the 1st Iteration of while loop


And in the 1st Iteration of for loop, we find the document.write statement.


document.write(x, " ", y, " :: ")

If we check the current value of x, it is 1.

java_Collections

And in the first iteration of for loop, the first value from the names Array is taken and placed into the variable y.

java_Collections

So, we got the value of x and y. And the output is,

Output :



  1 John ::

And we are done with the first iteration of for loop. And begin the second iteration of for loop.


2nd Iteration of for loop inside the 1st Iteration of while loop


Now, in the 2nd Iteration of for loop, we find the document.write statement.


document.write(x, " ", y, " :: ")

Similarly, we get the current value of x, which is still 1.

java_Collections

And in the second iteration of for loop, the second value from the names Array is taken and placed into the variable y. Which is Sam.

java_Collections

So, we got the value of x and y. And the output is,

Output :



  1 John :: 1 Sam ::

So, we are done with the execution of the inner for loop. And we get out of the for loop.


And we encounter the line,


x++

Where we have incremented the value of x by 1.

java_Collections

Then we come back to the while loop.


while (x <= 2)

And the condition of while loop satisfies as the value of x is 2. So, we start the second iteration of while loop.


2nd Iteration of while loop


Similarly, during the second iteration of while loop, we find the for loop inside the while loop.


for (y of names)

And we start the iteration of for loop inside the 2nd Iteration of while loop.


1st Iteration of for loop inside the 2nd Iteration of while loop


And in the 1st Iteration of for loop, we find the document.write statement.


document.write(x, " ", y, " :: ")

If we check the current value of x, it is 2.

java_Collections

And in the first iteration of for loop, the first value from the names Array is taken and placed into the variable y.

java_Collections

So, we got the value of x and y. And the output is,

Output :



  1 John :: 1 Sam :: 2 John ::

And we are done with the first iteration of for loop. And begin the second iteration of for loop.


2nd Iteration of for loop inside the 1st Iteration of while loop


Now, in the 2nd Iteration of for loop, we find the document.write statement.


document.write(x, " ", y, " :: ")

Similarly, we get the current value of x, which is still 2.

java_Collections

And in the second iteration of for loop, the second value from the names Array is taken and placed into the variable y. Which is Sam.

java_Collections

So, we got the value of x and y. And the output is,

Output :



  1 John :: 1 Sam :: 2 John :: 2 Sam ::

So, we are done with the execution of the inner for loop. And we get out of the for loop.


And we encounter the line,


x++

Where we have incremented the value of x by 1.

java_Collections

Then we come back to the while loop.


while (x <= 2)

And the condition of while loop fails, as the value of x is 3. And the execution ends.


So, we have learnt, how the execution of a for loop inside a while loop works.


Now, let us look at a little complex example, of a Nested for loop. i.e. A for loop inside a for loop.


Say, we have a Array that has two names, Sam and Jack.


Now, we need to document.write each letter of the name, as stated below :


S, a, m, --> J, a, c, k, -->

Let us explain with the below example.


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
		
	var names = ["Sam", "Jack"]
	
	for (x of names) {
    	for (y of x) {
        	document.write(y , ", ")
        }
     	document.write(" --> ")
    }    
    
</script>      
</body>
</html>


Output :



  S, a, m, --> J, a, c, k, -->

So, if we look at the above code, we have created a Array named names,


var names = ["Sam", "Jack"]

And stored the names, Sam and Jack in it.

java_Collections

Now, our main motto is, to document.write each letters of the names. And for that we have used two for loops.

  1. Outer for loop

  2. And the Inner for loop

So, we come to the next line where the execution of Outer for loop begins.


for (x of names)

1st Iteration of the Outer for loop


So, in the 1st Iteration of the Outer for loop, we get the first element(i.e. Sam) from the Array names and store it in the variable x.

java_Collections

Then we come to the inner for loop.


for (y of x)

1st Iteration of the Inner for loop


Now, in the 1st Iteration of the Inner for loop, we collect the value from x, one by one and store in the variable y.


Since, there is Sam stored in x. The first element of x (i.e. S) is taken and stored in y.

java_Collections

Then we encounter the document.write statement.


	document.write(y , ", ")

Where we print the value of y.

Output :



  S,

Then we start the second Iteration of the Inner for loop.


2nd Iteration of the Inner for loop


Similarly, in the 2nd Iteration of the Inner for loop, we collect second value from x, and store it in the variable y.


Since, there is Sam stored in x. The second element of x (i.e. a) is taken and stored in y.

java_Collections

Then we encounter the document.write statement.


	document.write(y)

Where we document.write the value of y.

Output :



  S, a,

Then we start the third Iteration of the Inner for loop.


3rd Iteration of the Inner for loop


Similarly, in the 3rd Iteration, we collect third value from x, and store it in the variable y.

java_Collections

And document.write the value of y.

Output :



  S, a, m,

And we come to an end of the Inner for loop and print -->


document.write(" --> ")

Then JavaScript control goes back to the outer for loop.


2nd Iteration of the Outer for loop


Again, in the 2nd Iteration of the Outer for loop, we get the second element(i.e. Sam) from the Array names.

java_Collections

And store it in the variable x.

java_Collections

Then we come to the inner for loop.


for (y of x)

1st Iteration of the Inner for loop


Now, in the 1st Iteration of the Inner for loop, we collect the value from x, one by one and store in the variable y.


Since, there is Jack stored in x. The first element of x (i.e. J) is taken and stored in y.

java_Collections

Then we encounter the document.write statement.


	document.write(y , ", ")

Where we document.write the value of y.

Output :



  S, a, m, --> J,

And continuing in the same way, we get the below output.

Output :



  S, a, m, --> J, a, c, k, -->