A loop inside another loop is called the nesting of loops. So a for loop inside the body of another for loop is called a nested for loop. Nesting of for loop are essential for handling multidimensional arrays. Also nested for loop are needed for handling table of contents (rows & columns).
Syntax of a Nested for loop:
for ( initialization ; condition ; updating)
{ // Starting of outer loop
for ( initialization ; condition ; updating)
{ // Starting of inner loop
Statement / Statements ;
}
Statement / Statements ;
}
Nested for loop example:
class NestedDemo
{
public static void main(String args[])
{
for(int i = 0 ;i<10; i++ ) // outer loop
{
for(int j = 1; j<=i+1; j++ )// inner loop
{
System.out.print("*");
}
System.out.println();
}
}
}
Comments
Post a Comment