Concepts / Variables and Data Storage

Variables and Data Storage

In the source scenario, the data is a list of student test scores.

  • Programming

Following the Program's Path

Every program is a series of instructions that a computer executes. The important question is not only what instructions exist, but also when each instruction runs. Some instructions run one after another in a fixed order. Some run only when a condition is true. Others run repeatedly, often with some variation, until a stopping condition is met. These three execution patterns work together whenever a program processes data, makes decisions, or automates a task.

A useful way to trace execution is to track both the current instruction and the data being processed. In the source scenario, the data is a list of student test scores. The program reads the scores, checks an average against 70, congratulates qualifying students, and then prints a summary.

Read scoresCheck averageabove 70?Process studentsCongratulationsPrint summarynextyesnoqualifying studentnext studentfinished
What happens as the program reads scores, makes a decision, repeats an action, and finishes with a summary?

Reading Instructions in Order

Sequential execution means that statements run one after another in the order they appear in the script. The program begins with the first instruction, completes it, and then moves to the next instruction. Unless another execution pattern changes the path, the order of the statements determines the order of the work.

Worked example

Reading the Scores Sequentially

Trace the first part of the student-score program before it makes a decision.

  1. 1
    First instruction

    The program reads the student scores in order.

  2. 2
    Next instruction

    After reading the scores, the program determines the average score.

  3. 3
    Next decision

    Only after those earlier steps does the program check whether the average is above 70.

Result

The order of the instructions establishes the initial path: read the scores, determine the average, and then evaluate the condition.

Student scoresAverage scoreAverage checkSummarythentheneventually
In what order does the program execute the main statements from beginning to end?

Letting Conditions Choose

Conditional execution checks a condition and uses its result to decide what happens next. If the condition is satisfied, the program runs the associated block of statements. If it is not satisfied, the program skips that block. The condition therefore changes the path through the instructions without changing the fact that the program follows an ordered execution process.

In the student-score scenario, the condition asks whether the average score is above 70. A true result allows the program to continue to the repeated congratulations step. A false result means that step is skipped, and the program can continue toward the summary.

Average scoreabove 70?CongratulationsSkipped blockSummarytruefalseafter blockcontinue
How does the average-score condition determine which statements run and which statements are skipped?

Returning to Repeated Work

Repeated execution runs a block of statements multiple times, usually with some variation, until a stopping condition is met. The repeated block is not treated as one single action. The program returns to that block, performs it again for another item or state, and eventually stops when the relevant work is complete.

In the source scenario, once the average is above 70, the program processes the students one by one. For each student whose score is above the average, it prints a congratulations message. The student being considered changes from one repetition to the next, and the repeated processing ends after the students have been handled.

First studentcheck scoreNext studentcheck scoreAnother studentcheck scoreStudents finishedrepeatrepeatstopping conditionmore students
How does control return to the repeated statements, and what changes on each iteration?

Tracking Data Through Execution

When tracing a program, treat the data as part of the program's current state. The source scenario begins with student scores. Sequential execution reads them in order. The program then derives an average for its condition. If that condition permits the next stage, repeated execution examines students in turn and produces congratulations messages for the qualifying students. Finally, sequential execution reaches the summary.

This trace shows why data processing and execution control belong together. The scores provide the data being processed, the average provides the basis for a decision, and the individual student scores provide variation across repetitions. The source pack describes what the program reads, checks, processes, and prints; it does not specify a particular programming language or implementation for storing these items.

Student scoresinput dataAverage scoredecision dataCurrent studentrepeated processingSummaryfinal outputused to determinecontrols next stageafter processing
What data items are being used as the program moves from reading scores to making a decision and producing output?

Mistakes in Program Tracing

  • Common mistake

    Treating every instruction as if it runs exactly once.

    What it looks like

    The program processes the student scores and then handles students one by one.

    Why it fails

    Repeated execution runs a block multiple times, while sequential execution describes the order of statements.

    Do this instead

    Mark which statements are sequential, which block is conditional, and which block repeats.

  • Common mistake

    Assuming a conditional block always runs.

    What it looks like

    The congratulations step is assumed to occur even when the average is not above 70.

    Why it fails

    A condition can cause its block to run or be skipped.

    Do this instead

    Evaluate the condition first, then follow only the branch allowed by its result.

  • Common mistake

    Forgetting that repeated execution has a stopping condition.

    What it looks like

    The student-processing step is traced forever.

    Why it fails

    Repeated execution continues until a stopping condition is met.

    Do this instead

    Identify what marks the end of the repeated processing, such as finishing the students.

  • Common mistake

    Ignoring variation between repetitions.

    What it looks like

    Every repetition is treated as if it processes the same student.

    Why it fails

    Repeated execution usually includes some variation; in the source scenario, the student being processed changes.

    Do this instead

    Record what changes from one repetition to the next.

Practice the Full Trace

MEDIUM

Trace the student-score scenario in order. Identify the sequential steps, state the condition that controls the next stage, describe what repeats, and explain where the final summary occurs.

Hints
  • Begin with the instruction that reads the scores.
  • The decision compares the average score with 70.
  • The repeated stage processes students and congratulates those whose scores are above the average.
  • The summary occurs after the earlier processing has finished.
Worked example

A Complete Execution Trace

Explain the control flow of the program that processes student test scores.

  1. 1
    Sequential start

    The program reads the scores in order.

  2. 2
    Sequential calculation

    The program determines the average from the scores.

  3. 3
    Conditional choice

    The program checks whether the average is above 70. This condition decides whether the congratulations processing runs.

  4. 4
    Repeated processing

    When the condition allows it, the program processes students one at a time and prints a congratulations message for each student whose score is above the average.

  5. 5
    Sequential finish

    After the repeated processing is complete, the program prints a summary.

Result

The program combines sequential execution, conditional execution, and repeated execution in one path: ordered setup, a decision, repeated work when permitted, and an ordered final summary.

The Three Patterns Together

  1. 1
    Sequential execution runs statements one after another in the order they appear.
  2. 2
    Conditional execution checks a condition and runs or skips a block of statements.
  3. 3
    Repeated execution runs a block multiple times, usually with variation, until a stopping condition is met.
  4. 4
    Programs can combine all three patterns to process data, make decisions, and automate tasks.
  5. 5
    Tracing both the execution path and the data being processed helps explain what a program does.

Key Takeaways

  • Sequential execution determines the basic order of instructions.
  • A condition controls whether a block runs or is skipped.
  • Repeated execution returns to a block for multiple items or states and stops when its stopping condition is met.
  • The student-score scenario combines ordered reading, a decision about the average, repeated student processing, and a final summary.
  • Tracing control flow together with the data being processed is a foundation for understanding program behavior.

Related concepts

More concepts starting with V