Algorithms with Selection and Repetition
The Three Building Blocks
Every algorithm, no matter how complex, is built from just three ingredients:
- Sequence — steps run in order, one after another.
- Selection — branch based on a true/false decision (
if/else). - Repetition — repeat a block until some condition is met (loops).
That's genuinely it. Every autonomous routine, every teleop control loop, every piece of robot code you'll ever write is some combination of these three.
Planning Before Coding
For anything beyond a couple of lines, it helps to plan the steps before writing real Java — in plain English, a diagram, or pseudocode (informal step-by-step notes that aren't real code, but capture the sequence/selection/repetition structure). This is the same idea from Lesson 1.2, applied to a bigger problem.
Pseudocode for a simple autonomous routine:
Drive forward until the distance sensor reads < 12 inches
If a game piece is detected, run the intake
Otherwise, back up and try a different path
Only after the steps are clear does it make sense to translate each line into real Java.
Common Pitfalls
- Skipping the planning step for a multi-branch routine. Jumping straight to code for anything with several conditions or a loop is exactly how logic errors sneak in — a wrong branch order, a missed case.
- Assuming "selection" only means
if. Any true/false branching decision counts, including the conditions inside loops.
Key Takeaways
- Every algorithm is built from sequence, selection, and repetition — nothing else is needed.
- Pseudocode (informal step-by-step notes) is a real planning tool, not busywork — use it before writing real code for anything with multiple branches or a loop.
Derived from csawesome-2026: unit-2-selection-and-iteration/2.01-algorithms-with-selection-and-repetition.md (CSA 2.1)