Computer Science
Grade 4
20 min
Understanding Loops
Understanding Loops
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Identify and describe a nested loop.
Explain the role of an outer loop and an inner loop.
Trace the value of a counter variable through a nested loop.
Write a simple algorithm using a nested loop to create a grid pattern.
Predict the total number of times an inner loop will run.
Identify the cause of a simple infinite loop.
Have you ever wanted to build a giant castle in a game, one block at a time? 🏰 What if you could write one command to build a whole wall or floor automatically?
Today, we're going beyond simple loops to learn a super-coder skill: nested loops! This is like putting a loop inside another loop, letting us do amazing things like creating patterns, grids, and solving bigger problems with just a few lines of code.
Real-World Applicat...
2
Key Concepts & Vocabulary
TermDefinitionExample
Nested LoopA loop that is placed inside of another loop. The inside loop is called the 'inner loop' and the outside loop is called the 'outer loop'.Imagine you have 3 boxes of crayons (outer loop). For EACH box, you check all 8 crayons inside it (inner loop).
Outer LoopIn a nested loop, this is the loop on the outside. It controls the main cycles.In our crayon example, the loop that goes through the 3 boxes is the outer loop.
Inner LoopIn a nested loop, this is the loop on the inside. It runs completely for every single time the outer loop runs once.The loop that checks the 8 crayons is the inner loop. It runs 3 separate times, once for each box.
Loop CounterA variable that keeps track of how many times a loop has run. We can use its value inside...
3
Core Syntax & Patterns
The Nested Loop Pattern
LOOP for row from 1 to 3:
LOOP for column from 1 to 4:
Do something
Use this pattern to work with grids. The outer loop handles the rows, and the inner loop handles the columns for each row. The 'Do something' part happens for every single spot in the grid.
Total Runs Calculation
Total Inner Loop Runs = (Number of Outer Loop Runs) x (Number of Inner Loop Runs)
To figure out how many total times the innermost code will run, multiply the number of times the outer loop runs by the number of times the inner loop runs. In the example above, it's 3 x 4 = 12 times.
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
Look at this code: `count = 5`. `LOOP WHILE count > 0`. ` ADD 1 to count`. Why will this loop run forever?
A.The value of `count` is increasing, so it will always be greater than 0
B.The loop should be `WHILE count < 0`
C.The starting value of `count` is too high
D.Computers cannot count above 5
Challenging
A character in a game needs to find a key. The key is in the 3rd of 10 boxes. Which search plan is "smarter" (more efficient)?
A.loop that checks all 10 boxes, no matter what
B.loop that checks each box and stops as soon as the key is found
C.loop that checks only the even-numbered boxes (2, 4, 6, 8, 10)
D.loop that checks the boxes in reverse order (10, 9, 8...)
Challenging
A robot starts at step 0 and wants to get to step 10. It can only take steps of size 2 or 3. Which loop could NOT help the robot land exactly on step 10?
A.loop that adds 2 five times
B.loop that adds 2 two times, then adds 3 two times
C.loop that adds 3 three times, then adds 2 one time
D.loop that adds 3 two times, then adds 2 two times
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free