Nested for loop
-
Last Updated: October 30, 2023
-
By: javahandson
-
Series
In this article, we will learn what is a nested for loop in Java with proper examples. If you want to learn about other Java topics you can click the above Java menu or you can click on this link Java topics.
We can write a for loop inside another for loop. Such a type of loop is called a nested for loop.
for( expression1; expression2; expression3) { for( expression1; expression2; expression3) { // execute a set of statements } }
There is no limit to the number of nested for loops. However, it is very difficult to debug the program if the number of nested for loops exceeds a certain limit.
Please find a link here that answers what is the limit to the number of nested for loops ( The most voted answer says Between 550 and 575 )
https://stackoverflow.com/questions/41347019/is-there-a-limit-to-the-number-of-nested-for-loops
In the real programming world, we will never use that many nested loops. The max number of nested for loops that a programmer will be using is 3-4 that to in rare scenarios.
We will try to understand it better with an example. The below program prints the numbers from 1 to 5.
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { for (int i=1; i<=5; i++) { System.out.println("Value of i = "+i); } } } Output: Value of i = 1 Value of i = 2 Value of i = 3 Value of i = 4 Value of i = 5
The above for loop will execute 5 times.
Note* If you want to understand the for loop in more depth then please check the article on for loop here.
Now we will write a program that prints the numbers from 1 to 4.
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { for (int j=1; j<=4; j++) { System.out.println("Value of j = "+j); } } } Output: Value of j = 1 Value of j = 2 Value of j = 3 Value of j = 4
The above for loop will execute 4 times.
Now we will combine the above 2 loops to form a nested for loop.
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { for (int i=1; i<=5; i++) { for (int j=1; j<=4; j++) { System.out.println("Value of i = "+i + " and value of j = "+j); } System.out.println(); } } }
The above for loop will execute m * n times i.e. 5 * 4 = 20. We will understand it below.
a. We are initializing the outer for loop with i=1 and we are checking the condition if i <= 5. Since 1 <= 5, we will go inside the outer for loop.
b. We are initializing the inner for loop with j=1 and we are checking the condition if j <= 4. Since 1 <= 4, we will go inside the inner for loop.
c. Inside the inner for loop we are printing the value of i & j. Here i = 1 and j = 1.
d. Now the modification expression of the inner for loop will be executed and will increment the value of j = 2.
e. We will check if j <= 4. Since 2 <= 4, we will go inside the inner for loop.
f. Inside the inner for loop we are printing the value of i & j. Here i = 1 and j = 2.
g. Now the modification expression of the inner for loop will be executed and will increment the value of j = 3.
h. We will check if j <= 4. Since 3 <= 4, we will go inside the inner for loop.
i. Inside the inner for loop we are printing the value of i & j. Here i = 1 and j = 3.
j. Now the modification expression of the inner for loop will be executed and will increment the value of j = 4.
k. We will check if j <= 4. Since 4 <= 4, we will go inside the inner for loop.
l. Inside the inner for loop we are printing the value of i & j. Here i = 1 and j = 4.
m. Now the modification expression of the inner for loop will be executed and will increment the value of j = 5.
n. We will check if j <= 4. Since 5 <= 4 here the condition is false and we will come out of the inner for loop ( Here inner for loop is terminated for i = 1 )
o. Now the modification expression of the outer for loop will be executed and will increment the value of i = 2.
p. We will check if i <= 5 since 2 <= 5 hence we will go inside the outer for loop.
q. We are again initializing the inner for loop with j=1 and we are checking the condition if j <= 4. Since 1 <= 4, we will go inside the inner for loop.
r. Inside the inner for loop we are printing the value of i & j. Here i = 2 and j = 1.
s. Now the modification expression of the inner for loop will be executed and will increment the value of j = 2.
t. We will check if j <= 4. Since 2 <= 4, we will go inside the inner for loop.
u. Inside the inner for loop we are printing the value of i & j. Here i = 2 and j = 2.
v. The inner for loop will execute till Here i = 2 and j = 4.
w. Now the modification expression of the inner for loop will be executed and will increment the value of j = 5.
x. We will check if j <= 4. Since 5 <= 4 here the condition is false and we will come out of the inner for loop ( Here inner for loop is terminated for i = 2 )
y. Again the modification expression of the outer for loop will be executed and will increment the value of i = 3.
z. The above steps will be repeated again.
Output of the above program : Value of i = 1 and value of j = 1 Value of i = 1 and value of j = 2 Value of i = 1 and value of j = 3 Value of i = 1 and value of j = 4 Value of i = 2 and value of j = 1 Value of i = 2 and value of j = 2 Value of i = 2 and value of j = 3 Value of i = 2 and value of j = 4 Value of i = 3 and value of j = 1 Value of i = 3 and value of j = 2 Value of i = 3 and value of j = 3 Value of i = 3 and value of j = 4 Value of i = 4 and value of j = 1 Value of i = 4 and value of j = 2 Value of i = 4 and value of j = 3 Value of i = 4 and value of j = 4 Value of i = 5 and value of j = 1 Value of i = 5 and value of j = 2 Value of i = 5 and value of j = 3 Value of i = 5 and value of j = 4
Print the below patterns
*
**
***
****
*****
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { for (int i=1; i<=5; i++) { for (int j=1; j<=i; j++) { System.out.print("*"); } System.out.println(); } } }
*****
****
***
**
*
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { for (int i=5; i>=0; i--) { for (int j=1; j<=i; j++) { System.out.print("*"); } System.out.println(); } } }
So this is all about nested for loop in Java. I hope you like the article. If you have any questions on this topic please raise them in the comments section. Please share this post.