Lặp lồng
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| nested iteration/ˈnestɪd ˌɪtəˈreɪʃn/ | lặp lồng |
A loop inside a loop
- Nested iteration 嵌套迭代 puts one loop inside another's body.
- The inner loop runs completely for each pass of the outer loop.
- Outer runs
mtimes, inner runsntimes each → the inner body runsm × ntimes. - Nesting handles grids, tables, and all-pairs comparisons.
Rows and columns
- A common use: outer loop for rows, inner loop for columns.
for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { ... } }- For each row, the inner loop sweeps all columns before the outer advances.
- This visits every cell of a 2-D grid in order.

The inner loop runs completely for each pass of the outer loop
Tracing the order
- The inner loop finishes all its passes, then the outer takes one step.
- Outer
r=0: innerc=0,1,2; outerr=1: innerc=0,1,2; and so on. - Reset any inner counter at the start of each outer pass.
- Trace slowly — mixing up which loop advances is a common mistake.
Watch the inner variable
- Declare the inner loop's counter inside or reset it each outer pass.
- If you reuse an outer variable in the inner condition, watch for interference.
- The inner and outer counters should be independent unless you intend otherwise.
- A triangle pattern (
cfrom0tor) makes the inner count depend on the outer.
In nested loops, the inner loop runs FULLY for every single outer pass. So the inner body executes outer × inner times — a 10 × 10 nest runs its inner body 100 times, not 20. Reset the inner counter at each outer pass (declaring it in the inner for header does this automatically), or the second outer pass starts where the first left off.
Printing a 3×3 grid of stars:
for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { print("*"); } println(); }- Outer runs
3times; inner runs3each →9stars total. - Each outer pass prints one row of three, then a newline.
Nested iteration puts a loop inside another; the inner loop runs fully for each outer pass, so the inner body executes outer × inner times. It's the pattern for grids (outer = rows, inner = columns). Reset the inner counter each outer pass, and trace carefully — the inner finishes before the outer advances.
Vòng lặp lồng nhau quét một lưới
Ngoài = hàng, trong = cột; vòng lặp trong điền đầy mỗi hàng.
Vòng lặp ngoài chạy 3 lần, vòng lặp trong chạy 3 lần mỗi lần. Thân của vòng lặp trong chạy bao nhiêu lần?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
3 × 3 = 9.
Trong vòng lặp lồng nhau, vòng lặp trong chạy trọn vẹn cho mỗi lần lặp đơn lẻ của vòng lặp ngoài.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Đó là lý do thân của vòng lặp trong chạy ngoại × trong số lần.
Đối với một lưới 2-D, vòng lặp nào thường xử lý các cột?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Ngoài = hàng, trong = cột.
Một vòng lặp lồng nhau 10 × 10 chạy thân của nó bao nhiêu lần?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
10 × 10 = 100.
Khai báo bộ đếm bên trong trong header for bên trong sẽ đặt lại nó mỗi lần lặp ngoài.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Một int i mới trong header bên trong khởi động lại nó mỗi lần.