Concepts / Program Flow and Control

Program Flow and Control

Every program takes input, performs processing, and produces output—this is the fundamental structure of all programs.

  • Programming

From Data to Result

Whenever a program produces a result, ask three questions: What entered the program? What did the program do with it? What came out? These questions describe the fundamental structure of every program: input, processing, and output. Thinking in these stages gives you a clear way to follow program flow instead of treating the program as one unexplained block.

A program receives input, processes that input, and produces output. The data may change as it moves through the processing stage.

The Three-Stage Pattern

dataresultInputdata entersProcessingdata transformsOutputresult leaves
What happens to data as it moves from input, through processing, to output?

Input is information received by the program. Processing is the work the program performs on that information. Output is the result produced by the program. The pattern is useful even when a program appears complicated, because you can still ask which activity is receiving information, which activity is transforming it, and which activity is presenting the result.

StageRoleLocation in the program model
InputInformation enters the programBoundary with the outside world
ProcessingThe program works with the informationInside the program
OutputThe program provides a resultBoundary with the outside world

The three stages separate external interaction from internal work.

Tracing a Value

A trace follows one piece of data from the moment it enters the program until the program produces its result. At each point, compare the actual value with the value you expected. The first point where they differ identifies the stage that deserves closer inspection.

actual dataactual resultexpected dataexpected resultInput3Input3Processing5Processing6Output5Output6
Where does the data first differ from what was expected as it moves through the program?

Finding the First Divergence

A user provides the value 3. The intended processing adds 3 to that value, but the program adds 2 instead. Trace the actual and expected results.

Input: The actual input is 3, and the expected input is also 3. There is no divergence at the input stage.

Processing: The expected transformation is 3 plus 3, which gives 6. The actual transformation is 3 plus 2, which gives 5. The values first diverge during processing.

Output: The program outputs 5, while the expected output is 6. The output reveals the problem, but the first incorrect stage is processing.

Inspect the processing stage first, because that is where the actual data first differs from the expected data.

The final wrong output does not necessarily mean the output stage is wrong. Trace backward through input, processing, and output to find the first divergence.

Inside and Outside the Program

informationentersused byresultinformationOutside worldinput sourceInputenters programDatastored insideProcessingtransformationOutputleaves programOutside worldreceived result
How does data cross into and out of the program, and which parts happen inside versus outside it?

Input and output are the points where a program interacts with the outside world. Processing and data storage happen inside the program. This distinction helps you describe a failure precisely: the program may have received the wrong information, transformed correct information incorrectly, or presented a correct internal result incorrectly.

Debugging by Stage

  1. Check the input: Did the program receive the information you expected?
  2. Check the processing: Did the program transform the information correctly?
  3. Check the output: Did the program display or provide the internal result correctly?
  4. Identify the first stage where the actual result differs from the expected result.
  5. Focus further investigation on that stage instead of treating the whole program as one undivided problem.
  • Looking only at the final output

    The incorrect value may have been created during input or processing and merely passed through to the output.

    Fix: Trace the data through all three stages and find the first divergence.

  • Ignoring transformations during processing

    Processing may change the data's type, value, or both.

    Fix: Record what the data is like before and after processing.

  • Mixing internal work with external interaction

    Input and output are boundaries, while processing and data storage happen inside the program.

    Fix: Label each observation as input, internal processing or storage, or output.

The Input-Processing-Output pattern is a debugging tool. It narrows a broad complaint such as “the result is wrong” into a more useful question about which stage failed.

Practice the Trace

EASY

A program receives a value, changes it during processing, and then provides a result. The observed result is different from the intended result. Describe the checks you would perform in order, and explain how you would identify the first stage where the actual data differs from the expected data.

Hints
  • Begin at the input boundary.
  • Compare the data before and after processing.
  • Do not assume that the stage showing the wrong final result is the stage that caused the problem.

What do you think happens?

If the input is correct but the final output is wrong, which stage should you compare most closely with its expected result first?

  • Input
  • Processing
  • Outside world only
  • No stage can be checked
Reveal answer

Answer: Processing

When input is confirmed as correct, compare the transformation and its result next. The first divergence may be in processing, although the output stage should also be checked if the internal result is correct.

Key Takeaways

  1. Every program follows the fundamental pattern of input, processing, and output.
  2. Input and output connect the program with the outside world.
  3. Processing and data storage happen inside the program.
  4. Data can change in value, type, or both during processing.
  5. To debug systematically, trace the data and locate the first stage where the actual result differs from the expected result.

Key Takeaways

  • Input, processing, and output form the fundamental structure of every program.
  • Input and output are the program's boundaries with the outside world.
  • Processing and internal data storage occur inside the program and may transform data.
  • Tracing actual and expected data through each stage helps locate the first divergence.
  • The Input-Processing-Output pattern turns debugging into a systematic stage-by-stage check.