6 · Loops & conditionals (making decisions)
Sequencing alone gets long and repetitive. Two more ideas make code powerful: loops and conditionals. With just sequencing, loops, and conditionals, you can describe almost any behavior a robot needs.
Loops — do it again. A loop tells the computer to repeat steps instead of writing them out over and over. If you want a robot to take 50 steps, you don't write "step forward" 50 times — you write it once inside a loop that says "repeat 50 times." Code.org describes a loop simply as "the action of doing something over and over again," and teaches kinds like a while loop (repeat while something is true) and an until loop (repeat until something becomes true) (Code.org, n.d.).
Conditionals — decide. A conditional runs code only if a condition is true. This is how a machine makes choices. The shape is if / else:
IF the path ahead is clear:
drive forward
ELSE:
turn right
Code.org explains that conditionals "are statements that only run under certain conditions," and that if/else lets a program "run entire sections of code only if something is true, otherwise it runs something else" (Code.org, n.d.). That single idea is what makes a robot seem smart: it can react differently to different inputs.
Put it together and a line-following robot is just: loop forever { IF I see the line, go straight; ELSE, steer back toward it }. Sensing (input), deciding with a conditional (process), moving (output) — over and over in a loop. You already understand the whole machine.
Try this (unplugged). Be the robot. A partner gives you a tiny program out loud, like: "Loop 4 times: take one step; IF you reach the wall, stop." Act it out literally. Then make the rule trickier and see if the program still works. Spotting where it breaks is exactly what programmers do.
Check yourself. What does a loop do, and what does a conditional (if/else) do? Give one real robot behavior that needs both.
Sources
Code.org. (n.d.). Computer science fundamentals curriculum. https://code.org/en-US/curriculum/computer-science-fundamentals