From Plan to Program: Variables, Conditionals, and Loops
Byte, a friendly luminous robot guide, stands at a glowing holographic whiteboard tracing a flowchart with arrows that light up as code blocks assemble beside it in a tidy classroom workshop.
- Identify the four core programming constructs — variables, sequencing, selection, and iteration — and distinguish variables as a data building block from the three control-flow constructs.
- Translate a plain-language algorithm into ordered pseudocode steps.
- Apply an if/else conditional to choose between two outcomes based on a condition.
- Construct a loop that repeats a step a controlled number of times without running forever.
- Trace control flow through a short program to predict its output.
Key terms
- Variable
- A named container storing a value that can be read or changed later.
- Sequencing
- Running statements one at a time in top-to-bottom written order.
- Selection
- Choosing which branch to run based on whether a condition is true.
- Iteration
- Repeating a block of statements while a stopping condition stays unmet.
- Control flow
- The order in which a program's statements actually execute.
Data Versus Control-Flow Blocks
AP CS Principles draws a precise line that students often miss: a variable is a data building block, while sequencing, selection, and iteration are control-flow building blocks. A variable holds information but does not by itself decide which statements run next. Control flow determines order and choice. This distinction matters because a question can describe storing a value, which is a variable, and tempt you to call it selection because the value is later tested. Storing is not deciding; only the condition test in an if/else performs the branching choice that defines selection.
Anatomy of a Safe Loop
Every loop needs three pieces working together to terminate correctly: an initialization that sets a starting point, a stopping condition checked before or after each pass, and an update that moves the state toward that condition. Omit the update and the condition never changes, so the loop runs forever, the classic infinite-loop bug. For example, while count <= 3 with count starting at 1 must include 'add 1 to count' each pass, or it prints 1 endlessly. Reading any loop, locate all three pieces and confirm the update genuinely drives the condition toward false.
Worked examples
Trace this loop: set count = 1; while count <= 3 print count then add 1 to count.
- count = 1; condition 1 <= 3 is true, so print 1, then update count to 2.
- count = 2; condition 2 <= 3 is true, so print 2, then update count to 3.
- count = 3; condition 3 <= 3 is true, so print 3, then update count to 4.
- count = 4; condition 4 <= 3 is false, so the loop stops.
Answer: The loop prints 1, 2, 3 and then terminates because the update makes the condition false.
Activity
Put these pseudocode steps in the correct order to count up and announce the result
Practice
Predict the output of: set x = 2; if x > 5 print 'big' else print 'small'.
Identify why a while loop with no update statement never stops running.
Common mistakes to avoid
- Selection runs both branchesAn if/else runs exactly one branch, never both, depending on the condition's truth value.
- A loop stops on its own at the limitWithout an update moving toward the stopping condition, the loop runs forever.
Check your understanding
Which construct lets a program choose between two different actions based on whether a condition is true?
What does this code print? set x = 2; if x > 5 then print 'big' else print 'small'
A loop is written: set n = 1; while n <= 3, print n (but n is never changed). What happens?
Why does the order of statements matter when you write code?
Recap
Programs are built from one data block, the variable, and three control-flow blocks: sequencing runs statements in order, selection chooses a branch by condition, and iteration repeats with initialization, a stopping condition, and an update that prevents infinite loops.
Reflect
Why does hand-tracing each variable after every line catch bugs that just reading code misses?