Computer engineeringLevel 2 · Builder
Decisions and loops
Programs become useful when they can choose and repeat. An if statement runs a block of code only when a condition is true, and elif and else handle the other cases in order, so only the first matching branch runs. Conditions are built from comparisons joined by the Boolean operators and, or and not. A for loop repeats a block once for each item in a sequence, such as every reading in a list. A while loop repeats for as long as a condition stays true, so something inside it must eventually make that condition false. Most loop bugs are off-by-one mistakes on the first or last pass, or loops that never end.
Why it mattersA line-following robot's code is a loop that runs many times a second and decides on every pass whether to steer left, steer right or go straight.