💡 Quick Study Guide: Scratch While Loops
- 📖 A "while loop" (often implemented as
repeat until or forever if in Scratch) is a control structure that allows a block of code to be executed repeatedly as long as a specified condition remains true. - ⚙️ Before each execution of the loop's code, Scratch checks the condition. If the condition is true, the code inside the loop runs. If it's false, the loop stops, and the program continues with the code after the loop.
- 🧱 Key Scratch Blocks:
- 🔄
repeat until <condition>: This block will run the code inside it repeatedly UNTIL the condition becomes true. So, it effectively runs WHILE the condition is FALSE. - ❓
forever if <condition>: This block, often nested inside a forever loop, will execute its code only when the condition is true during each iteration of the forever loop. It acts as a continuous conditional loop.
- ⚠️ It's crucial that something inside the loop changes a variable or state that affects the loop's condition. If the condition never becomes false (for
repeat until) or never becomes true (for forever if in a forever loop's context), the loop will run endlessly, potentially freezing the program (an "infinite loop"). - 🎮 While loops are great for tasks like: waiting for user input (e.g.,
repeat until <key pressed>), game mechanics (e.g., repeat until <score = 10>), animations that continue as long as a condition is met, and counting or accumulating values until a limit is reached.
🧠 Practice Quiz: Scratch While Loops
- What is the primary purpose of a "while loop" in programming, as simulated by blocks like
repeat until in Scratch?
A) To make a sprite move randomly.
B) To execute a block of code only once.
C) To repeat a block of code as long as a condition is true (or until it becomes true).
D) To define a new variable. - Consider the Scratch block
repeat until <score = 10>. When will the code inside this loop stop executing?
A) When the score variable is less than 10.
B) When the score variable is exactly 10.
C) When the score variable is greater than 10.
D) It will never stop, creating an infinite loop. - Which of the following is a common issue if you forget to include a block that changes the loop's condition inside a
repeat until loop?
A) The loop will execute only once.
B) The program will run faster than intended.
C) The loop might become an "infinite loop," running forever.
D) The sprite will change its costume automatically. - A high school student wants to make a sprite say "Hello!" repeatedly until the user presses the space key. Which Scratch block combination would best achieve this?
A) say "Hello!" for 2 seconds
B) forever { say "Hello!" }
C) repeat until <key [space] pressed?> { say "Hello!" }
D) when [space] key pressed { say "Hello!" } - In Scratch, if you have a
forever loop that contains an if <condition> then block, and inside that if block, there's code that runs, how does this relate to a "while loop" concept?
A) It's exactly the same as a repeat until loop.
B) It acts like a "while loop" that continuously checks and executes its code as long as the condition is true.
C) It's used only for changing backgrounds.
D) It's a way to stop the script entirely. - Imagine a game where a character needs to keep moving right (
change x by 10) as long as it has energy > 0. Which Scratch structure best represents this logic for a "while loop"?
A) when green flag clicked { if <energy > 0> then { change x by 10 } }
B) repeat until <energy = 0> { change x by 10 }
C) forever { change x by 10 }
D) repeat 10 times { change x by 10 } - What happens immediately after a
repeat until loop's condition becomes true in Scratch?
A) The code inside the loop executes one more time.
B) The script stops completely.
C) The code immediately following the repeat until block starts executing.
D) The condition is checked again, but the loop never runs.
Click to see Answers
1. C
2. B
3. C
4. C
5. B
6. B
7. C