Functions and Code Reuse
To understand a program's behavior, trace its control flow: identify the next statement, evaluate any condition, follow any repeated block, and then continue to the next statement after that block.
Following the Execution Path
Every program is a series of instructions that a computer executes. The important point is that instructions do not all behave in the same way. Some run one after another in a fixed order, some run only when a condition is true, and some repeat several times. These three execution patterns determine what a program does and when it does it.
To understand a program's behavior, trace its control flow: identify the next statement, evaluate any condition, follow any repeated block, and then continue to the next statement after that block.
What do you think happens?
When tracing a program from its beginning, what should you identify first?
Reveal answer
The first statement that executes
Sequential execution processes statements one after another in the order they appear. Starting with the first statement gives you the path for evaluating later conditions and repetitions.
Three Ways Instructions Run
Sequential execution is the straightforward pattern: statements run one after another in the order they appear in the script. Conditional execution introduces a decision. The program checks a condition and either runs a block of statements or skips that block. Repeated execution returns to a block and runs it multiple times, usually with some variation, until a stopping condition is met.
| Execution pattern | What control does | Question to ask while tracing |
|---|---|---|
| Sequential | Runs statements one after another in script order | What statement comes next? |
| Conditional | Checks a condition and runs or skips a block | Is the condition true? |
| Repeated | Runs a block multiple times, usually with variation, until a stopping condition is met | Does another repetition remain? |
The three execution patterns describe how control moves through a program.
A Combined Program Trace
The source scenario processes a list of student test scores. First, the program reads the scores in order. That is sequential execution. Next, it checks whether the average score is above 70. That is conditional execution. If the condition is satisfied, the program prints a congratulations message for each student whose score is above the average. Processing students one at a time is repeated execution because the same kind of action occurs multiple times with a different student being considered. Finally, the program prints a summary, returning to sequential execution.
Tracing the Student Score Scenario
Identify the execution pattern at each stage of the student score program.
- 1Start with the scores
The program reads the student scores in order. This is sequential execution because the actions follow a fixed order.
- 2Evaluate the average
The program checks whether the average score is above 70. This is the conditional decision point.
- 3Follow the condition
If the average is above 70, the congratulations block runs. If it is not, that block is skipped.
- 4Repeat the student check
When the congratulations block runs, the program processes each student whose score is above the average. The block is used multiple times with variation in the student being considered.
- 5Finish with the summary
After the conditional and repeated work, the program prints a summary. This is another sequential step.
The complete flow is sequential processing, a conditional decision, possible repeated processing, and a final sequential summary.
Conditions Select the Path
A condition does not automatically run every statement that follows it. Instead, it determines whether a particular block runs or is skipped. In the student score scenario, the condition is whether the average score is above 70. A true result allows the congratulations block to run. A false result skips that block and allows execution to continue to the summary.
Repetition Changes the Focus
Repeated execution runs a block multiple times rather than only once. The actions in the block remain the same kind of actions, while the item being processed can vary from one repetition to the next. In the source scenario, the program prints a congratulations message for each student whose score is above the average. The repeated block ends when the students that need to be processed have been handled.
A repetition is not a separate unrelated sequence each time. It is the same block being run again while the item or situation being processed can change.
Mistakes in Flow Tracing
Common mistake Treating every statement as if it runs exactly once
What it looks likeAssuming the student-processing step happens only once
Why it failsRepeated execution runs a block multiple times, usually with some variation.
Do this insteadMark the repeated block and trace each relevant pass through it before moving to the summary.
Common mistake Forgetting the false path of a condition
What it looks likeTracing only what happens when the average is above 70
Why it failsA condition can cause a block to run or to be skipped.
Do this insteadRecord what happens when the condition is false and show where execution continues afterward.
Common mistake Losing the original statement order
What it looks likeStarting with the summary before reading the scores
Why it failsSequential execution processes statements in the order they appear in the script.
Do this insteadBegin at the first statement and move forward unless a condition or repetition changes the path.
Common mistake Treating the three patterns as isolated features
What it looks likeDescribing the repeated student processing without connecting it to the earlier condition and later summary
Why it failsReal programs combine sequential, conditional, and repeated execution.
Do this insteadTrace the entire path from the first sequential action through the decision and repetitions to the final sequential action.
A Reliable Tracing Method
- 1Start with the first statement and write down the order of the initial actions.
- 2Mark each condition and write the block that runs when it is true.
- 3Write down the block that is skipped when the condition is false.
- 4For every repeated block, identify what changes from one pass to the next.
- 5Continue tracing until the stopping condition is met.
- 6Follow the next sequential statement after the conditional or repeated block.
- 7Check that the trace ends with the program's final stated action, such as printing a summary.
This method turns a large program into a sequence of smaller questions. What runs next? Is there a condition to evaluate? Does a block repeat? What changes during the repetition? Where does control go when the block is complete? These questions help reveal how the three execution patterns work together.
Trace It Yourself
Describe the execution pattern in this scenario: a program reads student scores in order, checks whether the average is above 70, processes each student whose score is above the average, and then prints a summary. Identify the sequential steps, the condition, the repeated block, and the point where execution finishes.
Hints
- Start by listing the actions that occur in a fixed order.
- State exactly what condition controls the decision.
- Identify which action may happen once for each qualifying student.
- Explain what happens if the condition is false.
- End by locating the summary statement.
Model Trace
Classify the stages of the student score scenario as sequential, conditional, or repeated.
- 1Read the scores
This occurs in order before the later decision, so it is sequential execution.
- 2Check the average
The program checks whether the average is above 70, so this is conditional execution.
- 3Process qualifying students
The same kind of action is performed for each student whose score is above the average, so this is repeated execution.
- 4Print the summary
The program performs this final action after the earlier stages, so it is sequential execution again.
The trace combines sequential execution, conditional execution, repeated execution, and sequential execution again.
Key Takeaways
- 1Sequential execution runs statements one after another in the order they appear.
- 2Conditional execution checks a condition and runs or skips a block based on the result.
- 3Repeated execution runs a block multiple times, usually with variation, until a stopping condition is met.
- 4Real programs combine all three patterns to make decisions, process data, and automate tasks.
- 5A dependable trace follows statement order, records both conditional paths, tracks repetitions, and continues to the next statement after each block.
At a glance
- Sequential execution processes statements in the order they appear.
- Conditional execution selects whether a block runs or is skipped.
- Repeated execution revisits a block multiple times while the item or situation being processed can vary.
- Combined programs use all three patterns to process data, make decisions, and automate tasks.
- Tracing becomes manageable when you record the order, condition outcomes, repetitions, and stopping point.