For Loop with Nested For Looping – JavaScript Tutorial


In this article, we will take a brief look at For Loop with nested for looping. Well, when you are learning a for loop is essential to understand. For loop is often very convenient than while loop Because of its structure. For loop is very easy to construct. In while loop and do while loop has a common pattern of initialization expression and statement. We first initialize the variable then evaluate the expression and execute the statement. The same way for loop also executed. but there is one difference in for loop is you can initialize variable specify expiration and increment or decrement variable value on the one line.

For loop

So let’s understand for loop using syntax.

we will start for loop using for keyword and in the parentheses, we specify initialization expression and increment or decrement. So we will first in a slice the variable with value and specify semicolon after initialization after that we will specify expression and specify semicolon at the end and at the last specify increment and decrement.

after that, we specify the statement which we wanted to execute when the value in the expression is equal to true. Keep in mind every loop execute it statement only if the value of expression returns true otherwise the execution of the loop will be terminated. Using the example you will get the complete understanding of for loop so let’s take an example.

Explanation:-

Now the important thing is don’t forget to specify semicolon before and after the expression. So when you execute this example you will get 0 to 4 numbers on the console. Looping is executed just like a while loop but we just initialize a variable specify the condition and increase the count value on the single line. The difference between while and for loop is structure. Right, let me first explain how for loop works.

When you specify for loop it will first create a variable count and specify value 0 to that variable. After that it will check if the value of the count variable is less than 5 or not if it is returning true then the body statement is executed. so you will get zero on the console.  After that for loop will increase the size of the variable using increment or decrement operator. We had used the increment operator so the value of the count variable will increase by one. so now you have 1 numeric value in the count variable.

Now the force Luke will go at the top again and evaluate an expression with the new count value. So it will check if one is less than 5 or not. If the expression returns true then the body of the statement is executed.  and increment the value of the count variable again.

So the for loop execute the same pattern again and again till the expression of the for loop become false. So when the value of the count variable becomes 5 the expression will return false. Because 5 is not less than 5. Right.

Execution Pattern of For Loop
  1. For loop will initialize variable with the value.
  2. For loop will evaluate expression before moving to the statement. if the expression is false then for loop will terminate. If the expression returns true then for loop follow the third step.
  3. For loop will execute the statement in the third step. You can also use the compound statement to execute multiple statements one by one and fourth last step is for loop will increase or decrease the value of the variable by one.

After that for loop will go at the top and skip the initialization step because we already initialize the variable so for loop will move to the next step and evaluate expression then it will move to the third step and then move to the fourth step.

So the for loop follow the execution pattern repeatedly till the expression returns false. You can any slide multiple variables also in the initialization step and also specify multiple expressions and increment and decrement operators.

In this example, we declare two variables I and J and specify 0 and 5 value. After that, we will check if I is less than 5 or not if this expression returns true then we will execute console.log method and print i variable value and J variable value on the console. You can also see we use increment and decrement operator to I and J variable.

so when you execute this code you will get 0 to 4 number value of a variable and 5 to 1 value of j variable on the console. we will increase the value of a variable and decrease the value of a variable right.

Nested For Loop

So nested for loop is just a for loop inside for loop. Nested for loop used to access the dynamic array element or you can also use this for creating different patterns and for Matrix manipulation. There are multiple ways to use nested for loop in your program. But we are using this nested for loop for dynamic array. Taking this article you will understand how nested for loop works and how you can use this for dynamic array.

so let’s create an example and after that, I will tell you how this example works.

Explanation:-

So now let me tell you what we have done in this example. We first created a for loop with for keyword and specify initialization, expression and increment and decrement. Then created a variable using let keyword and specify value one to that variable. After that, we had evaluated the expression and check if i is less than 4 then execute the body of the for the loop. If i is not less than 4 then skip the body section. After that, we increase the value of the variable by one using + + operator.

Just after that in the body section, we created in a for loop with the variable K and specified value 1 to that variable. Then we check key is less than 4 or not the expression returns true then we execute the body of the inner for loop. And after that increase the value of the key variable by one using the increment operator and just after that in the console we print special character \n and on the console. This special character used to print a character on the new line.

so execute this code and see what it is the output.

Output Explanation

So the first outer for loop will first execute and specify one value to the i variable. After that, the outer follow to execute the inner for loop. Now in a for loop will execute 3 times. Because it will first specify value 1 to the K variable and evaluate the expression and print console.log method with I and K variable value. after that, this inner for loop will increase the value of the k variable and evaluate expression again. So the inner loop executes 3 times till the expression become false. So when the value of a variable is equal to 4 then the expression will become false and terminate the inner loop. And just after that the execution exit the inner loop and print console.log method just after the inner loop.

After that, the outer loop will increase the value of i variable by one. and execute the outer loop. Along with that the outer loop will execute the inner loop again and print 123 on the console. When the inner loop will be terminated. And execute the outer loop again and increase the value of the variable by one. So now you have value 3 in the variable.

now the outer loop again executed and execute inner for loop. When the inner loop terminated and print 123 on the console with the I value. Outer for loop will increase the size of i variable by one. now you have four in the variable.

now when the outer loop expression evaluates its returns false because 4 is not less than 4. So the outer loop will terminate and exit the loop. So that is why you will get this type of output on the console. That’s it I hope you understand the nested for low enough.

comment me if you have any question.

Thank You for reading this article.