By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Grade 5 Computer Science Study Guide: Loops in Python (for and while)
for
while
"If I want my game character to jump 10 times in a row, or my robot to keep moving until it hits a wall, why do I have to type the same command over and over? Isn’t there a way to tell the computer ‘do this a bunch of times’ without copying and pasting the same code like a broken record?"
Imagine you’re teaching a robot dog named Barkley to fetch a tennis ball. You could tell Barkley: "Take one step. Pick up the ball. Take one step back. Drop the ball. Repeat that 5 times." But typing those four lines five times would be exhausting—and what if you wanted Barkley to fetch 100 times? That’s where loops come in. A loop is like a shortcut command that says, "Do this set of actions over and over, either a certain number of times (for loop) or until something changes (while loop)."
for loop: Use this when you know exactly how many times you want something to happen. Example: "Barkley, fetch the ball 5 times." python for i in range(5): print("Barkley fetches the ball!") The range(5) is like counting on your fingers: 0, 1, 2, 3, 4 (that’s 5 times).
python for i in range(5): print("Barkley fetches the ball!")
range(5)
while loop: Use this when you don’t know how many times you’ll need to repeat, but you know the condition that will stop it. Example: "Barkley, keep fetching the ball until the sun goes down." python sun_is_up = True while sun_is_up: print("Barkley fetches the ball!") # Later, someone sets sun_is_up = False
python sun_is_up = True while sun_is_up: print("Barkley fetches the ball!") # Later, someone sets sun_is_up = False
Key Vocabulary: - Loop: A command that repeats a block of code. Example: A microwave’s "30-second" button—it keeps heating until the time runs out (like a while loop checking the timer). - Iteration: One complete run through a loop. Example: If you do 10 jumping jacks, each jack is one iteration. - Condition: A true/false statement that controls a while loop. Example: "Is the cookie jar empty?" (If yes, stop reaching for cookies.) - Range: A sequence of numbers used in a for loop. Example: range(3) gives you 0, 1, 2 (like counting laps in gym class).
range(3)
How this appears in class: - Exit Ticket: "Write a for loop that prints ‘Hello!’ 4 times. Then, write a while loop that prints ‘Goodbye!’ until a variable is_raining becomes True." - Proficient response: ```python # for loop for i in range(4): print("Hello!")
is_raining
True
# while loop is_raining = False while not is_raining: print("Goodbye!") is_raining = True # This stops the loop ``` *Teacher looks for*: Correct loop structure, proper indentation, and a condition that *eventually* stops the `while` loop.
Developing response: ```python # Missing indentation for i in range(4): print("Hello!")
while True: print("Goodbye!") `` *Loses credit for*: Syntax errors (indentation), or awhile` loop that never ends.
`` *Loses credit for*: Syntax errors (indentation), or a
State Test Framing (Grade 5): - Multiple Choice: "Which loop is best for printing ‘Happy Birthday!’ 10 times?" - Distractors: - A while loop with no condition (infinite loop). - A for loop with range(11) (off-by-one error). - A for loop with range(10, 0) (backwards counting). - Correct answer: for i in range(10): print("Happy Birthday!")
range(11)
range(10, 0)
for i in range(10): print("Happy Birthday!")
python while True: print("Loading...")
time_left = 5
time_left = time_left - 1
Mistake 1: The Infinite while Loop - Prompt: "Write a while loop that prints ‘Countdown: 3’ then ‘Countdown: 2’ then ‘Countdown: 1’." - Common Wrong Response: python count = 3 while count > 0: print("Countdown:", count) Loses credit: The loop never stops because count never changes. The computer gets stuck printing "Countdown: 3" forever. - Correct Approach: python count = 3 while count > 0: print("Countdown:", count) count = count - 1 # This updates the condition!
python count = 3 while count > 0: print("Countdown:", count)
count
"Countdown: 3"
python count = 3 while count > 0: print("Countdown:", count) count = count - 1 # This updates the condition!
Mistake 2: Off-by-One in for Loops - Prompt: "Write a for loop that prints numbers 1 through 5." - Common Wrong Response: python for i in range(5): print(i) Loses credit: This prints 0, 1, 2, 3, 4. range(5) starts at 0 by default. - Correct Approach: python for i in range(1, 6): # Start at 1, stop before 6 print(i)
python for i in range(5): print(i)
0, 1, 2, 3, 4
python for i in range(1, 6): # Start at 1, stop before 6 print(i)
Mistake 3: Indentation Errors - Prompt: "Fix this code so it prints ‘Meow’ 3 times." python for i in range(3): print("Meow") - Common Wrong Response: "It’s fine!" (No, it’s not—Python needs indentation to know what’s inside the loop.) - Correct Approach: python for i in range(3): print("Meow") # Indented!
python for i in range(3): print("Meow")
python for i in range(3): print("Meow") # Indented!
Within Computer Science: Loops-Functions Why it matters: Loops repeat code blocks; functions let you name those blocks (e.g., def fetch_ball():) and reuse them anywhere. Without loops, functions would just run once!
def fetch_ball():
Across Subjects: Loops-Music (Rhythm) Why it matters: A for loop is like a metronome—it repeats a beat a set number of times (e.g., 4 counts in a measure). A while loop is like a drum solo—it keeps going until the drummer decides to stop.
Outside School: Loops-Video Game Speedruns Why it matters: Speedrunners use loops to glitch through walls by repeating a jump or crouch at the exact right moment. A while loop in their controller input might say, "Keep pressing ‘jump’ until the character clips through the floor."
"What happens if you put a for loop inside a while loop? For example, this code:
level = 1 while level < 3: for i in range(2): print("Level", level, "Jump", i) level = level + 1
How many times does ‘Jump’ print? Why does the output look like a staircase?"
Pointer Toward the Answer: The while loop runs twice (for level = 1 and level = 2). Each time, the for loop runs twice (for i = 0 and i = 1). So "Jump" prints 4 times total, but the output groups them by level:
level = 1
level = 2
i = 0
i = 1
Level 1 Jump 0 Level 1 Jump 1 Level 2 Jump 0 Level 2 Jump 1
It’s like a nested staircase—the outer loop (while) is the flight of stairs, and the inner loop (for) is each step. Try changing range(2) to range(3) and see what happens!
range(2)
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.