Concepts / Program Flow and Control

Program Flow and Control

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

  • 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.

Key point

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

Inputdata entersProcessingdata transformsOutputresult leavesdataresult
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.

Input3Input3Processing5Processing6Output5Output6actual dataactual resultexpected dataexpected result
Where does the data first differ from what was expected as it moves through the program?
Worked example

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.

  1. 1
    Input

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

  2. 2
    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.

  3. 3
    Output

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

Result

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

Key point

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

Outside worldinput sourceInputenters programDatastored insideProcessingtransformationOutputleaves programOutside worldreceived resultinformationentersused byresultinformation
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. 1
    Check the input: Did the program receive the information you expected?
  2. 2
    Check the processing: Did the program transform the information correctly?
  3. 3
    Check the output: Did the program display or provide the internal result correctly?
  4. 4
    Identify the first stage where the actual result differs from the expected result.
  5. 5
    Focus further investigation on that stage instead of treating the whole program as one undivided problem.
  • Common mistake

    Looking only at the final output

    What it looks like

    A result is wrong, so the output stage is blamed immediately.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Ignoring transformations during processing

    What it looks like

    The input is recorded, but the value is treated as though it stayed unchanged.

    Why it fails

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

    Do this instead

    Record what the data is like before and after processing.

  • Common mistake

    Mixing internal work with external interaction

    What it looks like

    The way a result is received outside the program is treated as though it were the same as the program's internal calculation.

    Why it fails

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

    Do this instead

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

Key point

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

At a glance

  • 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.

Related concepts

More concepts starting with P