AP® Computer Science Principles · Big Idea 1 · Topic 1.2

Program Outputs and Flow of Control

A program's output depends on both the input it just got and the state it already had stored. Flow of control is the order in which instructions run, and that order decides how state and input combine.

Press the same button twice and you can get two different results, because the program is not just reacting to your click, it's reacting to your click plus whatever it already remembers. That memory is called state: values stored in variables that persist across actions. A counter program doesn't just know you clicked, it knows you clicked while already sitting at 7. Output is really a function of two things together, current state and new input, and flow of control is the sequence of steps the program follows to combine them.

Flow of control usually runs top to bottom, but it bends: a condition can skip a block, a loop can repeat one, an event like a click can jump execution to a handler that updates state immediately. Each time that handler runs, it reads the existing variable, changes it, and writes the new value back, so the next click starts from a different place than the last one did. A reset is just another instruction in that flow: it overwrites state back to a known value, breaking the chain so later input no longer depends on earlier input.

This matters beyond single programs. The variables a program keeps are its data abstractions, the meaningful quantities it tracks to represent something real, like a score or a total. Output is only trustworthy if that internal state is managed on purpose: reset when it should be, preserved when it shouldn't be. Leftover state that nobody resets is one of the most common sources of a program that 'used to work' and then mysteriously doesn't.

Key terms

State
Values stored in a program's variables that persist across actions or events.
Flow of control
The order in which a program's instructions actually execute, including branches and loops.
Input
Data a program receives from outside itself, such as a click or typed value.
Output
What a program displays or produces as a result of combining input with its current state.
Data abstraction
A named variable or structure that represents a meaningful quantity the program tracks.

Watch it explained

  1. How it worksHow does a program combine its stored internal state with fresh input to decide what to output?
  2. Worked exampleTrace the output of a counter program starting at 0 that receives four clicks and then a reset.
  3. Going furtherWhat happens to output when a program has state it never resets between runs?
  4. Ties togetherHow do the inputs and outputs of a program connect to the data abstractions it keeps inside?

Check yourself

Your turn1 of 2
A counter program starts at 0. It receives 14 clicks, each adding 1, is then reset back to 0, and afterward receives 13 more clicks. What value does the counter display now?

End of topic.