Computer Science
Grade 5
20 min
Repeat Loops
Repeat Loops
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define and identify a nested repeat loop.
Use a variable to control the number of repetitions in a loop.
Trace the value of a counter variable through each iteration of a repeat loop.
Construct a nested repeat loop to create simple 2D patterns like grids or triangles.
Explain the difference between a fixed repeat loop and a variable-controlled repeat loop.
Identify and correct a common 'off-by-one' error in a repeat loop.
Have you ever wanted to make a character in a game jump higher and higher each time? How can we tell a computer to do something a changing number of times? 🤔
In this lesson, we'll explore advanced repeat loops! You will learn how to use variables to make your loops more powerful and flexible. We'll even learn how to...
2
Key Concepts & Vocabulary
TermDefinitionExample
Repeat LoopA block of code that tells the computer to run the same set of instructions over and over again a specific number of times.`repeat 4 times { move forward; turn right; }` This code draws a square by repeating the instructions 4 times.
IterationA single time that a loop runs its block of code. A loop that repeats 5 times has 5 iterations.In a loop that draws a square, the first iteration draws the first side, the second iteration draws the second side, and so on.
Loop Variable (or Counter)A variable that is used to control how many times a loop repeats. Its value can change.`set score to 5; repeat (score) times { collect coin; }` The loop will run 5 times because the 'score' variable is 5.
Nested LoopA loop that is placed inside another loop. The i...
3
Core Syntax & Patterns
Variable-Controlled Loop Pattern
`set [variable] to [value]; repeat ([variable]) times { ...code... }`
Use this when you don't know the exact number of repeats when you write the code, or when the number of repeats needs to change based on user input or other game events.
Nested Loop Pattern
`repeat [outer_count] times { // Outer loop
repeat [inner_count] times { // Inner loop
...inner code...
}
...outer code...
}`
Use this to work with 2D structures like grids, tables, or patterns. The outer loop often controls rows, and the inner loop controls columns.
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
A variable `size` starts at 2. You want to create the number sequence 2, 4, 8, 16 by updating the `size` variable. Which loop correctly does this?
A.repeat 4 times { size = size * 2; }
B.repeat 4 times { size = size + 2; }
C.repeat 3 times { size = size * 2; }
D.repeat 3 times { size = size + size; }
Challenging
The binary number `101` is equal to the decimal number 5. What is the final value of `count`? `binary_value = "101"; num_repeats = convert_to_decimal(binary_value); count = 0; repeat num_repeats times { count = count + 1; }`
A.5
B.10
C.3
D.1
Challenging
A programmer wants to draw a 4x4 grid of dots. Their code is below. What is the logical error? `repeat 4 times { repeat 4 times { draw dot; } move right; }`
A.The `move right` command should be outside both loops.
B.The loops should repeat 3 times, not 4.
C.The `move right` command should be inside the inner loop.
D.The code draws four horizontal lines of dots instead of a grid.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free