Java Code Example: double nested for-loop

This code is a Java program that demonstrates the use of a double nested loop. It calculates the result of multiplying i, j, and k inside the innermost loop and prints it to the console.

The outermost loop (for (int i = 3; i < 5; i++)) runs twice with the values of i being 3 and 4.

The second loop (for (int j = 1; j < 10; j++)) runs 9 times for each iteration of the outer loop with the values of j being 1 to 9.

The innermost loop (for (int k = 1; k < 5; k++)) runs 4 times for each iteration of the second loop with the values of k being 1 to 4.

So the innermost loop runs 4 * 9 * 2 = 72 times in total, and the result of each multiplication of i, j, and k is printed to the console with a space after it. After each iteration of the second loop, a line break is printed, and after each iteration of the outer loop, two line breaks are printed.

public class DoubleNestedLoop {  
    public static void main(String[] args) {
        for (int i = 3; i < 5; i++) {
            for (int j = 1; j < 10; j++) {
                for (int k = 1; k < 5; k++) {
                    int result = i * j * k;
                    System.out.print(result + " ");
                }
                System.out.println();
            }
            System.out.println();
        }
    }
}
Output
3 6 9 12 
6 12 18 24 
9 18 27 36 
12 24 36 48 
15 30 45 60 
18 36 54 72 
21 42 63 84 
24 48 72 96 
27 54 81 108 

4 8 12 16 
8 16 24 32 
12 24 36 48 
16 32 48 64 
20 40 60 80 
24 48 72 96 
28 56 84 112 
32 64 96 128 
36 72 108 144