Introduction to Loops
Loops belong to repeated execution: a block of statements runs multiple times, usually with some variation, until a stopping condition is met.
Three Ways Programs Run
Every program is a series of instructions that a computer executes. These instructions do not all run in the same way. Some run one after another in a fixed order. Some run only when a condition is true. Others repeat multiple times. These three patterns are sequential execution, conditional execution, and repeated execution.
Loops belong to repeated execution: a block of statements runs multiple times, usually with some variation, until a stopping condition is met.
Tracing the Score-Processing Program
A useful way to understand program flow is to trace a program one stage at a time. Consider a program that processes a list of student test scores. It first reads the scores in order. It then checks whether the average score is above 70. If that condition is true, it prints a congratulations message for each student whose score is above the average. Finally, it prints a summary.
Following the control flow
Identify which execution pattern is represented at each stage of the score-processing program.
- 1Read the scores
The program reads the scores in the order they appear. This is sequential execution because the statements proceed one after another.
- 2Check the average
The program checks whether the average score is above 70. This is conditional execution because the result determines whether a block of statements runs or is skipped.
- 3Process students
When the average is above 70, the program checks students and prints a congratulations message for each student whose score is above the average. This is repeated execution because the work is performed for multiple students.
- 4Print the summary
After the earlier work is complete, the program prints a summary. This returns to sequential execution because the summary follows the preceding statements.
The program combines sequential execution, conditional execution, and repeated execution rather than using only one pattern.
How Each Pattern Controls Flow
Sequential execution is the basic order of a program. Statements are performed one after another in the order they appear in the script. Unless another execution pattern changes the flow, the next statement follows the current statement.
Conditional execution introduces a decision. The program checks a condition and decides whether to run or skip a block of statements. In the score-processing scenario, the condition is whether the average score is above 70. The result of that check determines whether the congratulations-processing block is used.
Repeated execution introduces a return to an earlier block. The program runs the block multiple times, usually with some variation, until a stopping condition is met. In the score-processing scenario, the student being checked changes as the program processes the list of students.
What Changes on Each Repetition
A loop is not simply a block that runs forever. Repeated execution continues until a stopping condition is met. Something usually varies between repetitions. In the source scenario, the program moves through different students, checks each student's score against the average, and prints a congratulations message only for a student whose score is above that average.
Practice the Trace
Using the student-score scenario, describe the order in which the program performs its major actions. Identify the sequential stages, the condition that controls whether the student-processing block runs, the repeated work inside that block, and the stopping condition for the repetition.
Hints
- Start with the actions that happen in a fixed order.
- Name the question the program checks before deciding whether to process students.
- For repeated execution, identify what changes from one student check to the next.
- End by identifying what happens after all students have been processed.
Common mistake Treating every statement as sequential execution
What it looks likeAssuming that the congratulations message runs for every student without considering the condition.
Why it failsConditional execution can cause a block of statements to run or be skipped.
Do this insteadCheck the condition first, then determine whether the block runs.
Common mistake Describing repetition without identifying what changes
What it looks likeSaying that the program repeats but not identifying which student is being checked.
Why it failsRepeated execution usually includes variation and continues until a stopping condition is met.
Do this insteadTrack the changing item and the condition that ends the repetition.
Common mistake Ignoring the statements after a loop
What it looks likeStopping the trace after the last student is checked.
Why it failsThe source scenario performs sequential execution again by printing a summary after the repeated processing.
Do this insteadContinue tracing after the repeated block and include the final sequential statements.
Flow-Control Summary
- 1Sequential execution performs statements one after another in the order they appear.
- 2Conditional execution checks a condition and 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 the order, decisions, repetitions, and stopping points helps explain what a program does.
At a glance
- Sequential execution follows the order of statements in a script.
- Conditional execution uses a condition to decide whether a block runs or is skipped.
- Repeated execution returns to a block and performs it multiple times with variation until a stopping condition is met.
- Programs commonly combine sequential, conditional, and repeated execution.
- A reliable trace follows the program's order, checks each decision, tracks each repetition, and continues through the final statements.