liveBook · Manning

Nested loop

A nested loop is a loop placed inside another loop so that the inner loop executes within each iteration of the outer loop.

How 4 Manning books explain it

Get Programming: Learn to code with Python

A nested loop is a loop placed inside another loop so that its execution occurs within each iteration of the outer loop.

How it is used
Uses nested loops to test every letter of each candidate word against available Scrabble tiles.
How deep it goes
Explains outer and inner responsibilities, early exit, duplicate handling, remaining state, and the final completion check.
Examples
Demonstrates word candidates, individual letters, tile removal, duplicate letters, and words such as art, hue, and color.
What you can do after
Build it from scratch by translating a two-level checking process into pseudocode and executable loop logic.

Read how Get Programming: Learn to code with Python explains nested loop →

Learn AI-Assisted Python Programming, Second Edition

A nested loop is a loop whose body contains another loop, causing the inner loop to complete its iterations for each iteration of the outer loop.

How it is used
Processes every combination or every cell in row-and-column data.
How deep it goes
Explains execution order and list-of-lists traversal, but stops short of performance analysis.
Examples
Uses country pairs and an Olympic-medals list to demonstrate nested iteration.
What you can do after
Apply nested Python for loops to generate combinations and visit two-dimensional list values.

Read how Learn AI-Assisted Python Programming, Second Edition explains nested loop →

CUDA for LLMs

A nested loop is a loop construct placed inside one or more other loop constructs to iterate over multiple dimensions or levels.

How it is used
Uses nested loops to express matrix operations and as a starting point for mapping independent work onto GPU threads.
How deep it goes
Traces loop roles through matrix addition, GEMM, and tiled attention, including which iterations are parallelized or retained.
Examples
Uses matrix addition, triple-loop GEMM, neural-network forward-pass operations, and nested Q/K/V tile loops in Flash Attention.
What you can do after
Build a naive GEMM kernel by assigning each output element to a thread while retaining its dot-product loop.

Read how CUDA for LLMs explains nested loop →

Practices of the Python Pro

A nested loop is a loop construct placed inside the body of another loop, causing repeated iteration across multiple levels.

How it is used
Uses nested loops to identify quadratic growth while analyzing algorithm performance.
How deep it goes
Explains the repeated comparisons, early termination, and worst-case O(n²) behavior without extending into optimization techniques.
Examples
Demonstrates duplicate detection with Python loops over a sequence.
What you can do after
Evaluate nested-loop code by recognizing its potential quadratic worst-case running time.

Read how Practices of the Python Pro explains nested loop →

Also covered in

What to know first

Related concepts

Questions about nested loop

What is nested loop?

A nested loop is a loop placed inside another loop so that the inner loop executes within each iteration of the outer loop.

Which Manning books explain nested loop?

Get Programming: Learn to code with Python, Learn AI-Assisted Python Programming, Second Edition, CUDA for LLMs and Practices of the Python Pro.

What should I know before learning nested loop?

Manning books that teach nested loop usually introduce loop and indexing first.

What is the difference between nested loop and CUDA kernel?

A nested loop is a loop placed inside another loop so that the inner loop executes within each iteration of the outer loop. A CUDA kernel is a function launched on a GPU for execution by multiple threads in parallel.

What is nested loop a kind of?

Nested loop is a kind of loop. A loop is a programming-language control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.