DEV Community

Deepikandas
Deepikandas

Posted on

#23 Known is a Drop! Nested for loop in JAVA with examples-

What is a Nested for Loop?

A nested loop means:
👉 A for loop inside another for loop
Outer loop → controls rows
Inner loop → controls columns/repetitions inside each row

Example 1:
Print
1 1 1 1 1
1 1 1 1 1


public class NestedForLoop {

    public static void main(String[] args) {
        for(int row=1;row<=2;row++) {
            for(int column=1;column<=5;column++) {
                System.out.print(1+" ");
            }
            System.out.println();
        }

    }

}
Output:
1 1 1 1 1 
1 1 1 1 1 

Enter fullscreen mode Exit fullscreen mode

Example 2: Print
1 1 1 1 1
2 2 2 2 2

package nestedForLoop;

public class NestedForLoop {

    public static void main(String[] args) {
        for(int row=1;row<=2;row++) {
            for(int column=1;column<=5;column++) {
                System.out.print(row+" ");
            }
            System.out.println();
        }

    }

}
**Output:**
1 1 1 1 1 
2 2 2 2 2 
Enter fullscreen mode Exit fullscreen mode

Example 3:Print
1 2 3 4 5
1 2 3 4 5

public class NestedForLoop {

    public static void main(String[] args) {
        for(int row=1;row<=2;row++) {
            for(int column=1;column<=5;column++) {
                System.out.print(column+" ");
            }
            System.out.println();
        }
    }
}
Output:
1 2 3 4 5 
1 2 3 4 5 
Enter fullscreen mode Exit fullscreen mode

Example 4:Print
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6

public class NestedForLoop {

    public static void main(String[] args) {
        for(int row=1;row<=4;row++) {
            for(int column=1;column<=6;column++) {
                System.out.print(column+" ");
            }
            System.out.println();
        }
    }
}
output:
1 2 3 4 5 6 
1 2 3 4 5 6 
1 2 3 4 5 6 
1 2 3 4 5 6 
Enter fullscreen mode Exit fullscreen mode

Example 5:
print
& & & & &

& & & & &

& & & & &

public class NestedForLoop {

    public static void main(String[] args) {
        for(int row=1;row<=3;row++) {
            for(int column=1;column<=5;column++) {
                System.out.print("&  ");
            }
            System.out.println();
        }
    }
}

output:
& & & & &   
& & & & &                  
& & & & &  
Enter fullscreen mode Exit fullscreen mode

Example 6:
Print
& & & & &

& & & &

& & &
& &
&

public class NestedForLoop {

    public static void main(String[] args) {
        for(int row=5;row<=5&& row!=0;row--) {
            for(int column=1;column<=row;column++) {
                System.out.print("&  ");
            }
            System.out.println();
        }
    }
}

Enter fullscreen mode Exit fullscreen mode
& & & & &   
& & & &                 
& & &
& &
&

//Using count variable;
public class NestedForLoop {

    public static void main(String[] args) {
        int count=5;
        for(int row=1;row<=5;row++) {
            for(int column=1;column<=count;column++) {
                System.out.print("*  ");
            }
                count--;

            System.out.println();
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Example 7:
Print
&
& &

& & &

& & & &
& & & & &

public class NestedForLoop {

    public static void main(String[] args) {
        int count=1;
        for(int row=1;row<=5;row++) {
            for(int column=1;column<=count;column++) {
                System.out.print("*  ");
            }
                count++;

            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)