You're printing a 10×10 multiplication table. For each row (1-10), you need to print 10 products (row × 1, row × 2, ... row × 10). The outer loop picks the row; the inner loop fills each column.

Multiplication table

Generate a multiplication table using nested loops.

size
Multiplication.java
Replay: real traced execution (multi-file project)
public class Multiplication {
    public static void main(String[] args) {
        int size = 5;

        // Print header
        System.out.print("   ");
        for (int i = 1; i <= size; i++) {
            System.out.printf("%4d", i);
        }
        System.out.println();
        System.out.println("   " + "-".repeat(size * 4));

        // Print table
        for (int row = 1; row <= size; row++) {
            System.out.printf("%2d |", row);
            for (int col = 1; col <= size; col++) {
                System.out.printf("%4d", row * col);
            }
            System.out.println();
        }


    }
}
public class Multiplication {
    public static void main(String[] args) {
        int size = 3;

        // Print header
        System.out.print("   ");
        for (int i = 1; i <= size; i++) {
            System.out.printf("%4d", i);
        }
        System.out.println();
        System.out.println("   " + "-".repeat(size * 4));

        // Print table
        for (int row = 1; row <= size; row++) {
            System.out.printf("%2d |", row);
            for (int col = 1; col <= size; col++) {
                System.out.printf("%4d", row * col);
            }
            System.out.println();
        }


    }
}
public class Multiplication {
    public static void main(String[] args) {
        int size = 10;

        // Print header
        System.out.print("   ");
        for (int i = 1; i <= size; i++) {
            System.out.printf("%4d", i);
        }
        System.out.println();
        System.out.println("   " + "-".repeat(size * 4));

        // Print table
        for (int row = 1; row <= size; row++) {
            System.out.printf("%2d |", row);
            for (int col = 1; col <= size; col++) {
                System.out.printf("%4d", row * col);
            }
            System.out.println();
        }


    }
}
  1. size ← 5

    1public class Multiplication {2    public static void main(String[] args) {3        int size→ 5 = 5;  //@size=3, 104        5        // Print header6        System.out.print("   ");7        for (int i = 1; i <= size; i++) {
    output   
  2. for (int i = 1; i <= size; i++)

    pass 1 of 5
    6System.out.print("   ");7for (int i1 = 1; i <= size5; i++) {8    System.out.printf("%4d", i1);9}
    All 5 passes — pass 1 is the card above
    passi
    11
    22
    33
    44
    55
  3. System.out.println(" " + "-".repeat(size * 4));

    9}10System.out.println();11System.out.println("   " + "-".repeat(size5 * 4));
    output   --------------------
  4. for (int row = 1; row <= size; row++)

    pass 1 of 5
    13// Print table14for (int row1 = 1; row <= size5; row++) {  //#?outer_loop15    System.out.printf("%2d |", row1);16    for (int col = 1; col <= size; col++) {  //#?inner_loop
    All 5 passes — pass 1 is the card above
    passrow
    11
    22
    33
    44
    55
  5. for (int col = 1; col <= size; col++)

    pass 1 of 25
    15System.out.printf("%2d |", row);16for (int col1 = 1; col <= size5; col++) {  //#?inner_loop17    System.out.printf("%4d", row1 * col1);18}
    25 passes — pass 1 is the card above
    passcolrow
    111
    221
    331
    441
    551
    612
    722
    832
    942
    ⋯ 14 more passes ⋯
    2445
    2555
  6. System.out.println();

    18    }19    System.out.println();20}
  7. System.out.println();

    18    }19    System.out.println();20}
  8. System.out.println();

    18    }19    System.out.println();20}
  9. System.out.println();

    18    }19    System.out.println();20}
  10. System.out.println();

    18    }19    System.out.println();20}
  1. size ← 3

    1public class Multiplication {2    public static void main(String[] args) {3        int size→ 3 = 3;4        5        // Print header6        System.out.print("   ");7        for (int i = 1; i <= size; i++) {
    output   
  2. for (int i = 1; i <= size; i++)

    pass 1 of 3
    6System.out.print("   ");7for (int i1 = 1; i <= size3; i++) {8    System.out.printf("%4d", i1);9}
    All 3 passes — pass 1 is the card above
    passi
    11
    22
    33
  3. System.out.println(" " + "-".repeat(size * 4));

    9}10System.out.println();11System.out.println("   " + "-".repeat(size3 * 4));
    output   ------------
  4. for (int row = 1; row <= size; row++)

    pass 1 of 3
    13// Print table14for (int row1 = 1; row <= size3; row++) {15    System.out.printf("%2d |", row1);16    for (int col = 1; col <= size; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    11
    22
    33
  5. for (int col = 1; col <= size; col++)

    pass 1 of 9
    15System.out.printf("%2d |", row);16for (int col1 = 1; col <= size3; col++) {17    System.out.printf("%4d", row1 * col1);18}
    All 9 passes — pass 1 is the card above
    passcolrow
    111
    221
    331
    412
    522
    632
    713
    823
    933
  6. System.out.println();

    18    }19    System.out.println();20}
  7. System.out.println();

    18    }19    System.out.println();20}
  8. System.out.println();

    18    }19    System.out.println();20}
  1. size ← 10

    1public class Multiplication {2    public static void main(String[] args) {3        int size→ 10 = 10;4        5        // Print header6        System.out.print("   ");7        for (int i = 1; i <= size; i++) {
    output   
  2. for (int i = 1; i <= size; i++)

    pass 1 of 10
    6System.out.print("   ");7for (int i1 = 1; i <= size10; i++) {8    System.out.printf("%4d", i1);9}
    All 10 passes — pass 1 is the card above
    passi
    11
    22
    33
    44
    55
    66
    77
    88
    99
    1010
  3. System.out.println(" " + "-".repeat(size * 4));

    9}10System.out.println();11System.out.println("   " + "-".repeat(size10 * 4));
    output   ----------------------------------------
  4. for (int row = 1; row <= size; row++)

    pass 1 of 10
    13// Print table14for (int row1 = 1; row <= size10; row++) {15    System.out.printf("%2d |", row1);16    for (int col = 1; col <= size; col++) {
    All 10 passes — pass 1 is the card above
    passrow
    11
    22
    33
    44
    55
    66
    77
    88
    99
    1010
  5. for (int col = 1; col <= size; col++)

    pass 1 of 100
    15System.out.printf("%2d |", row);16for (int col1 = 1; col <= size10; col++) {17    System.out.printf("%4d", row1 * col1);18}
    100 passes — pass 1 is the card above
    passcolrow
    111
    221
    331
    441
    551
    661
    771
    881
    991
    ⋯ 89 more passes ⋯
    99910
    1001010
  6. System.out.println();

    18    }19    System.out.println();20}
  7. System.out.println();

    18    }19    System.out.println();20}
  8. System.out.println();

    18    }19    System.out.println();20}
  9. System.out.println();

    18    }19    System.out.println();20}
  10. System.out.println();

    18    }19    System.out.println();20}
  11. System.out.println();

    18    }19    System.out.println();20}
  12. System.out.println();

    18    }19    System.out.println();20}
  13. System.out.println();

    18    }19    System.out.println();20}
  14. System.out.println();

    18    }19    System.out.println();20}
  15. System.out.println();

    18    }19    System.out.println();20}

Outer loop: each row (1-10). Inner loop: each column (multiply by 1-10).

nested loop A loop inside another loop. Inner completes fully for each outer iteration.

Print rectangle of stars

Draw a rectangle pattern with asterisks.

