How Loops Change Execution Flow
Code file order (top to bottom) is not the same as execution order (the actual sequence the program runs).
The Path a Program Takes
A program file has an order: its instructions are written from top to bottom. The running program has a different order: the actual sequence of instructions it executes. These two orders can differ. A loop can send execution back to an earlier part of the program, so a later instruction may run only after several earlier instructions have been repeated.
What do you think happens?
A program reaches a loop whose body is followed by a condition check. After the body runs, what should you look for next when tracing execution?
Reveal answer
Answer: The loop condition and the possible return to the loop body
Following execution flow means tracing the true path the program takes. A loop can cause the body to run again before execution continues to instructions after the loop.
A Loop Trace in Action
Tracing repeated execution
Imagine a short program that prepares a total, processes three items through a loop, and then reports the total. Trace the order in which its actions execute.
1. Prepare the total: Execution performs the setup action before reaching the loop.
2. Check the loop: Execution reaches the loop and evaluates whether another item should be processed.
3. Process the first item: Because another item is available, the loop body runs for the first item.
4. Check the loop again: Execution does not move directly to the reporting action. It returns to the loop check.
5. Process the second item: The loop body runs again for the second item.
6. Check the loop again: Execution returns to the loop check a second time.
7. Process the third item: The loop body runs for the third item.
8. Check the loop again: The loop is checked after the third pass.
9. Report the total: When the loop no longer has another item to process, execution continues to the action after the loop.
The reporting action appears once in the file, but it runs only after the loop body has run three times and the loop check has been revisited.
Reading by Execution Flow
Linear reading asks, “What is written on the next line?” Execution-flow reading asks, “What can run next?” That change in question is important. When you encounter a loop, identify the loop check, follow the body, and then trace where execution goes after the body. If the loop continues, the same path is followed again. If it stops, execution moves to the instructions after the loop. The goal is not to ignore the file order; it is to use file order together with the control path that determines what actually runs.
When tracing a loop, write down each visit to the loop check and each pass through the loop body. Do not record only the first time you see those lines in the file. The trace should represent the actual execution sequence, including repeated actions and the point where execution finally moves beyond the loop.
Mistakes in Loop Tracing
Assuming the program always runs from the first line to the last line exactly once
A loop changes execution flow by sending execution back to the loop check and possibly through the body again.
Fix:
Record every loop check and every loop-body pass before moving beyond the loop.Counting a line only once because it appears once in the file
File position does not tell you how many times an instruction executes.
Fix:
Count visits in the execution trace, not appearances in the file.Ignoring the point where the loop ends
The actual path includes the transition from repeated loop execution to the instructions after the loop.
Fix:
After each body pass, trace the loop check and identify when execution proceeds beyond the loop.
Your Execution Trace
Create an execution trace for a program with one setup action, a loop body that processes two items, and one final action after the loop. List the actions in the order they run. Include each visit to the loop check, both loop-body passes, and the final action.
Hints
- Begin with the setup action.
- Place a loop check before the first body pass.
- Return to the loop check after the first body pass.
- Move to the final action only after the second body pass and the final loop check.
- What statement or action runs first?
- Where can execution return to an earlier point?
- Which actions repeat?
- Which actions are reached only after the loop ends?
The Execution-Flow Habit
- Code file order and execution order are not always the same.
- A loop can send execution back to an earlier part of the program, causing actions to repeat.
- An execution trace records the actual path, including repeated loop checks and loop-body passes.
- Following execution flow is often more effective than reading a program only from top to bottom.
- Understanding execution flow helps with debugging and predicting program behavior.
Key Takeaways
- The order in which instructions are written is not always the order in which they run.
- Loops change execution flow by allowing the program to revisit an earlier point.
- To trace a loop, follow the loop check, record each body pass, and continue only when the loop ends.
- Execution-flow reading is a practical way to understand, debug, and predict program behavior.