Java Nesting of Loops
What is nesting of loop?
Nesting of loop means writting a loop inside the another loop.
Example - for loop contains another for loop or while loop or do while loop.
Example - Displaying the star pattern using nesting of loop
In this example we are going to write for loop inside the for loop.
public class StarPatternEx
{
public static void main(String[] args)
{
int rows = 4;
for(int i=1;i<=rows;i++) // single row
{
for(int j=1;j<=i;j++) // repeat in rows
{
System.out.print("*"); // print stars
}
System.out.print("\n"); // move to new line
}
}
}
Output
*
**
***
****