example
Rectangle.java
Replay: real traced execution (multi-file project)
public class Rectangle {
    public static void main(String[] args) {
        int width = 5;
        int height = 3;

        System.out.println("Rectangle " + width + "x" + height + ":");

        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                System.out.print("* ");
            }
            System.out.println();  // New line after each row
        }

        // Hollow rectangle
        System.out.println("\nHollow rectangle:");
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}
public class Rectangle {
    public static void main(String[] args) {
        int width = 3;
        int height = 3;

        System.out.println("Rectangle " + width + "x" + height + ":");

        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                System.out.print("* ");
            }
            System.out.println();  // New line after each row
        }

        // Hollow rectangle
        System.out.println("\nHollow rectangle:");
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}
public class Rectangle {
    public static void main(String[] args) {
        int width = 8;
        int height = 3;

        System.out.println("Rectangle " + width + "x" + height + ":");

        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                System.out.print("* ");
            }
            System.out.println();  // New line after each row
        }

        // Hollow rectangle
        System.out.println("\nHollow rectangle:");
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}
public class Rectangle {
    public static void main(String[] args) {
        int width = 5;
        int height = 2;

        System.out.println("Rectangle " + width + "x" + height + ":");

        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                System.out.print("* ");
            }
            System.out.println();  // New line after each row
        }

        // Hollow rectangle
        System.out.println("\nHollow rectangle:");
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}
public class Rectangle {
    public static void main(String[] args) {
        int width = 5;
        int height = 5;

        System.out.println("Rectangle " + width + "x" + height + ":");

        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                System.out.print("* ");
            }
            System.out.println();  // New line after each row
        }

        // Hollow rectangle
        System.out.println("\nHollow rectangle:");
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}
  1. width ← 5, height ← 3

    1public class Rectangle {2    public static void main(String[] args) {3        int width→ 5 = 5;   //@width=3, 84        int height→ 3 = 3;  //@height=2, 55        6        System.out.println("Rectangle " + width5 + "x" + height3 + ":");
    outputRectangle 5x3:
  2. for (int row = 0; row < height; row++)

    pass 1 of 3
    8for (int row0 = 0; row < height3; row++) {9    for (int col = 0; col < width; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  3. for (int col = 0; col < width; col++)

    pass 1 of 15
    8for (int row = 0; row < height; row++) {9    for (int col0 = 0; col < width5; col++) {10        System.out.print("* ");11    }
    output* 
    15 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    60
    71
    82
    93
    ⋯ 4 more passes ⋯
    143
    154
  4. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  5. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  6. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  7. System.out.println(" Hollow rectangle:");

    15// Hollow rectangle16System.out.println("\nHollow rectangle:");17for (int row = 0; row < height; row++) {
    output
    Hollow rectangle:
  8. for (int row = 0; row < height; row++)

    pass 1 of 3
    16System.out.println("\nHollow rectangle:");17for (int row0 = 0; row < height3; row++) {18    for (int col = 0; col < width; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  9. for (int col = 0; col < width; col++)

    pass 1 of 15
    17for (int row = 0; row < height; row++) {18    for (int col0 = 0; col < width5; col++) {19        if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
    15 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    60
    71
    82
    93
    ⋯ 4 more passes ⋯
    143
    154
  10. if (row == 0 || row == height - 1 || col == 0 || col == width - 1)

    pass 1 of 12
    18for (int col = 0; col < width; col++) {19    if (row0 == 0 || row == height3 - 1 || col0 == 0 || col == width5 - 1) {20        System.out.print("* ");21    } else {
    output* 
    All 12 passes — pass 1 is the card above
    passrowcol
    100
    201
    302
    403
    504
    610
    714
    820
    921
    1022
    1123
    1224
  11. System.out.println();

    24    }25    System.out.println();26}
  12. else

    pass 1 of 3
    20    System.out.print("* ");21} else {22    System.out.print("  ");23}
    output  
  13. System.out.println();

    24    }25    System.out.println();26}
  14. System.out.println();

    24    }25    System.out.println();26}
  1. width ← 3, height ← 3

    1public class Rectangle {2    public static void main(String[] args) {3        int width→ 3 = 3;4        int height→ 3 = 3;5        6        System.out.println("Rectangle " + width3 + "x" + height3 + ":");
    outputRectangle 3x3:
  2. for (int row = 0; row < height; row++)

    pass 1 of 3
    8for (int row0 = 0; row < height3; row++) {9    for (int col = 0; col < width; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  3. for (int col = 0; col < width; col++)

    pass 1 of 9
    8for (int row = 0; row < height; row++) {9    for (int col0 = 0; col < width3; col++) {10        System.out.print("* ");11    }
    output* 
    All 9 passes — pass 1 is the card above
    passcol
    10
    21
    32
    40
    51
    62
    70
    81
    92
  4. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  5. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  6. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  7. System.out.println(" Hollow rectangle:");

    15// Hollow rectangle16System.out.println("\nHollow rectangle:");17for (int row = 0; row < height; row++) {
    output
    Hollow rectangle:
  8. for (int row = 0; row < height; row++)

    pass 1 of 3
    16System.out.println("\nHollow rectangle:");17for (int row0 = 0; row < height3; row++) {18    for (int col = 0; col < width; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  9. for (int col = 0; col < width; col++)

    pass 1 of 9
    17for (int row = 0; row < height; row++) {18    for (int col0 = 0; col < width3; col++) {19        if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
    All 9 passes — pass 1 is the card above
    passcol
    10
    21
    32
    40
    51
    62
    70
    81
    92
  10. if (row == 0 || row == height - 1 || col == 0 || col == width - 1)

    pass 1 of 8
    18for (int col = 0; col < width; col++) {19    if (row0 == 0 || row == height3 - 1 || col0 == 0 || col == width3 - 1) {20        System.out.print("* ");21    } else {
    output* 
    All 8 passes — pass 1 is the card above
    passrowcol
    100
    201
    302
    410
    512
    620
    721
    822
  11. System.out.println();

    24    }25    System.out.println();26}
  12. else

    20    System.out.print("* ");21} else {22    System.out.print("  ");23}
    output  
  13. System.out.println();

    24    }25    System.out.println();26}
  14. System.out.println();

    24    }25    System.out.println();26}
  1. width ← 8, height ← 3

    1public class Rectangle {2    public static void main(String[] args) {3        int width→ 8 = 8;4        int height→ 3 = 3;5        6        System.out.println("Rectangle " + width8 + "x" + height3 + ":");
    outputRectangle 8x3:
  2. for (int row = 0; row < height; row++)

    pass 1 of 3
    8for (int row0 = 0; row < height3; row++) {9    for (int col = 0; col < width; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  3. for (int col = 0; col < width; col++)

    pass 1 of 24
    8for (int row = 0; row < height; row++) {9    for (int col0 = 0; col < width8; col++) {10        System.out.print("* ");11    }
    output* 
    24 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    65
    76
    87
    90
    ⋯ 13 more passes ⋯
    236
    247
  4. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  5. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  6. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  7. System.out.println(" Hollow rectangle:");

    15// Hollow rectangle16System.out.println("\nHollow rectangle:");17for (int row = 0; row < height; row++) {
    output
    Hollow rectangle:
  8. for (int row = 0; row < height; row++)

    pass 1 of 3
    16System.out.println("\nHollow rectangle:");17for (int row0 = 0; row < height3; row++) {18    for (int col = 0; col < width; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  9. for (int col = 0; col < width; col++)

    pass 1 of 24
    17for (int row = 0; row < height; row++) {18    for (int col0 = 0; col < width8; col++) {19        if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
    24 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    65
    76
    87
    90
    ⋯ 13 more passes ⋯
    236
    247
  10. if (row == 0 || row == height - 1 || col == 0 || col == width - 1)

    pass 1 of 18
    18for (int col = 0; col < width; col++) {19    if (row0 == 0 || row == height3 - 1 || col0 == 0 || col == width8 - 1) {20        System.out.print("* ");21    } else {
    output* 
    18 passes — pass 1 is the card above
    passrowcol
    100
    201
    302
    403
    504
    605
    706
    807
    910
    ⋯ 7 more passes ⋯
    1726
    1827
  11. System.out.println();

    24    }25    System.out.println();26}
  12. else

    pass 1 of 6
    20    System.out.print("* ");21} else {22    System.out.print("  ");23}
    output  
  13. System.out.println();

    24    }25    System.out.println();26}
  14. System.out.println();

    24    }25    System.out.println();26}
  1. width ← 5, height ← 2

    1public class Rectangle {2    public static void main(String[] args) {3        int width→ 5 = 5;4        int height→ 2 = 2;5        6        System.out.println("Rectangle " + width5 + "x" + height2 + ":");
    outputRectangle 5x2:
  2. for (int row = 0; row < height; row++)

    pass 1 of 2
    8for (int row0 = 0; row < height2; row++) {9    for (int col = 0; col < width; col++) {
  3. for (int col = 0; col < width; col++)

    pass 1 of 10
    8for (int row = 0; row < height; row++) {9    for (int col0 = 0; col < width5; col++) {10        System.out.print("* ");11    }
    output* 
    All 10 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    60
    71
    82
    93
    104
  4. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  5. for (int row = 0; row < height; row++)

    pass 2 of 2
    8for (int row1 = 0; row < height2; row++) {9    for (int col = 0; col < width; col++) {
  6. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  7. System.out.println(" Hollow rectangle:");

    15// Hollow rectangle16System.out.println("\nHollow rectangle:");17for (int row = 0; row < height; row++) {
    output
    Hollow rectangle:
  8. for (int row = 0; row < height; row++)

    pass 1 of 2
    16System.out.println("\nHollow rectangle:");17for (int row0 = 0; row < height2; row++) {18    for (int col = 0; col < width; col++) {
  9. for (int col = 0; col < width; col++)

    pass 1 of 10
    17for (int row = 0; row < height; row++) {18    for (int col0 = 0; col < width5; col++) {19        if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
    All 10 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    60
    71
    82
    93
    104
  10. if (row == 0 || row == height - 1 || col == 0 || col == width - 1)

    pass 1 of 10
    18for (int col = 0; col < width; col++) {19    if (row0 == 0 || row == height2 - 1 || col0 == 0 || col == width5 - 1) {20        System.out.print("* ");21    } else {
    output* 
    All 10 passes — pass 1 is the card above
    passrowcol
    100
    201
    302
    403
    504
    610
    711
    812
    913
    1014
  11. System.out.println();

    24    }25    System.out.println();26}
  12. for (int row = 0; row < height; row++)

    pass 2 of 2
    16System.out.println("\nHollow rectangle:");17for (int row1 = 0; row < height2; row++) {18    for (int col = 0; col < width; col++) {
  13. System.out.println();

    24    }25    System.out.println();26}
  1. width ← 5, height ← 5

    1public class Rectangle {2    public static void main(String[] args) {3        int width→ 5 = 5;4        int height→ 5 = 5;5        6        System.out.println("Rectangle " + width5 + "x" + height5 + ":");
    outputRectangle 5x5:
  2. for (int row = 0; row < height; row++)

    pass 1 of 5
    8for (int row0 = 0; row < height5; row++) {9    for (int col = 0; col < width; col++) {
    All 5 passes — pass 1 is the card above
    passrow
    10
    21
    32
    43
    54
  3. for (int col = 0; col < width; col++)

    pass 1 of 25
    8for (int row = 0; row < height; row++) {9    for (int col0 = 0; col < width5; col++) {10        System.out.print("* ");11    }
    output* 
    25 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    60
    71
    82
    93
    ⋯ 14 more passes ⋯
    243
    254
  4. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  5. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  6. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  7. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  8. System.out.println(); // New line after each row

    11    }12    System.out.println();  // New line after each row13}
  9. System.out.println(" Hollow rectangle:");

    15// Hollow rectangle16System.out.println("\nHollow rectangle:");17for (int row = 0; row < height; row++) {
    output
    Hollow rectangle:
  10. for (int row = 0; row < height; row++)

    pass 1 of 5
    16System.out.println("\nHollow rectangle:");17for (int row0 = 0; row < height5; row++) {18    for (int col = 0; col < width; col++) {
    All 5 passes — pass 1 is the card above
    passrow
    10
    21
    32
    43
    54
  11. for (int col = 0; col < width; col++)

    pass 1 of 25
    17for (int row = 0; row < height; row++) {18    for (int col0 = 0; col < width5; col++) {19        if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
    25 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    60
    71
    82
    93
    ⋯ 14 more passes ⋯
    243
    254
  12. if (row == 0 || row == height - 1 || col == 0 || col == width - 1)

    pass 1 of 16
    18for (int col = 0; col < width; col++) {19    if (row0 == 0 || row == height5 - 1 || col0 == 0 || col == width5 - 1) {20        System.out.print("* ");21    } else {
    output* 
    16 passes — pass 1 is the card above
    passrowcol
    100
    201
    302
    403
    504
    610
    714
    820
    924
    ⋯ 5 more passes ⋯
    1543
    1644
  13. System.out.println();

    24    }25    System.out.println();26}
  14. else

    pass 1 of 9
    20    System.out.print("* ");21} else {22    System.out.print("  ");23}
    output  
  15. System.out.println();

    24    }25    System.out.println();26}
  16. System.out.println();

    24    }25    System.out.println();26}
  17. System.out.println();

    24    }25    System.out.println();26}
  18. System.out.println();

    24    }25    System.out.println();26}

Outer loop: rows. Inner loop: columns within each row.

Print triangle pattern

Create a right triangle by varying inner loop iterations.

height
Triangle.java
Replay: real traced execution (multi-file project)
public class Triangle {
    public static void main(String[] args) {
        int height = 5;

        // Right triangle (left-aligned)
        System.out.println("Right triangle:");
        for (int row = 1; row <= height; row++) {
            for (int col = 1; col <= row; col++) {
                System.out.print("* ");
            }
            System.out.println();
        }

        // Inverted triangle
        System.out.println("\nInverted triangle:");
        for (int row = height; row >= 1; row--) {
            for (int col = 1; col <= row; col++) {
                System.out.print("* ");
            }
            System.out.println();
        }

        // Right-aligned triangle
        System.out.println("\nRight-aligned triangle:");
        for (int row = 1; row <= height; row++) {
            // Print spaces
            for (int space = height - row; space > 0; space--) {
                System.out.print("  ");
            }
            // Print stars
            for (int star = 1; star <= row; star++) {
                System.out.print("* ");
            }
            System.out.println();
        }

    }
}
public class Triangle {
    public static void main(String[] args) {
        int height = 3;

        // Right triangle (left-aligned)
        System.out.println("Right triangle:");
        for (int row = 1; row <= height; row++) {
            for (int col = 1; col <= row; col++) {
                System.out.print("* ");
            }
            System.out.println();
        }

        // Inverted triangle
        System.out.println("\nInverted triangle:");
        for (int row = height; row >= 1; row--) {
            for (int col = 1; col <= row; col++) {
                System.out.print("* ");
            }
            System.out.println();
        }

        // Right-aligned triangle
        System.out.println("\nRight-aligned triangle:");
        for (int row = 1; row <= height; row++) {
            // Print spaces
            for (int space = height - row; space > 0; space--) {
                System.out.print("  ");
            }
            // Print stars
            for (int star = 1; star <= row; star++) {
                System.out.print("* ");
            }
            System.out.println();
        }

    }
}
public class Triangle {
    public static void main(String[] args) {
        int height = 7;

        // Right triangle (left-aligned)
        System.out.println("Right triangle:");
        for (int row = 1; row <= height; row++) {
            for (int col = 1; col <= row; col++) {
                System.out.print("* ");
            }
            System.out.println();
        }

        // Inverted triangle
        System.out.println("\nInverted triangle:");
        for (int row = height; row >= 1; row--) {
            for (int col = 1; col <= row; col++) {
                System.out.print("* ");
            }
            System.out.println();
        }

        // Right-aligned triangle
        System.out.println("\nRight-aligned triangle:");
        for (int row = 1; row <= height; row++) {
            // Print spaces
            for (int space = height - row; space > 0; space--) {
                System.out.print("  ");
            }
            // Print stars
            for (int star = 1; star <= row; star++) {
                System.out.print("* ");
            }
            System.out.println();
        }

    }
}
  1. height ← 5

    1public class Triangle {2    public static void main(String[] args) {3        int height→ 5 = 5;  //@height=3, 74        5        // Right triangle (left-aligned)6        System.out.println("Right triangle:");7        for (int row = 1; row <= height; row++) {
    outputRight triangle:
  2. for (int row = 1; row <= height; row++)

    pass 1 of 5
    6System.out.println("Right triangle:");7for (int row1 = 1; row <= height5; row++) {8    for (int col = 1; col <= row; col++) {  //#?varying_limit
    All 5 passes — pass 1 is the card above
    passrow
    11
    22
    33
    44
    55
  3. for (int col = 1; col <= row; col++)

    pass 1 of 15
    7for (int row = 1; row <= height; row++) {8    for (int col1 = 1; col <= row1; col++) {  //#?varying_limit9        System.out.print("* ");10    }
    output* 
    15 passes — pass 1 is the card above
    passcolrow
    111
    212
    322
    413
    523
    633
    714
    824
    934
    ⋯ 4 more passes ⋯
    1445
    1555
  4. System.out.println();

    10    }11    System.out.println();12}
  5. System.out.println();

    10    }11    System.out.println();12}
  6. System.out.println();

    10    }11    System.out.println();12}
  7. System.out.println();

    10    }11    System.out.println();12}
  8. System.out.println();

    10    }11    System.out.println();12}
  9. System.out.println(" Inverted triangle:");

    14// Inverted triangle15System.out.println("\nInverted triangle:");16for (int row = height; row >= 1; row--) {
    output
    Inverted triangle:
  10. for (int row = height; row >= 1; row--)

    pass 1 of 5
    15System.out.println("\nInverted triangle:");16for (int row5 = height; row >= 1; row--) {17    for (int col = 1; col <= row; col++) {
    All 5 passes — pass 1 is the card above
    passrow
    15
    24
    33
    42
    51
  11. for (int col = 1; col <= row; col++)

    pass 1 of 15
    16for (int row = height; row >= 1; row--) {17    for (int col1 = 1; col <= row5; col++) {18        System.out.print("* ");19    }
    output* 
    15 passes — pass 1 is the card above
    passcolrow
    115
    225
    335
    445
    555
    614
    724
    834
    944
    ⋯ 4 more passes ⋯
    1422
    1511
  12. System.out.println();

    19    }20    System.out.println();21}
  13. System.out.println();

    19    }20    System.out.println();21}
  14. System.out.println();

    19    }20    System.out.println();21}
  15. System.out.println();

    19    }20    System.out.println();21}
  16. System.out.println();

    19    }20    System.out.println();21}
  17. System.out.println(" Right-aligned triangle:");

    23// Right-aligned triangle24System.out.println("\nRight-aligned triangle:");25for (int row = 1; row <= height; row++) {
    output
    Right-aligned triangle:
  18. for (int row = 1; row <= height; row++)

    pass 1 of 5
    24System.out.println("\nRight-aligned triangle:");25for (int row1 = 1; row <= height5; row++) {26    // Print spaces
    All 5 passes — pass 1 is the card above
    passrow
    11
    22
    33
    44
    55
  19. for (int space = height - row; space > 0; space--)

    pass 1 of 10
    26// Print spaces27for (int space4 = height5 - row1; space > 0; space--) {28    System.out.print("  ");29}
    output  
    All 10 passes — pass 1 is the card above
    passspacerow
    141
    231
    321
    411
    532
    622
    712
    823
    913
    1014
  20. for (int star = 1; star <= row; star++)

    pass 1 of 15
    30// Print stars31for (int star1 = 1; star <= row1; star++) {32    System.out.print("* ");33}
    output* 
    15 passes — pass 1 is the card above
    passstarrow
    111
    212
    322
    413
    523
    633
    714
    824
    934
    ⋯ 4 more passes ⋯
    1445
    1555
  21. System.out.println();

    33    }34    System.out.println();35}
  22. System.out.println();

    33    }34    System.out.println();35}
  23. System.out.println();

    33    }34    System.out.println();35}
  24. System.out.println();

    33    }34    System.out.println();35}
  25. System.out.println();

    33    }34    System.out.println();35}
  1. height ← 3

    1public class Triangle {2    public static void main(String[] args) {3        int height→ 3 = 3;4        5        // Right triangle (left-aligned)6        System.out.println("Right triangle:");7        for (int row = 1; row <= height; row++) {
    outputRight triangle:
  2. for (int row = 1; row <= height; row++)

    pass 1 of 3
    6System.out.println("Right triangle:");7for (int row1 = 1; row <= height3; row++) {8    for (int col = 1; col <= row; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    11
    22
    33
  3. for (int col = 1; col <= row; col++)

    pass 1 of 6
    7for (int row = 1; row <= height; row++) {8    for (int col1 = 1; col <= row1; col++) {9        System.out.print("* ");10    }
    output* 
    All 6 passes — pass 1 is the card above
    passcolrow
    111
    212
    322
    413
    523
    633
  4. System.out.println();

    10    }11    System.out.println();12}
  5. System.out.println();

    10    }11    System.out.println();12}
  6. System.out.println();

    10    }11    System.out.println();12}
  7. System.out.println(" Inverted triangle:");

    14// Inverted triangle15System.out.println("\nInverted triangle:");16for (int row = height; row >= 1; row--) {
    output
    Inverted triangle:
  8. for (int row = height; row >= 1; row--)

    pass 1 of 3
    15System.out.println("\nInverted triangle:");16for (int row3 = height; row >= 1; row--) {17    for (int col = 1; col <= row; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    13
    22
    31
  9. for (int col = 1; col <= row; col++)

    pass 1 of 6
    16for (int row = height; row >= 1; row--) {17    for (int col1 = 1; col <= row3; col++) {18        System.out.print("* ");19    }
    output* 
    All 6 passes — pass 1 is the card above
    passcolrow
    113
    223
    333
    412
    522
    611
  10. System.out.println();

    19    }20    System.out.println();21}
  11. System.out.println();

    19    }20    System.out.println();21}
  12. System.out.println();

    19    }20    System.out.println();21}
  13. System.out.println(" Right-aligned triangle:");

    23// Right-aligned triangle24System.out.println("\nRight-aligned triangle:");25for (int row = 1; row <= height; row++) {
    output
    Right-aligned triangle:
  14. for (int row = 1; row <= height; row++)

    pass 1 of 3
    24System.out.println("\nRight-aligned triangle:");25for (int row1 = 1; row <= height3; row++) {26    // Print spaces
    All 3 passes — pass 1 is the card above
    passrow
    11
    22
    33
  15. for (int space = height - row; space > 0; space--)

    pass 1 of 3
    26// Print spaces27for (int space2 = height3 - row1; space > 0; space--) {28    System.out.print("  ");29}
    output  
    All 3 passes — pass 1 is the card above
    passspacerow
    121
    211
    312
  16. for (int star = 1; star <= row; star++)

    pass 1 of 6
    30// Print stars31for (int star1 = 1; star <= row1; star++) {32    System.out.print("* ");33}
    output* 
    All 6 passes — pass 1 is the card above
    passstarrow
    111
    212
    322
    413
    523
    633
  17. System.out.println();

    33    }34    System.out.println();35}
  18. System.out.println();

    33    }34    System.out.println();35}
  19. System.out.println();

    33    }34    System.out.println();35}
  1. height ← 7

    1public class Triangle {2    public static void main(String[] args) {3        int height→ 7 = 7;4        5        // Right triangle (left-aligned)6        System.out.println("Right triangle:");7        for (int row = 1; row <= height; row++) {
    outputRight triangle:
  2. for (int row = 1; row <= height; row++)

    pass 1 of 7
    6System.out.println("Right triangle:");7for (int row1 = 1; row <= height7; row++) {8    for (int col = 1; col <= row; col++) {
    All 7 passes — pass 1 is the card above
    passrow
    11
    22
    33
    44
    55
    66
    77
  3. for (int col = 1; col <= row; col++)

    pass 1 of 28
    7for (int row = 1; row <= height; row++) {8    for (int col1 = 1; col <= row1; col++) {9        System.out.print("* ");10    }
    output* 
    28 passes — pass 1 is the card above
    passcolrow
    111
    212
    322
    413
    523
    633
    714
    824
    934
    ⋯ 17 more passes ⋯
    2767
    2877
  4. System.out.println();

    10    }11    System.out.println();12}
  5. System.out.println();

    10    }11    System.out.println();12}
  6. System.out.println();

    10    }11    System.out.println();12}
  7. System.out.println();

    10    }11    System.out.println();12}
  8. System.out.println();

    10    }11    System.out.println();12}
  9. System.out.println();

    10    }11    System.out.println();12}
  10. System.out.println();

    10    }11    System.out.println();12}
  11. System.out.println(" Inverted triangle:");

    14// Inverted triangle15System.out.println("\nInverted triangle:");16for (int row = height; row >= 1; row--) {
    output
    Inverted triangle:
  12. for (int row = height; row >= 1; row--)

    pass 1 of 7
    15System.out.println("\nInverted triangle:");16for (int row7 = height; row >= 1; row--) {17    for (int col = 1; col <= row; col++) {
    All 7 passes — pass 1 is the card above
    passrow
    17
    26
    35
    44
    53
    62
    71
  13. for (int col = 1; col <= row; col++)

    pass 1 of 28
    16for (int row = height; row >= 1; row--) {17    for (int col1 = 1; col <= row7; col++) {18        System.out.print("* ");19    }
    output* 
    28 passes — pass 1 is the card above
    passcolrow
    117
    227
    337
    447
    557
    667
    777
    816
    926
    ⋯ 17 more passes ⋯
    2722
    2811
  14. System.out.println();

    19    }20    System.out.println();21}
  15. System.out.println();

    19    }20    System.out.println();21}
  16. System.out.println();

    19    }20    System.out.println();21}
  17. System.out.println();

    19    }20    System.out.println();21}
  18. System.out.println();

    19    }20    System.out.println();21}
  19. System.out.println();

    19    }20    System.out.println();21}
  20. System.out.println();

    19    }20    System.out.println();21}
  21. System.out.println(" Right-aligned triangle:");

    23// Right-aligned triangle24System.out.println("\nRight-aligned triangle:");25for (int row = 1; row <= height; row++) {
    output
    Right-aligned triangle:
  22. for (int row = 1; row <= height; row++)

    pass 1 of 7
    24System.out.println("\nRight-aligned triangle:");25for (int row1 = 1; row <= height7; row++) {26    // Print spaces
    All 7 passes — pass 1 is the card above
    passrow
    11
    22
    33
    44
    55
    66
    77
  23. for (int space = height - row; space > 0; space--)

    pass 1 of 21
    26// Print spaces27for (int space6 = height7 - row1; space > 0; space--) {28    System.out.print("  ");29}
    output  
    21 passes — pass 1 is the card above
    passspacerow
    161
    251
    341
    431
    521
    611
    752
    842
    932
    ⋯ 10 more passes ⋯
    2015
    2116
  24. for (int star = 1; star <= row; star++)

    pass 1 of 28
    30// Print stars31for (int star1 = 1; star <= row1; star++) {32    System.out.print("* ");33}
    output* 
    28 passes — pass 1 is the card above
    passstarrow
    111
    212
    322
    413
    523
    633
    714
    824
    934
    ⋯ 17 more passes ⋯
    2767
    2877
  25. System.out.println();

    33    }34    System.out.println();35}
  26. System.out.println();

    33    }34    System.out.println();35}
  27. System.out.println();

    33    }34    System.out.println();35}
  28. System.out.println();

    33    }34    System.out.println();35}
  29. System.out.println();

    33    }34    System.out.println();35}
  30. System.out.println();

    33    }34    System.out.println();35}
  31. System.out.println();

    33    }34    System.out.println();35}

