Java


Java Tutorial


Admission Enquiry Form

  

Nested For Loop in Java

What is Nested For loop in Java?

Nested For loop is a For loop inside another For loop.

Syntax of Nested For loop

for(initialization;condition;increment/decrement)
{
for(initialization;condition;increment/decrement)
{
//number of statements.
} //end of inner loop
} //end of outer loop




Example of Nested For loop:

//Java program of Nested for loop
public class NestedForLoop {
public static void main(String[] args)
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
System.out.print("*");
} //end of inner for loop
System.out.println();  
} //end of outer for loop
}//end of main method
}//end of class


Output:

***
***
***

In the above example, a star pattern will we printed in 3 rows and 3 columns format.Rows will be printed by the outer loop and columns will be printed by inner loop