Computer Science
Grade 3
20 min
Nested Loops
Nested Loops
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define a nested loop as a 'loop inside a loop'.
Identify the 'outer loop' and the 'inner loop' in a block of code.
Explain that the inner loop completes all its repetitions for each single repetition of the outer loop.
Trace the output of a simple nested loop that creates a grid or pattern.
Create a simple nested loop to draw a pattern of a specific size (e.g., a 3x4 rectangle).
Predict how changing the repeat counts in a nested loop will change the final pattern.
Have you ever wondered how a computer can draw a perfect grid of squares, like on a checkerboard, so quickly? ♟️ Let's learn the secret!
Today, we are going to learn about a special computer science tool called a 'nested loop'. It's like having...
2
Key Concepts & Vocabulary
TermDefinitionExample
LoopA command that tells the computer to repeat a set of instructions over and over again.A 'Repeat 4 times' block that makes a character move forward four steps.
Nested LoopA loop that is placed completely inside another loop.Putting a 'Repeat 3 times' block inside a 'Repeat 4 times' block.
Outer LoopThe first, or 'outside,' loop. It controls the main repetitions, like counting the rows in a grid.In a garden, the outer loop would be 'FOR each ROW'.
Inner LoopThe second, or 'inside,' loop. It runs completely every time the outer loop runs just once. It controls actions within each row, like planting seeds in that row.In a garden, the inner loop would be 'FOR each SEED in the row'.
IterationOne sing...
3
Core Syntax & Patterns
The Nested Loop Structure
REPEAT (Outer Count) times:
REPEAT (Inner Count) times:
DO ACTION
This is the basic pattern. The 'DO ACTION' part happens inside the inner loop. The inner loop is indented to show it's inside the outer loop.
The Inner Loop Rule
The inner loop must finish ALL its repetitions before the outer loop can start its next repetition.
Think of it like this: you must plant all the seeds in the first row (inner loop) before you can move to the second row (outer loop).
The Total Actions Formula
Total Actions = (Outer Loop Count) x (Inner Loop Count)
To find out how many times the action will happen in total, you multiply the number of times the outer loop repeats by the number of times the inner loop repeats.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
The outer loop represents T-shirt styles (Polo, V-Neck). The inner loop represents colors (Blue, Green, Red). If the code runs completely, which of these is NOT a possible combination that would be created?
A.Blue Polo
B.Yellow V-Neck
C.Green V-Neck
D.Red Polo
Challenging
A robot follows this code: REPEAT 4 TIMES { Move Forward 1 step; REPEAT 2 TIMES { Turn Right 90 degrees } }. If the robot starts facing North, which way is it facing at the end?
A.North
B.East
C.South
D.West
Challenging
A special loop draws a triangle of stars. On the first pass of the outer loop, it draws 1 star. On the second pass, it draws 2 stars. On the third pass, it draws 3 stars. What is happening?
A.The outer loop's repeat count is changing each time
B.The inner loop's repeat count is increasing by one each time
C.There is no inner loop
D.The computer is making a random pattern
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free