The inner loop's limit depends on the outer loop variable.

pattern Using loop variables to control output (spaces, stars, numbers).

Search in 2D grid

Find a value in a 2D array (matrix).

target
GridSearch.java
Replay: real traced execution (multi-file project)
public class GridSearch {
    public static void main(String[] args) {
        int[][] grid = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        };
        int target = 7;

        // Print the grid
        System.out.println("Grid:");
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[row].length; col++) {
                System.out.printf("%3d", grid[row][col]);
            }
            System.out.println();
        }

        // Search for target
        int foundRow = -1;
        int foundCol = -1;

        search:
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[row].length; col++) {
                if (grid[row][col] == target) {
                    foundRow = row;
                    foundCol = col;
                    break search;
                }
            }
        }

        if (foundRow != -1) {
            System.out.println("\nFound " + target + " at [" + foundRow + "][" + foundCol + "]");
        } else {
            System.out.println("\n" + target + " not found in grid");
        }
    }
}
public class GridSearch {
    public static void main(String[] args) {
        int[][] grid = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        };
        int target = 1;

        // Print the grid
        System.out.println("Grid:");
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[row].length; col++) {
                System.out.printf("%3d", grid[row][col]);
            }
            System.out.println();
        }

        // Search for target
        int foundRow = -1;
        int foundCol = -1;

        search:
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[row].length; col++) {
                if (grid[row][col] == target) {
                    foundRow = row;
                    foundCol = col;
                    break search;
                }
            }
        }

        if (foundRow != -1) {
            System.out.println("\nFound " + target + " at [" + foundRow + "][" + foundCol + "]");
        } else {
            System.out.println("\n" + target + " not found in grid");
        }
    }
}
public class GridSearch {
    public static void main(String[] args) {
        int[][] grid = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        };
        int target = 12;

