Nested loop in java with example

Java allows for, while and do while loops to be nested. that is one loop is inside another loop.

Example:-

class group{
public static void main(String angt[]){
int i,j;
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
System.out.print(".");
System.out.println();
}
}
}

Output:-

.....
....
...
..
.

one type of loop can be nested with other type of loop. for example while loop is inside for loop.

Example:-


class group{
public static void main(String angt[]){
int i,j;
for(i=0;i<2;i++)
{
j=1;
while(j<=5)
{
System.out.println(j);
j++;
}
}
}
}

Previous Code:-
Do-while loop

List Code:-
Java codes

Next Code:-
Math class methods

Java Programs:-
List of Java Programs

Leave reply

Add your comments here

Back to Top