DEV Community

velvizhi Muthu
velvizhi Muthu

Posted on

Module-2 Nested for loop

What is a Nested Loop?

A nested loop is a loop inside another loop. It’s a programming concept where one loop (called the inner loop) runs entirely within another loop (called the outer loop). This is often used when you need to perform repeated operations within another repeated operation.

Nested for loop :

output :
1 1 1 1 1

ans :
System.out.print(1 + " ");
System.out.print(1 + " ");
System.out.print(1 + " ");
System.out.print(1 + " ");
System.out.print(1 + " ");

for(int i = 1;i<=5; i++)
{
System.out.print(1 + " ");
}


output :
1 1 1 1 1
1 1 1 1 1

ans :
for(int i = 1;i<=5; i++)
{
System.out.print(1 + " ");
}
System.out.println();
System.out.print(1 + " ");
System.out.print(1 + " ");
System.out.print(1 + " ");
System.out.print(1 + " ");
System.out.print(1 + " ");

for(int i = 1;i<=5; i++)
{
System.out.print(1 + " ");
}
System.out.println();
for(int i = 1;i<=5; i++)
{
System.out.print(1 + " ");
}


output :
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

ans :
for(int i = 1;i<=5; i++)
{
System.out.print(1 + " ");
}
System.out.println();
for(int i = 1;i<=5; i++)
{
System.out.print(1 + " ");
}
System.out.println();
System.out.print(1 + " ");
System.out.print(1 + " ");
System.out.print(1 + " ");
System.out.print(1 + " ");
System.out.print(1 + " ");

for(int i = 1;i<=5; i++)
{
System.out.print(1 + " ");
}
System.out.println();
for(int i = 1;i<=5; i++)
{
System.out.print(1 + " ");
}
for(int i = 1;i<=5; i++)
{
System.out.print(1 + " ");
}


Nested for loop :

for(int row = 1; row<=3;row++)
{
for(int column = 1; column <= 5; column++)
{
System.out.println(1 + " ");
}
System.out.println();
}


output :




for(int row = 1; row<=3;row++)
{
for(int column = 1; column <= 5; column++)
{
System.out.print("*" + " ");
}
System.out.println();
}


1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
for(int row = 1; row<=3;row++)
{
for(int column = 1; column <= 5; column++)
{
System.out.print(row + " ");
}
System.out.println();

}

1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
for(int row = 1; row<=5;row++)
{
for(int column = 1; column <= 5; column++)
{
System.out.print(row + " ");
}
System.out.println();
}


1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
for(int row = 1; row<=5;row++)
{
for(int column = 1; column <= 5; column++)
{
System.out.print(column + " ");
}
System.out.println();

}

Right-angled Triangle
1
1 1
1 1 1
for(int row=1; row<=5; row++) {
for(int column=1; column<=row; column++) {
System.out.print(1 + " ");
}
System.out.println();

}

Right-angled Triangle
*

  • * * * * for(int row=1; row<=3; row++) { for(int column=1; column<=row; column++) { System.out.print(1 + " "); } System.out.println(); } --------------------------------------------------- A A B A B C A B C D for(int row=1; row<=4; row++) { char ch = 'A'; for(int column=1; column<=i; column++) { System.out.print(ch + " "); ch++; } System.out.println(); } ----------------------------------------------- Inverted Triangle : ***** **** *** ** * for(int row=5; row>=1; row--) { for(int column=1; column<=row; column++) { System.out.print("*"); } System.out.println(); } --------------------------------------------------------

Top comments (0)