Boolean Logic and Conditions
A condition can be understood through its Boolean outcome: when the condition is true, the associated block can run; when it is not true, that block is skipped.
Three Ways Instructions Flow
Every program is a series of instructions that a computer executes. Those instructions can follow three important execution patterns: they can run one after another in a fixed order, run only when a condition is satisfied, or repeat multiple times. Understanding how these patterns work together helps you control what a program does and when it does it.
The central question is not only which instructions a program contains, but also which instruction runs next, whether a block is skipped, and how many times a repeated block runs.
Following the Program in Order
Sequential execution means that statements run one after another in the order they appear in the script. The first statement is processed, then the next statement, and so on. Nothing in this pattern changes the order or causes a block to repeat: the program moves forward through its instructions.
In the source scenario, the program first reads student scores in order. After that step, it checks the average score. Finally, it prints a summary. The order of these actions illustrates sequential execution: each action follows the previous one.
Making a Boolean Decision
Conditional execution checks a condition and decides whether to run or skip a block of statements. A condition can be understood through its Boolean outcome: when the condition is true, the associated block can run; when it is not true, that block is skipped. The decision changes the path the program follows.
Checking the class average
Trace what happens when a program processes student scores and checks whether the average score is above 70.
- 1Sequential step
The program reads the student scores in order.
- 2Condition check
The program checks whether the average score is above 70. This check produces the decision used by the conditional execution.
- 3Conditional path
If the average is above 70, the program proceeds to the congratulations block. If it is not, that block is skipped.
- 4Next instruction
The program continues to the summary after the conditional part.
The condition controls whether the congratulations block runs, while the surrounding steps retain their sequential order.
Repeating with Variation
Repeated execution runs a block of statements multiple times, usually with some variation, until a stopping condition is met. Each repetition is another pass through the block. The values or item being processed can vary from one repetition to the next, while the stopping condition determines when the repeated execution ends.
In the student-score scenario, the program prints a congratulations message for each student whose score is above the average. The message-producing action is repeated for multiple students, and the student being considered can vary on each repetition.
A repeated block has two important features: it runs more than once, and it eventually stops when its stopping condition is met.
Tracing the Combined Flow
Real programs combine sequential, conditional, and repeated execution. The student-score scenario shows how these patterns fit together: the program first reads the scores in order, then checks a condition about the average, then repeatedly processes qualifying students, and finally prints a summary.
| Stage | Execution pattern | What happens |
|---|---|---|
| Read the scores | Sequential | Scores are read in order. |
| Check the average | Conditional | The program decides whether the average is above 70. |
| Process students | Repeated | The program considers students multiple times, with the student being processed varying between repetitions. |
| Print the summary | Sequential | The program performs the final summary step after the earlier processing. |
The three execution patterns can appear in one program and can be nested within one another.
When tracing a combined program, identify the current statement, check whether a condition changes the path, identify whether a block repeats, and then continue to the next sequential step after the decision or repetition finishes.
Mistakes in Flow Tracing
Common mistake Treating every statement as if it runs at the same time.
What it looks likeAssuming that the summary is produced before the score-processing steps have finished.
Why it failsSequential execution processes statements one after another in the order they appear.
Do this insteadTrace the program from the first statement to the next statement, preserving the stated order.
Common mistake Assuming that a conditional block always runs.
What it looks likeExpecting the congratulations message to run without checking whether the average score is above 70.
Why it failsConditional execution can run a block or skip it depending on the condition.
Do this insteadWrite down the condition's outcome before following the selected path.
Common mistake Confusing one decision with repeated execution.
What it looks likeThinking that checking whether the average is above 70 automatically processes every student.
Why it failsA condition selects whether a block runs or is skipped, while repeated execution runs a block multiple times until a stopping condition is met.
Do this insteadMark the decision and the repeated block as separate parts of the flow.
Common mistake Ignoring the stopping condition.
What it looks likeTracing the student-processing block repeatedly without identifying when the program should stop.
Why it failsRepeated execution continues until a stopping condition is met.
Do this insteadAt each repetition, identify what changes and check whether the stopping condition has been met.
Practice the Trace
Trace the student-score scenario in four stages. First, identify the sequential actions. Next, identify the condition that selects a path. Then identify the block that repeats and the stopping condition. Finally, state which action occurs after the repeated processing finishes.
Hints
- Start with the order in which the program reads scores, checks the average, processes students, and prints the summary.
- Separate the average check from the repeated processing of individual students.
- The final action is the summary step.
Answering the trace
State the control-flow pattern for the student-score program.
- 1Order
The scores are read in order.
- 2Decision
The program checks whether the average score is above 70.
- 3Repetition
When the relevant path is taken, the program processes each student whose score is above the average.
- 4Completion
After the processing, the program prints a summary.
The trace combines sequential execution, conditional execution, repeated execution, and sequential execution again.
What to Remember
- 1Sequential execution runs statements one after another in the order they appear.
- 2Conditional execution checks a condition and either runs or skips a block of statements.
- 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.
- 5Tracing control flow means following the order, evaluating decisions, tracking repetitions, and identifying what runs next.
At a glance
- Sequential execution follows the order of statements in a script.
- A condition produces a decision about whether a block runs or is skipped.
- Repeated execution processes a block multiple times with variation until a stopping condition is met.
- Programs become useful by combining ordered steps, decisions, and repetitions.
- A control-flow trace should account for every sequential step, branch, repetition, and stopping point.