Concepts / Variables and Data Storage

Variables and Data Storage

Sequential execution performs statements one after another in the order they appear in the script.

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

nextyesnoqualifying studentnext studentfinishedRead scoresCheck averageabove 70?Process studentsCongratulationsPrint summary
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.

Reading the Scores Sequentially

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

First instruction: The program reads the student scores in order.

Next instruction: After reading the scores, the program determines the average score.

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

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

thentheneventuallyStudent scoresAverage scoreAverage checkSummary
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.

truefalseafter blockcontinueAverage scoreabove 70?CongratulationsSkipped blockSummary
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.

repeatrepeatstopping conditionmore studentsFirst studentcheck scoreNext studentcheck scoreAnother studentcheck scoreStudents finished
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.

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

Mistakes in Program Tracing

  • Treating every instruction as if it runs exactly once.

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

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

  • Assuming a conditional block always runs.

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

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

  • Forgetting that repeated execution has a stopping condition.

    Repeated execution continues until a stopping condition is met.

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

  • Ignoring variation between repetitions.

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

    Fix: 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.

A Complete Execution Trace

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

Sequential start: The program reads the scores in order.

Sequential calculation: The program determines the average from the scores.

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

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.

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

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. Sequential execution runs statements one after another in the order they appear.
  2. Conditional execution checks a condition and runs or skips a block of statements.
  3. Repeated execution runs a block multiple times, usually with variation, until a stopping condition is met.
  4. Programs can combine all three patterns to process data, make decisions, and automate tasks.
  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.