上一片,小电脑有了盒子 total。可主人要数 100 包货,一行行写到天黑。这一片,让电脑自己转圈数。Last leaf, the little computer got its box total. Now the owner must count 100 parcels — writing 100 lines lasts till dark. This leaf lets the computer run laps itself.
第 1 步:再来一圈Step 1: One more round
100 行代码,能压成几行?Can 100 lines of code shrink to a few?
循环就是“重复跑”。跑步机上,第 3 圈和第 1 圈动作一样。写一次,让电脑跑 100 次。数 1 到 100,一个循环搞定。A loop means “run again”. On a treadmill, lap 3 copies lap 1. Write once, let the computer run 100 times. Counting 1 to 100 takes one loop.
电脑怎么知道何时停?How does the computer know when to stop?
第 2 步:说清跑几次Step 2: Say how many runs
三个词说清:起始、终止、步长。从 0 到 10,步长 5。只经过 0、5、10。闹钟也一样:每天 7 点,响 7 天就是 7 次。Three words fix it: start, end, step. From 0 to 10, step 5. Only 0, 5, 10 pass by. Alarms match: 7 o'clock daily, 7 days means 7 rings.
停的条件丢了,会怎样?What if the stop rule is lost?
第 3 步:忘了说停Step 3: Forgot to say stop
会转成死循环!小电脑晕头转向,停不下来。写循环先写停的条件。这是新手最常见的坑,检查一遍再跑。You get an endless loop! The little computer spins dizzy, never stopping. Always write the stop rule first. It is the top rookie trap — check before you run.
🎮 你来当教练(30 秒)🎮 Your turn: be the coach (30 seconds)
道理讲完了。先拖滑块跑几遍,再接 4 关:预测跑几次,揪出死循环。Theory done. First drag the sliders and run a few rounds, then take 4 challenges: predict the count and catch the endless loop.
一句话记住它:循环 = 让电脑重复干活;起始 / 终止 / 步长说清次数;丢了终止条件就是死循环。Remember it in one line: a loop = make the computer repeat; start / end / step fix the count; losing the stop rule is an endless loop.
一次写完重复跑:数到 100 不用 100 行Write once, repeat: count to 100 without 100 lines三件套:起始 / 终止 / 步长The trio: start / end / step先写停的条件,别写死循环Write the stop rule first — no endless loops
内容参考 OpenStax 等公开教材,多来源核对 · AI 生成、人工审核 · 发现错误欢迎指正,帮这片叶子长得更好。Based on OpenStax and other open textbooks, cross-checked across sources · AI-generated, human-reviewed · Spotted a mistake? Tell us — help this leaf grow.