Menu
Coddy logo textTech

Nested Loop

Part of the Fundamentals section of Coddy's Swift journey — lesson 49 of 86.

A nested loop is simply a loop inside another loop. The inner loop completes all its iterations for each single iteration of the outer loop.

for row in 1...3 {
    for col in 1...2 {
        print("Row \(row), Col \(col)")
    }
}
// Output:
// Row 1, Col 1
// Row 1, Col 2
// Row 2, Col 1
// Row 2, Col 2
// Row 3, Col 1
// Row 3, Col 2

For each value of row, the inner loop runs completely through all values of col. So with 3 outer iterations and 2 inner iterations, the code inside runs 6 times total (3 × 2).

Nested loops are commonly used to work with grid-like structures or to generate patterns.

Here's an example that prints a simple rectangle of asterisks:

for row in 1...2 {
    for col in 1...4 {
        print("*", terminator: "")
    }
    print("")  // Move to next line
}
// Output:
// ****
// ****

The terminator: "" prevents print from adding a newline, so asterisks appear on the same row. After the inner loop finishes, print("") moves to the next line.

challenge icon

Challenge

Easy

Read two integers from input: rows and cols. Use nested loops to print a grid of numbers where each cell displays the product of its row number and column number.

You will receive the following inputs:

  • First line: number of rows (e.g., "3")
  • Second line: number of columns (e.g., "4")

Requirements:

  • Use an outer loop for rows (1 to rows)
  • Use an inner loop for columns (1 to cols)
  • Print the product of the current row and column numbers
  • Separate values on the same row with a space
  • Each row should be on its own line

Hint: Use terminator: " " to keep values on the same line, then use print("") after the inner loop to move to the next line.

For example, if rows is 3 and cols is 4, the output should be:

1 2 3 4 
2 4 6 8 
3 6 9 12 

Cheat sheet

A nested loop is a loop inside another loop. The inner loop completes all its iterations for each single iteration of the outer loop.

for row in 1...3 {
    for col in 1...2 {
        print("Row \(row), Col \(col)")
    }
}
// Output:
// Row 1, Col 1
// Row 1, Col 2
// Row 2, Col 1
// Row 2, Col 2
// Row 3, Col 1
// Row 3, Col 2

With 3 outer iterations and 2 inner iterations, the code runs 6 times total (3 × 2).

Nested loops are useful for grid-like structures and patterns:

for row in 1...2 {
    for col in 1...4 {
        print("*", terminator: "")
    }
    print("")  // Move to next line
}
// Output:
// ****
// ****

Use terminator: "" to prevent print from adding a newline, keeping output on the same line. Use print("") after the inner loop to move to the next line.

Try it yourself

let rows = Int(readLine()!)!
let cols = Int(readLine()!)!

// TODO: Write your code below
// Use nested loops to print a multiplication grid
// Outer loop: iterate through rows (1 to rows)
// Inner loop: iterate through columns (1 to cols)
// Print the product of row * column
// Use print(..., terminator: " ") to keep values on the same line
// Use print("") after the inner loop to move to the next line
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals