Bird
Raised Fist0
Pythonprogramming~5 mins

Nested while loops in Python

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction

Nested while loops let you repeat actions inside other repeated actions. This helps when you want to work with groups inside groups, like rows and columns.

When you want to print a grid or table of values.
When you need to check every item inside a list of lists.
When you want to repeat a process inside another repeated process, like a game board.
When you want to create patterns with characters or numbers.
When you need to handle multiple levels of choices or steps.
Syntax
Python
while condition1:
    # outer loop code
    while condition2:
        # inner loop code

The inner loop runs completely for each time the outer loop runs once.

Indentation is important to show which loop the code belongs to.

Examples
This prints pairs of i and j values, showing how the inner loop runs fully for each i.
Python
i = 1
while i <= 3:
    j = 1
    while j <= 2:
        print(f"i={i}, j={j}")
        j += 1
    i += 1
This multiplies numbers from two loops and prints the results.
Python
count = 1
while count <= 2:
    inner = 1
    while inner <= 3:
        print(count * inner)
        inner += 1
    count += 1
Sample Program

This program prints coordinates in a 3 by 4 grid. The outer loop controls rows, and the inner loop controls columns.

Python
row = 1
while row <= 3:
    col = 1
    while col <= 4:
        print(f"({row},{col})", end=' ')
        col += 1
    print()  # move to next line
    row += 1
OutputSuccess
Important Notes

Make sure the inner loop resets its variable each time the outer loop runs.

Be careful to change loop variables inside loops to avoid infinite loops.

Use print() without arguments to move to the next line after inner loop finishes.

Summary

Nested while loops let you repeat actions inside other repeated actions.

The inner loop runs fully for each time the outer loop runs once.

They are useful for working with grids, tables, or multi-level data.

Practice

(1/5)
1. What does a nested while loop do in Python?
while condition1:
  while condition2:
    # code
easy
A. Runs the inner loop completely for each iteration of the outer loop
B. Runs both loops only once
C. Runs the outer loop only after the inner loop finishes all iterations
D. Runs the inner loop only if the outer loop condition is false

Solution

  1. Step 1: Understand nested loops structure

    The outer loop runs while condition1 is true. For each iteration of this outer loop, the inner loop runs fully while condition2 is true.
  2. Step 2: Explain the inner loop behavior

    The inner loop completes all its iterations before the outer loop moves to the next iteration.
  3. Final Answer:

    Runs the inner loop completely for each iteration of the outer loop -> Option A
  4. Quick Check:

    Inner loop runs fully inside outer loop [OK]
Hint: Inner loop finishes all cycles before outer loop continues [OK]
Common Mistakes:
  • Thinking both loops run only once
  • Confusing order of loop execution
  • Assuming inner loop runs only if outer loop is false
2. Which of the following is the correct syntax for a nested while loop in Python?
easy
A. while x < 3: while y < 2: print(x, y) y += 1
B. while x < 3 while y < 2: print(x, y) y += 1
C. while x < 3: while y < 2: print(x, y) y += 1
D. while x < 3: while y < 2: print(x, y) y += 1

Solution

  1. Step 1: Check indentation and colons

    Python requires a colon after while conditions and proper indentation for nested blocks.
  2. Step 2: Identify correct indentation and syntax

    while x < 3: while y < 2: print(x, y) y += 1 uses colons and indents the inner while and print statements correctly.
  3. Final Answer:

    while x < 3: while y < 2: print(x, y) y += 1 -> Option D
  4. Quick Check:

    Correct colons and indentation = while x < 3: while y < 2: print(x, y) y += 1 [OK]
Hint: Look for colons and consistent indentation [OK]
Common Mistakes:
  • Missing colons after while statements
  • Incorrect indentation of inner loop
  • Mixing indentation levels
3. What is the output of this code?
i = 1
while i <= 2:
    j = 1
    while j <= 3:
        print(i, j)
        j += 1
    i += 1
medium
A. 1 1\n1 2\n2 1\n2 2
B. 1 1\n2 1\n3 1
C. 1 1\n1 2\n1 3\n2 1\n2 2\n2 3
D. 1 1\n1 2\n1 3

Solution

  1. Step 1: Trace outer loop iterations

    i starts at 1 and runs while i <= 2, so i = 1 and i = 2.
  2. Step 2: Trace inner loop iterations for each i

    For each i, j runs from 1 to 3, printing i and j each time.
  3. Final Answer:

    1 1\n1 2\n1 3\n2 1\n2 2\n2 3 -> Option C
  4. Quick Check:

    Inner loop prints 3 times per outer loop iteration [OK]
Hint: Inner loop runs fully for each outer loop iteration [OK]
Common Mistakes:
  • Only printing inner loop once
  • Mixing up i and j values
  • Stopping loops too early
4. Find the error in this nested while loop code:
i = 0
while i < 2:
    j = 0
    while j < 2:
    print(i, j)
    j += 1
    i += 1
medium
A. No error, code runs fine
B. Inner loop print and increments are not indented properly
C. Variables i and j are not initialized
D. Outer loop condition is incorrect

Solution

  1. Step 1: Check indentation inside inner while loop

    The print and j increment lines must be indented inside the inner while loop to run repeatedly.
  2. Step 2: Identify effect of wrong indentation

    Without indentation, print and j += 1 run only once, causing an infinite loop or logic error.
  3. Final Answer:

    Inner loop print and increments are not indented properly -> Option B
  4. Quick Check:

    Indent inner loop body correctly [OK]
Hint: Indent inner loop body to avoid logic errors [OK]
Common Mistakes:
  • Forgetting to indent inner loop code
  • Incrementing outer loop variable inside inner loop
  • Misplacing loop conditions
5. You want to print a 3x3 grid of numbers where each cell shows the sum of its row and column indexes (starting from 1). Which nested while loop code correctly does this?
hard
A. row = 1 while row <= 3: col = 1 while col <= 3: print(row + col, end=' ') col += 1 print() row += 1
B. row = 1 while row <= 3: col = 1 while col <= 3: print(row * col, end=' ') col += 1 print() row += 1
C. row = 0 while row < 3: col = 0 while col < 3: print(row + col, end=' ') col += 1 print() row += 1
D. row = 1 while row < 3: col = 1 while col < 3: print(row + col, end=' ') col += 1 print() row += 1

Solution

  1. Step 1: Check loop ranges for 1 to 3

    We want rows and columns from 1 to 3 inclusive, so conditions should be <= 3 starting at 1.
  2. Step 2: Verify sum calculation and printing format

    row = 1 while row <= 3: col = 1 while col <= 3: print(row + col, end=' ') col += 1 print() row += 1 sums row and col indexes and prints with space, then prints a newline after each row.
  3. Final Answer:

    row = 1 while row <= 3: col = 1 while col <= 3: print(row + col, end=' ') col += 1 print() row += 1 -> Option A
  4. Quick Check:

    Correct ranges and sum print = row = 1 while row <= 3: col = 1 while col <= 3: print(row + col, end=' ') col += 1 print() row += 1 [OK]
Hint: Use <= 3 and start from 1 for correct grid indexes [OK]
Common Mistakes:
  • Starting loops at 0 instead of 1
  • Using < 3 instead of <= 3 to miss last row/column
  • Printing product instead of sum