        // Print the grid
        System.out.println("Grid:");
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[row].length; col++) {
                System.out.printf("%3d", grid[row][col]);
            }
            System.out.println();
        }

        // Search for target
        int foundRow = -1;
        int foundCol = -1;

        search:
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[row].length; col++) {
                if (grid[row][col] == target) {
                    foundRow = row;
                    foundCol = col;
                    break search;
                }
            }
        }

        if (foundRow != -1) {
            System.out.println("\nFound " + target + " at [" + foundRow + "][" + foundCol + "]");
        } else {
            System.out.println("\n" + target + " not found in grid");
        }
    }
}
public class GridSearch {
    public static void main(String[] args) {
        int[][] grid = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        };
        int target = 99;

        // Print the grid
        System.out.println("Grid:");
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[row].length; col++) {
                System.out.printf("%3d", grid[row][col]);
            }
            System.out.println();
        }

        // Search for target
        int foundRow = -1;
        int foundCol = -1;

        search:
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[row].length; col++) {
                if (grid[row][col] == target) {
                    foundRow = row;
                    foundCol = col;
                    break search;
                }
            }
        }

        if (foundRow != -1) {
            System.out.println("\nFound " + target + " at [" + foundRow + "][" + foundCol + "]");
        } else {
            System.out.println("\n" + target + " not found in grid");
        }
    }
}
  1. target ← 7

    1public class GridSearch {2    public static void main(String[] args) {3        int[][] grid = {4            {1, 2, 3, 4},5            {5, 6, 7, 8},6            {9, 10, 11, 12}7        };8        int target→ 7 = 7;  //@target=1, 12, 999        10        // Print the grid11        System.out.println("Grid:");12        for (int row = 0; row < grid.length; row++) {
    outputGrid:
  2. for (int row = 0; row < grid.length; row++)

    pass 1 of 3
    11System.out.println("Grid:");12for (int row0 = 0; row < grid.length3; row++) {13    for (int col = 0; col < grid[row].length; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  3. for (int col = 0; col < grid[row].length; col++)

    pass 1 of 12
    12for (int row = 0; row < grid.length; row++) {13    for (int col0 = 0; col < grid[row].length4; col++) {14        System.out.printf("%3d", grid[row][col]1);15    }
    All 12 passes — pass 1 is the card above
    passcolrowgrid[row][col]
    1001
    2102
    3203
    4304
    5015
    6116
    7217
    8318
    9029
    101210
    112211
    123212
  4. System.out.println();

    15    }16    System.out.println();17}
  5. System.out.println();

    15    }16    System.out.println();17}
  6. System.out.println();

    15    }16    System.out.println();17}
  7. foundRow ← -1, foundCol ← -1

    19// Search for target20int foundRow→ -1 = -1;21int foundCol→ -1 = -1;
  8. if (foundRow != -1)

    34if (foundRow1 != -1) {35    System.out.println("\nFound " + target7 + " at [" + foundRow1 + "][" + foundCol2 + "]");36} else {
    output
    Found 7 at [1][2]
  1. target ← 1

    1public class GridSearch {2    public static void main(String[] args) {3        int[][] grid = {4            {1, 2, 3, 4},5            {5, 6, 7, 8},6            {9, 10, 11, 12}7        };8        int target→ 1 = 1;9        10        // Print the grid11        System.out.println("Grid:");12        for (int row = 0; row < grid.length; row++) {
    outputGrid:
  2. for (int row = 0; row < grid.length; row++)

    pass 1 of 3
    11System.out.println("Grid:");12for (int row0 = 0; row < grid.length3; row++) {13    for (int col = 0; col < grid[row].length; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  3. for (int col = 0; col < grid[row].length; col++)

    pass 1 of 12
    12for (int row = 0; row < grid.length; row++) {13    for (int col0 = 0; col < grid[row].length4; col++) {14        System.out.printf("%3d", grid[row][col]1);15    }
    All 12 passes — pass 1 is the card above
    passcolrowgrid[row][col]
    1001
    2102
    3203
    4304
    5015
    6116
    7217
    8318
    9029
    101210
    112211
    123212
  4. System.out.println();

    15    }16    System.out.println();17}
  5. System.out.println();

    15    }16    System.out.println();17}
  6. System.out.println();

    15    }16    System.out.println();17}
  7. foundRow ← -1, foundCol ← -1

    19// Search for target20int foundRow→ -1 = -1;21int foundCol→ -1 = -1;
  8. if (foundRow != -1)

    34if (foundRow0 != -1) {35    System.out.println("\nFound " + target1 + " at [" + foundRow0 + "][" + foundCol0 + "]");36} else {
    output
    Found 1 at [0][0]
  1. target ← 12

    1public class GridSearch {2    public static void main(String[] args) {3        int[][] grid = {4            {1, 2, 3, 4},5            {5, 6, 7, 8},6            {9, 10, 11, 12}7        };8        int target→ 12 = 12;9        10        // Print the grid11        System.out.println("Grid:");12        for (int row = 0; row < grid.length; row++) {
    outputGrid:
  2. for (int row = 0; row < grid.length; row++)

    pass 1 of 3
    11System.out.println("Grid:");12for (int row0 = 0; row < grid.length3; row++) {13    for (int col = 0; col < grid[row].length; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  3. for (int col = 0; col < grid[row].length; col++)

    pass 1 of 12
    12for (int row = 0; row < grid.length; row++) {13    for (int col0 = 0; col < grid[row].length4; col++) {14        System.out.printf("%3d", grid[row][col]1);15    }
    All 12 passes — pass 1 is the card above
    passcolrowgrid[row][col]
    1001
    2102
    3203
    4304
    5015
    6116
    7217
    8318
    9029
    101210
    112211
    123212
  4. System.out.println();

    15    }16    System.out.println();17}
  5. System.out.println();

    15    }16    System.out.println();17}
  6. System.out.println();

    15    }16    System.out.println();17}
  7. foundRow ← -1, foundCol ← -1

    19// Search for target20int foundRow→ -1 = -1;21int foundCol→ -1 = -1;
  8. if (foundRow != -1)

    34if (foundRow2 != -1) {35    System.out.println("\nFound " + target12 + " at [" + foundRow2 + "][" + foundCol3 + "]");36} else {
    output
    Found 12 at [2][3]
  1. target ← 99

    1public class GridSearch {2    public static void main(String[] args) {3        int[][] grid = {4            {1, 2, 3, 4},5            {5, 6, 7, 8},6            {9, 10, 11, 12}7        };8        int target→ 99 = 99;9        10        // Print the grid11        System.out.println("Grid:");12        for (int row = 0; row < grid.length; row++) {
    outputGrid:
  2. for (int row = 0; row < grid.length; row++)

    pass 1 of 3
    11System.out.println("Grid:");12for (int row0 = 0; row < grid.length3; row++) {13    for (int col = 0; col < grid[row].length; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  3. for (int col = 0; col < grid[row].length; col++)

    pass 1 of 12
    12for (int row = 0; row < grid.length; row++) {13    for (int col0 = 0; col < grid[row].length4; col++) {14        System.out.printf("%3d", grid[row][col]1);15    }
    All 12 passes — pass 1 is the card above
    passcolrowgrid[row][col]
    1001
    2102
    3203
    4304
    5015
    6116
    7217
    8318
    9029
    101210
    112211
    123212
  4. System.out.println();

    15    }16    System.out.println();17}
  5. System.out.println();

    15    }16    System.out.println();17}
  6. System.out.println();

    15    }16    System.out.println();17}
  7. foundRow ← -1, foundCol ← -1

    19// Search for target20int foundRow→ -1 = -1;21int foundCol→ -1 = -1;
  8. else

    35    System.out.println("\nFound " + target + " at [" + foundRow + "][" + foundCol + "]");36} else {37    System.out.println("\n" + target99 + " not found in grid");38}
    output
    99 not found in grid

