for Loops · for 循环
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| for loop/fɔː luːp/ | 计数循环 | jì shù xún huán |
A loop with a counter built in
- A for loop 计数循环 packs three parts into one line:
for (int i = 0; i < 5; i++) { ... }- Initialization (
int i = 0), condition (i < 5), update (i++). - Use
forwhen you know how many times to repeat.
内建计数器的循环
- for 循环把三个部分打包进一行:
for (int i = 0; i < 5; i++) { ... }- 初始化(
int i = 0)、条件(i < 5)、更新(i++)。 - 当你知道要重复多少次时用
for。
The three parts, in order
- Initialization runs once, before the loop starts.
- The condition is checked before each pass (like
while). - The update runs after each pass, then the condition is re-checked.
- So the flow is: init → (test → body → update) → (test → body → update) → …
三个部分,按序
- 初始化在循环开始前运行一次。
- 条件在每遍之前检查(像
while)。 - 更新在每遍之后运行,然后再次检查条件。
- 所以流程是:初始化 →(检验 → 主体 → 更新)→(检验 → 主体 → 更新)→……
for and while are interchangeable
- Any
forloop can be rewritten as awhileloop, and vice versa. for (int i=0; i<5; i++) {B}equalsint i=0; while (i<5) {B; i++;}.foris tidier when the counter setup, test, and change belong together.- Choose whichever makes the loop's intent clearest.
for 与 while 可互换
- 任何
for循环都能改写成while循环,反之亦然。 for (int i=0; i<5; i++) {B}等于int i=0; while (i<5) {B; i++;}。- 当计数器的设置、检验和改变属于一起时,
for更整洁。 - 选择让循环意图最清晰的那种。
Counting a known number of times
for (int i = 0; i < n; i++)runs the body exactlyntimes (i=0ton-1).- Change the bounds to count differently:
i = 1; i <= nrunsntimes too. - Watch the boundary:
<stops one before,<=includes the end. - This is the standard pattern for "do something
ntimes."
数已知的次数
for (int i = 0; i < n; i++)恰好把主体运行n次(i=0到n-1)。- 改变边界以不同方式计数:
i = 1; i <= n也运行n次。 - 留意边界:
<在前一个停止,<=包含末尾。 - 这是“做某事
n次”的标准模式。
Mind the loop bounds — an off-by-one changes the count. for (int i = 0; i < n; i++) runs n times (i from 0 to n-1); switching to i <= n runs n+1 times and often overruns an array. Trace the first and last value of i to confirm the count before trusting a for loop.
留意循环边界——一个差一会改变次数。for (int i = 0; i < n; i++) 运行 n 次(i 从 0 到 n-1);换成 i <= n 运行 n+1 次,常常会越过数组末尾。信任一个 for 循环之前,追踪 i 的第一个和最后一个值来确认次数。
Printing 0,1,2,3,4:
for (int i = 0; i < 5; i++) { System.out.println(i); }istarts0, runs whilei < 5,i++each pass.- Prints
0 1 2 3 4(five values); ati = 5the condition fails → stop.
打印 0,1,2,3,4:
for (int i = 0; i < 5; i++) { System.out.println(i); }i从0开始,在i < 5时运行,每遍i++。- 打印
0 1 2 3 4(五个值);在i = 5时条件不满足 → 停止。
A for loop combines initialization (once), a condition (checked before each pass), and an update (after each pass): for (int i = 0; i < n; i++) runs the body n times. It's interchangeable with a while loop. Mind the bounds — < vs <= changes the count by one.
for 循环把初始化(一次)、条件(每遍之前检查)和更新(每遍之后)组合起来:for (int i = 0; i < n; i++) 把主体运行 n 次。它与 while 循环可互换。留意边界——< 对 <= 会让次数差一。
A for loop counting 0 to 4 · 从 0 数到 4 的 for 循环
i runs 0..4 (five passes); at i=5 the condition fails. · i 走 0..4(五遍);在 i=5 时条件不满足。
The three parts of a for-loop header are, in order... · for 循环头的三个部分,按顺序是……
for (init; condition; update). · for (初始化; 条件; 更新)。
How many times does the body of for (int i = 0; i < 5; i++) run? · for (int i = 0; i < 5; i++) 的主体运行多少次?
i takes 0,1,2,3,4 → 5 passes. · i 取 0,1,2,3,4 → 5 遍。
The initialization part of a for loop runs... · for 循环的初始化部分运行……
Init runs once; condition is checked each pass; update runs after each. · 初始化运行一次;条件每遍检查;更新在每遍后运行。
Any for loop can be rewritten as an equivalent while loop. · 任何 for 循环都能改写成等价的 while 循环。
for and while are interchangeable. · for 与 while 可互换。
How many times does for (int i = 1; i <= 5; i++) run? · for (int i = 1; i <= 5; i++) 运行多少次?
i takes 1..5 (inclusive) → 5 passes. · i 取 1..5(含)→ 5 遍。