Computer Science · CSTA 2-AP-11, 2-AP-12 · 信息科技 · Programming

Coding: loops, conditions & variables

When Mars is farthest from Earth, a radio signal takes about 22 minutes to get there. You can’t drive a Mars rover with a joystick — by the time you see it heading for a rock, that happened 22 minutes ago! So engineers write programs: what to repeat (loops), what to do when something happens (conditions) and what to remember (variables). Today you write real code too.

Your mission: start as a Cadet. Code a turtle to draw a square, a star and a flower; program a Mars rover to collect samples and find its way through a canyon; then become a bug hunter and fix broken programs. Top rank: Flight Director.

Missions 1–5: turtle drawing

Write code and press Run. Click a coloured block to insert a command. The turtle starts in the middle, facing up. forward 100 = walk 100 steps; right 90 = turn right 90 degrees. Each mission gives up to 3 stars: correct drawing, the skill it asks for (a loop or a variable), and short code.

0/30total stars (10 missions)
0/10missions done
0programs run
—rank

Missions 6–10: program the Mars rover

In the rover world, forward moves one square, left / right turn 90°, and collect picks up a sample. The rover has sensors: blocked (rock or edge ahead), left_blocked, right_blocked, at_sample, at_base. In the later missions the map changes every run — your program must work on 3 different maps!

📊 Data: how long does a radio signal take, one way?

Radio travels at the speed of light (about 300,000 km or 186,000 miles per second). Drag the slider. Think: if the rover waited for Earth before every step, how far could it get in a day?

—distance
—one-way signal time (double it for a reply)

Data: approximate distances (Moon average about 384,400 km; Mars closest about 54.6 million km, average about 225 million km, farthest about 401 million km); time = distance ÷ speed of light (299,792 km/s).

The big ideas of coding + bug hunt

Scratch blocks, Python or JavaScript — every language is built from the same few ideas. Click an idea to see it in different languages; then find the bug hiding in each program on the right (click the broken line).

🐞 Bug hunt

0/4bugs found on the first click
⚠️ Don’t mix up:In code, “=” usually means assign (put the value on the right into the variable on the left), not “equals”. n = n + 1 means “add 1 to n” — impossible in math, perfectly fine in code. To test equality, use “==”.

Go further

Test yourself

🧠 Quiz

Pick the best answer and read the explanation.

🗂️ Sort it: sequence, loop, condition or variable?

Drag each item (or tap it, then tap a box) into the right group.

👩‍🏫 Teacher notes

US standards: CSTA K-12 CS Standards (2017) 2-AP-11 (clearly named variables and operations on their values), 2-AP-12 (design and iteratively develop programs with nested loops and compound conditionals), 2-AP-13 (decompose problems), 2-AP-17 (systematically test and refine programs using a range of test cases — the 3 random maps), 2-AP-19 (document programs — the language supports # comments). Georgia K-12 Computer Science Standards (grades 6–8): CSS.KC.6-8.20.1 (programming vocabulary: debugging, variables, loops, conditionals, events), 20.7 (store input in a variable), 20.8 (use a loop), 20.9 (make decisions from data or input), 20.10 (debug a program); CSS.CT.6-8.37.1 (block- or text-based languages), 37.2 (variables and data types), 38.3 (loops, conditionals, expressions, variables), 39 (systematic testing with multiple scenarios). Codes as listed in GaDOE’s 6–8 CS framework (via CodeHS); check wording against the GaDOE document.

China’s curriculum: Compulsory Education Information Technology Curriculum Standard (2022), core strand “algorithms”. Stage 3: grade 5 “Algorithms around us” (身边的算法: the three basic structures — sequence, branch, loop) and grade 6 “Process and control” (过程与控制: control, feedback and decisions by program); Stage 4 grade 8 “Internet of Things practice” (物联网实践与探索) uses programming to control devices. Textbook editions and lesson numbers vary.

Notes: the language on this page is a small teaching language (Logo-like) run by an interpreter written in the page — no eval, no network, and a 20,000-step limit stops endless loops. Mars distances are approximate; light speed 299,792 km/s. Photo captions follow the Wikimedia Commons file descriptions.

Suggested use: start unplugged by “programming a partner” (CS Unplugged Kidbots). After mission 4 ask how the value of side changes step by step. Before mission 10, sketch a canyon on paper and discuss what the rover should decide at each corner. Encourage Step mode and reading the line number in error messages.