Control Flow
Nested Loops
Loops Inside Loops
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.
public class Multiplication {
public static void main(String[] args) {
int size = ;
// 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 = ;
// 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 = ;
// 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();
}
}
}
Outer loop: each row (1-10). Inner loop: each column (multiply by 1-10). @concept nested loop A loop inside another loop. Inner completes fully for each outer iteration.
Print rectangle of stars
Draw a rectangle pattern with asterisks.
public class Rectangle {
public static void main(String[] args) {
int width = ;
int height = ;
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 = ;
int height = ;
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 = ;
int height = ;
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 = ;
int height = ;
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 = ;
int height = ;
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 = ;
int height = ;
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 = ;
int height = ;
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 = ;
int height = ;
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 = ;
int height = ;
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();
}
}
}
Outer loop: rows. Inner loop: columns within each row.
Print triangle pattern
Create a right triangle by varying inner loop iterations.
public class Triangle {
public static void main(String[] args) {
int height = ;
// 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 = ;
// 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 = ;
// 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();
}
}
}
The inner loop's limit depends on the outer loop variable.
Search in 2D grid
Find a value in a 2D array (matrix).
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 = ;
// 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 = ;
// 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 = ;
// 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 = ;
// 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");
}
}
}
Nested loops to traverse all [row][col] positions.
Compare all pairs
Find all pairs that satisfy a condition.
public class Pairs {
public static void main(String[] args) {
int[] numbers = ;
int targetSum = ;
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 = ;
int targetSum = ;
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 = ;
int targetSum = ;
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 = ;
int targetSum = ;
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 = ;
int targetSum = ;
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 = ;
int targetSum = ;
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] + ")");
}
}
}
}
}
Comparing every element with every other element requires O(n²) nested loops.
Exercise: Diamond.java
Create a diamond shape pattern with careful loop control