Nested loops to traverse all [row][col] positions.

row-major Process row by row: outer loop = rows, inner loop = columns.

Compare all pairs

Find all pairs that satisfy a condition.

example
Pairs.java
Replay: real traced execution (multi-file project)
public class Pairs {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40};
        int targetSum = 50;

        System.out.println("Find pairs that sum to " + targetSum + ":");

        // All pairs (including same index)
        System.out.println("\nAll pairs with sum " + targetSum + ":");
        for (int i = 0; i < numbers.length; i++) {
            for (int j = i + 1; j < numbers.length; j++) {
                int sum = numbers[i] + numbers[j];
                if (sum == targetSum) {
                    System.out.println("  " + numbers[i] + " + " + numbers[j] + " = " + targetSum);
                }
            }
        }

        // All possible pairs (cartesian product)
        System.out.println("\nAll pairs (order matters):");
        for (int i = 0; i < numbers.length; i++) {
            for (int j = 0; j < numbers.length; j++) {
                if (i != j) {
                    System.out.println("  (" + numbers[i] + ", " + numbers[j] + ")");
                }
            }
        }

    }
}
public class Pairs {
    public static void main(String[] args) {
        int[] numbers = {5, 10, 15, 20, 25};
        int targetSum = 50;

        System.out.println("Find pairs that sum to " + targetSum + ":");

        // All pairs (including same index)
        System.out.println("\nAll pairs with sum " + targetSum + ":");
        for (int i = 0; i < numbers.length; i++) {
            for (int j = i + 1; j < numbers.length; j++) {
                int sum = numbers[i] + numbers[j];
                if (sum == targetSum) {
                    System.out.println("  " + numbers[i] + " + " + numbers[j] + " = " + targetSum);
                }
            }
        }

        // All possible pairs (cartesian product)
        System.out.println("\nAll pairs (order matters):");
        for (int i = 0; i < numbers.length; i++) {
            for (int j = 0; j < numbers.length; j++) {
                if (i != j) {
                    System.out.println("  (" + numbers[i] + ", " + numbers[j] + ")");
                }
            }
        }

    }
}
public class Pairs {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40};
        int targetSum = 30;

        System.out.println("Find pairs that sum to " + targetSum + ":");

        // All pairs (including same index)
        System.out.println("\nAll pairs with sum " + targetSum + ":");
        for (int i = 0; i < numbers.length; i++) {
            for (int j = i + 1; j < numbers.length; j++) {
                int sum = numbers[i] + numbers[j];
                if (sum == targetSum) {
                    System.out.println("  " + numbers[i] + " + " + numbers[j] + " = " + targetSum);
                }
            }
        }

        // All possible pairs (cartesian product)
        System.out.println("\nAll pairs (order matters):");
        for (int i = 0; i < numbers.length; i++) {
            for (int j = 0; j < numbers.length; j++) {
                if (i != j) {
                    System.out.println("  (" + numbers[i] + ", " + numbers[j] + ")");
                }
            }
        }

    }
}
public class Pairs {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40};
        int targetSum = 45;

        System.out.println("Find pairs that sum to " + targetSum + ":");

        // All pairs (including same index)
        System.out.println("\nAll pairs with sum " + targetSum + ":");
        for (int i = 0; i < numbers.length; i++) {
            for (int j = i + 1; j < numbers.length; j++) {
                int sum = numbers[i] + numbers[j];
                if (sum == targetSum) {
                    System.out.println("  " + numbers[i] + " + " + numbers[j] + " = " + targetSum);
                }
            }
        }

        // All possible pairs (cartesian product)
        System.out.println("\nAll pairs (order matters):");
        for (int i = 0; i < numbers.length; i++) {
            for (int j = 0; j < numbers.length; j++) {
                if (i != j) {
                    System.out.println("  (" + numbers[i] + ", " + numbers[j] + ")");
                }
            }
        }

    }
}
  1. targetSum ← 50

    1public class Pairs {2    public static void main(String[] args) {3        int[] numbers = {10, 20, 30, 40};  //@numbers={5, 10, 15, 20, 25}4        int targetSum→ 50 = 50;  //@targetSum=30, 455        6        System.out.println("Find pairs that sum to " + targetSum50 + ":");7        8        // All pairs (including same index)9        System.out.println("\nAll pairs with sum " + targetSum50 + ":");10        for (int i = 0; i < numbers.length; i++) {
    outputFind pairs that sum to 50:
    
    All pairs with sum 50:
  2. for (int i = 0; i < numbers.length; i++)

    pass 1 of 4
    9System.out.println("\nAll pairs with sum " + targetSum + ":");10for (int i0 = 0; i < numbers.length4; i++) {11    for (int j = i + 1; j < numbers.length; j++) {  //#?avoid_duplicates
    All 4 passes — pass 1 is the card above
    passisumtargetSumnumbers[i]numbers[j]j
    10505010403
    21505020302
    32
    43
  3. sum ← 30

    pass 1 of 6
    10for (int i = 0; i < numbers.length; i++) {11    for (int j1 = i0 + 1; j < numbers.length4; j++) {  //#?avoid_duplicates12        int sum→ 30 = numbers[i]10 + numbers[j]20;13        if (sum == targetSum) {
    All 6 passes — pass 1 is the card above
    passjinumbers[i]numbers[j]targetSumsum
    110102030
    220103040
    33010405050
    42120305050
    531204060
    632304070
  4. if (sum == targetSum)

    pass 1 of 2
    12int sum = numbers[i] + numbers[j];13if (sum50 == targetSum50) {14    System.out.println("  " + numbers[i]10 + " + " + numbers[j]40 + " = " + targetSum50);15}
    output  10 + 40 = 50
    values this step0i3j
  5. if (sum == targetSum)

    pass 2 of 2
    12int sum = numbers[i] + numbers[j];13if (sum50 == targetSum50) {14    System.out.println("  " + numbers[i]20 + " + " + numbers[j]30 + " = " + targetSum50);15}
    output  20 + 30 = 50
    values this step1i2j
  6. System.out.println(" All pairs (order matters):");

    19// All possible pairs (cartesian product)20System.out.println("\nAll pairs (order matters):");21for (int i = 0; i < numbers.length; i++) {
    output
    All pairs (order matters):
  7. for (int i = 0; i < numbers.length; i++)

    pass 1 of 4
    20System.out.println("\nAll pairs (order matters):");21for (int i0 = 0; i < numbers.length4; i++) {22    for (int j = 0; j < numbers.length; j++) {
    All 4 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
  8. for (int j = 0; j < numbers.length; j++)

    pass 1 of 16
    21for (int i = 0; i < numbers.length; i++) {22    for (int j0 = 0; j < numbers.length4; j++) {23        if (i != j) {
    16 passes — pass 1 is the card above
    passj
    10
    21
    32
    43
    50
    61
    72
    83
    90
    ⋯ 5 more passes ⋯
    152
    163
  9. if (i != j)

    pass 1 of 12
    22for (int j = 0; j < numbers.length; j++) {23    if (i0 != j1) {24        System.out.println("  (" + numbers[i]10 + ", " + numbers[j]20 + ")");25    }
    output  (10, 20)
    All 12 passes — pass 1 is the card above
    passijnumbers[i]numbers[j]
    1011020
    2021030
    3031040
    4102010
    5122030
    6132040
    7203010
    8213020
    9233040
    10304010
    11314020
    12324030
  1. targetSum ← 50

    1public class Pairs {2    public static void main(String[] args) {3        int[] numbers = {5, 10, 15, 20, 25};4        int targetSum→ 50 = 50;5        6        System.out.println("Find pairs that sum to " + targetSum50 + ":");7        8        // All pairs (including same index)9        System.out.println("\nAll pairs with sum " + targetSum50 + ":");10        for (int i = 0; i < numbers.length; i++) {
    outputFind pairs that sum to 50:
    
    All pairs with sum 50:
  2. for (int i = 0; i < numbers.length; i++)

    pass 1 of 5
    9System.out.println("\nAll pairs with sum " + targetSum + ":");10for (int i0 = 0; i < numbers.length5; i++) {11    for (int j = i + 1; j < numbers.length; j++) {
    All 5 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
    54
  3. sum ← 15

    pass 1 of 10
    10for (int i = 0; i < numbers.length; i++) {11    for (int j1 = i0 + 1; j < numbers.length5; j++) {12        int sum→ 15 = numbers[i]5 + numbers[j]10;13        if (sum == targetSum) {
    All 10 passes — pass 1 is the card above
    passjinumbers[i]numbers[j]sum
    11051015
    22051520
    33052025
    44052530
    521101525
    631102030
    741102535
    832152035
    942152540
    1043202545
  4. System.out.println(" All pairs (order matters):");

    19// All possible pairs (cartesian product)20System.out.println("\nAll pairs (order matters):");21for (int i = 0; i < numbers.length; i++) {
    output
    All pairs (order matters):
  5. for (int i = 0; i < numbers.length; i++)

    pass 1 of 5
    20System.out.println("\nAll pairs (order matters):");21for (int i0 = 0; i < numbers.length5; i++) {22    for (int j = 0; j < numbers.length; j++) {
    All 5 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
    54
  6. for (int j = 0; j < numbers.length; j++)

    pass 1 of 25
    21for (int i = 0; i < numbers.length; i++) {22    for (int j0 = 0; j < numbers.length5; j++) {23        if (i != j) {
    25 passes — pass 1 is the card above
    passj
    10
    21
    32
    43
    54
    60
    71
    82
    93
    ⋯ 14 more passes ⋯
    243
    254
  7. if (i != j)

    pass 1 of 20
    22for (int j = 0; j < numbers.length; j++) {23    if (i0 != j1) {24        System.out.println("  (" + numbers[i]5 + ", " + numbers[j]10 + ")");25    }
    output  (5, 10)
    20 passes — pass 1 is the card above
    passijnumbers[i]numbers[j]
    101510
    202515
    303520
    404525
    510105
    6121015
    7131020
    8141025
    920155
    ⋯ 9 more passes ⋯
    19422515
    20432520
  1. targetSum ← 30

    1public class Pairs {2    public static void main(String[] args) {3        int[] numbers = {10, 20, 30, 40};4        int targetSum→ 30 = 30;5        6        System.out.println("Find pairs that sum to " + targetSum30 + ":");7        8        // All pairs (including same index)9        System.out.println("\nAll pairs with sum " + targetSum30 + ":");10        for (int i = 0; i < numbers.length; i++) {
    outputFind pairs that sum to 30:
    
    All pairs with sum 30:
  2. for (int i = 0; i < numbers.length; i++)

    pass 1 of 4
    9System.out.println("\nAll pairs with sum " + targetSum + ":");10for (int i0 = 0; i < numbers.length4; i++) {11    for (int j = i + 1; j < numbers.length; j++) {
    All 4 passes — pass 1 is the card above
    passisumtargetSumnumbers[i]numbers[j]j
    10303010201
    21
    32
    43
  3. sum ← 30

    pass 1 of 6
    10for (int i = 0; i < numbers.length; i++) {11    for (int j1 = i0 + 1; j < numbers.length4; j++) {12        int sum→ 30 = numbers[i]10 + numbers[j]20;13        if (sum == targetSum) {
    All 6 passes — pass 1 is the card above
    passjinumbers[i]numbers[j]targetSumsum
    11010203030
    220103040
    330104050
    421203050
    531204060
    632304070
  4. if (sum == targetSum)

    12int sum = numbers[i] + numbers[j];13if (sum30 == targetSum30) {14    System.out.println("  " + numbers[i]10 + " + " + numbers[j]20 + " = " + targetSum30);15}
    output  10 + 20 = 30
    values this step0i1j
  5. System.out.println(" All pairs (order matters):");

    19// All possible pairs (cartesian product)20System.out.println("\nAll pairs (order matters):");21for (int i = 0; i < numbers.length; i++) {
    output
    All pairs (order matters):
  6. for (int i = 0; i < numbers.length; i++)

    pass 1 of 4
    20System.out.println("\nAll pairs (order matters):");21for (int i0 = 0; i < numbers.length4; i++) {22    for (int j = 0; j < numbers.length; j++) {
    All 4 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
  7. for (int j = 0; j < numbers.length; j++)

    pass 1 of 16
    21for (int i = 0; i < numbers.length; i++) {22    for (int j0 = 0; j < numbers.length4; j++) {23        if (i != j) {
    16 passes — pass 1 is the card above
    passj
    10
    21
    32
    43
    50
    61
    72
    83
    90
    ⋯ 5 more passes ⋯
    152
    163
  8. if (i != j)

    pass 1 of 12
    22for (int j = 0; j < numbers.length; j++) {23    if (i0 != j1) {24        System.out.println("  (" + numbers[i]10 + ", " + numbers[j]20 + ")");25    }
    output  (10, 20)
    All 12 passes — pass 1 is the card above
    passijnumbers[i]numbers[j]
    1011020
    2021030
    3031040
    4102010
    5122030
    6132040
    7203010
    8213020
    9233040
    10304010
    11314020
    12324030
  1. targetSum ← 45

    1public class Pairs {2    public static void main(String[] args) {3        int[] numbers = {10, 20, 30, 40};4        int targetSum→ 45 = 45;5        6        System.out.println("Find pairs that sum to " + targetSum45 + ":");7        8        // All pairs (including same index)9        System.out.println("\nAll pairs with sum " + targetSum45 + ":");10        for (int i = 0; i < numbers.length; i++) {
    outputFind pairs that sum to 45:
    
    All pairs with sum 45:
  2. for (int i = 0; i < numbers.length; i++)

    pass 1 of 4
    9System.out.println("\nAll pairs with sum " + targetSum + ":");10for (int i0 = 0; i < numbers.length4; i++) {11    for (int j = i + 1; j < numbers.length; j++) {
    All 4 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
  3. sum ← 30

    pass 1 of 6
    10for (int i = 0; i < numbers.length; i++) {11    for (int j1 = i0 + 1; j < numbers.length4; j++) {12        int sum→ 30 = numbers[i]10 + numbers[j]20;13        if (sum == targetSum) {
    All 6 passes — pass 1 is the card above
    passjinumbers[i]numbers[j]sum
    110102030
    220103040
    330104050
    421203050
    531204060
    632304070
  4. System.out.println(" All pairs (order matters):");

    19// All possible pairs (cartesian product)20System.out.println("\nAll pairs (order matters):");21for (int i = 0; i < numbers.length; i++) {
    output
    All pairs (order matters):
  5. for (int i = 0; i < numbers.length; i++)

    pass 1 of 4
    20System.out.println("\nAll pairs (order matters):");21for (int i0 = 0; i < numbers.length4; i++) {22    for (int j = 0; j < numbers.length; j++) {
    All 4 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
  6. for (int j = 0; j < numbers.length; j++)

    pass 1 of 16
    21for (int i = 0; i < numbers.length; i++) {22    for (int j0 = 0; j < numbers.length4; j++) {23        if (i != j) {
    16 passes — pass 1 is the card above
    passj
    10
    21
    32
    43
    50
    61
    72
    83
    90
    ⋯ 5 more passes ⋯
    152
    163
  7. if (i != j)

    pass 1 of 12
    22for (int j = 0; j < numbers.length; j++) {23    if (i0 != j1) {24        System.out.println("  (" + numbers[i]10 + ", " + numbers[j]20 + ")");25    }
    output  (10, 20)
    All 12 passes — pass 1 is the card above
    passijnumbers[i]numbers[j]
    1011020
    2021030
    3031040
    4102010
    5122030
    6132040
    7203010
    8213020
    9233040
    10304010
    11314020
    12324030

Comparing every element with every other element requires O(n²) nested loops.

O(n²) Time complexity when nested loops both iterate n times.

Exercise: Diamond.java

Create a diamond shape pattern with careful loop control