Concepts / Functions and Code Reuse

Functions and Code Reuse

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

  • Programming

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?

  • The first statement that executes
  • The final summary
  • The repeated block only
  • The stopping condition only
Reveal answer

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 patternWhat control doesQuestion to ask while tracing
SequentialRuns statements one after another in script orderWhat statement comes next?
ConditionalChecks a condition and runs or skips a blockIs the condition true?
RepeatedRuns a block multiple times, usually with variation, until a stopping condition is metDoes another repetition remain?

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.

nextnexttruefalsenext studentfinishedcontinueRead scoressequentialAverage above 70conditionProcess qualifyingstudentrepeatedPrint summarysequentialCalculate averagesequentialSkip congratulationsblockconditional result
What path does control take as statements run in order, a condition selects a branch, and a repeated step processes qualifying students?

Tracing the Student Score Scenario

Identify the execution pattern at each stage of the student score program.

Start with the scores: The program reads the student scores in order. This is sequential execution because the actions follow a fixed order.

Evaluate the average: The program checks whether the average score is above 70. This is the conditional decision point.

Follow the condition: If the average is above 70, the congratulations block runs. If it is not, that block is skipped.

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

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

truefalseafter blockcontinueAverage above 70check conditionCongratulationsmessagerun blockSummarycontinueCongratulations blockskip block
How does the average-score condition determine which statements run and which statements are skipped?

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.

repeatrepeatmore studentsfinishedStudent 1compare scoreSummarystopping pointStudent 2compare scoreNext studentrepeat block
How does control revisit the same kind of step while the student being considered changes from one iteration to the next?

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

  • Treating every statement as if it runs exactly once

    Repeated execution runs a block multiple times, usually with some variation.

    Fix: Mark the repeated block and trace each relevant pass through it before moving to the summary.

  • Forgetting the false path of a condition

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

    Fix: Record what happens when the condition is false and show where execution continues afterward.

  • Losing the original statement order

    Sequential execution processes statements in the order they appear in the script.

    Fix: Begin at the first statement and move forward unless a condition or repetition changes the path.

  • Treating the three patterns as isolated features

    Real programs combine sequential, conditional, and repeated execution.

    Fix: Trace the entire path from the first sequential action through the decision and repetitions to the final sequential action.

A Reliable Tracing Method

  1. Start with the first statement and write down the order of the initial actions.
  2. Mark each condition and write the block that runs when it is true.
  3. Write down the block that is skipped when the condition is false.
  4. For every repeated block, identify what changes from one pass to the next.
  5. Continue tracing until the stopping condition is met.
  6. Follow the next sequential statement after the conditional or repeated block.
  7. Check 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

MEDIUM

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.

Read the scores: This occurs in order before the later decision, so it is sequential execution.

Check the average: The program checks whether the average is above 70, so this is conditional execution.

Process qualifying students: The same kind of action is performed for each student whose score is above the average, so this is repeated execution.

Print 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

  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 based on the result.
  3. Repeated execution runs a block multiple times, usually with variation, until a stopping condition is met.
  4. Real programs combine all three patterns to make decisions, process data, and automate tasks.
  5. A dependable trace follows statement order, records both conditional paths, tracks repetitions, and continues to the next statement after each block.

Key Takeaways

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