Unable to open this exercise
A Nested Loop is a control structure where one loop resides completely inside the body of another loop.
1. Mechanics of Nested Loops
In a nested loop:
- The outer loop begins its first iteration.
- The inner loop executes and completes all of its iterations.
- Only after the inner loop finishes completely does the outer loop advance to its second iteration.
- This cycle repeats until the outer loop reaches its termination bound.
[Outer Iteration 1]
โโโ [Inner 1]
โโโ [Inner 2]
โโโ [Inner 3]
[Outer Iteration 2]
โโโ [Inner 1]
โโโ [Inner 2]
โโโ [Inner 3]
Total Inner Body Executions:
2. Rules for Nested Loops
- Independent Identifiers: The outer loop and inner loop must use different counter variables (e.g.
RowandCol, oriandj). Re-using the same counter corrupts loop progression. - Proper Nesting Order: The inner loop must be completely closed (
NEXT Inner) before the outer loop closes (NEXT Outer). Crossing loop boundaries is a syntax violation.
AI Socratic Tutor
Need guidance? Ask a question or use the hint below.
AI Socratic Tutor is an All-Access Benefit
Unlock step-by-step Socratic hints, algorithm trace table walkthroughs, and error diagnostics for every Cambridge exercise.
You're chatting with an AI tutor, not a person. AI can make mistakes, so double-check its answers.
Independent Practice: A multiplication grid
Read a row count and column count, both from 1 to 4. Output each row-by-column product in row order, one number per line.
Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.
Interactive exercises for Module 2 and beyond require an active plan.
Run code to inspect step-by-step variable trace states.