Nested Iteration · 嵌套迭代
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| nested iteration/ˈnestɪd ˌɪtəˈreɪʃn/ | 嵌套迭代 | qiàn tào dié dài |
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.
循环里的循环
- 嵌套迭代把一个循环放进另一个的主体里面。
- 内层循环完整运行——对外层循环的每一遍。
- 外层运行
m次,内层每次运行n次 → 内层主体运行m × n次。 - 嵌套处理网格、表格和所有配对的比较。
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.
行与列
- 一个常见用途:外层循环管行,内层循环管列。
for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { ... } }- 对每一行,内层循环扫过所有列,外层才前进。
- 这按顺序访问二维网格的每个单元格。
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.
追踪顺序
- 内层循环跑完它所有的遍,然后外层走一步。
- 外层
r=0:内层c=0,1,2;外层r=1:内层c=0,1,2;如此类推。 - 在每个外层遍开始时重置任何内层计数器。
- 慢慢追踪——弄混哪个循环在前进是常见错误。
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.
留意内层变量
- 把内层循环的计数器声明在里面,或在每个外层遍重置它。
- 如果你在内层条件里复用一个外层变量,留意干扰。
- 内层和外层计数器应当独立,除非你有意为之。
- 一个三角形模式(
c从0到r)让内层次数依赖外层。
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.
**在嵌套循环里,内层循环对每一个外层遍都完整运行。**所以内层主体执行 外层 × 内层 次——一个 10 × 10 的嵌套把内层主体运行 100 次,而非 20 次。在每个外层遍重置内层计数器(在内层 for 头里声明它会自动做到这点),否则第二个外层遍会从第一个结束的地方开始。
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.
打印一个 3×3 的星号网格:
for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { print("*"); } println(); }- 外层运行
3次;内层每次3→ 共9个星号。 - 每个外层遍打印一行三个,然后换行。
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.
嵌套迭代把一个循环放进另一个;内层循环对每个外层遍完整运行,所以内层主体执行 外层 × 内层 次。它是网格的模式(外层 = 行,内层 = 列)。每个外层遍重置内层计数器,并仔细追踪——内层在外层前进之前结束。
Nested loops sweep a grid · 嵌套循环扫过网格
Outer = rows, inner = columns; the inner loop fills each row. · 外层 = 行,内层 = 列;内层循环填满每一行。
Outer loop runs 3 times, inner runs 3 times each. How many times does the inner body run? · 外层循环运行 3 次,内层每次运行 3 次。内层主体运行多少次?
3 × 3 = 9. · 3 × 3 = 9。
In nested loops, the inner loop runs fully for every single pass of the outer loop. · 在嵌套循环里,内层循环对外层循环的每一遍都完整运行。
That's why the inner body runs outer × inner times. · 这就是内层主体运行 外层 × 内层 次的原因。
For a 2-D grid, which loop typically handles the columns? · 对二维网格,哪个循环通常管列?
Outer = rows, inner = columns. · 外层 = 行,内层 = 列。
A 10 × 10 nested loop runs its inner body how many times? · 一个 10 × 10 的嵌套循环把它的内层主体运行多少次?
10 × 10 = 100. · 10 × 10 = 100。
Declaring the inner counter in the inner for-header resets it each outer pass. · 在内层 for 头里声明内层计数器会在每个外层遍重置它。
A fresh int i in the inner header restarts it each time. · 内层头里一个新的 int i 每次都重新开始。