Computer Science
Grade 5
20 min
Forever Loops
Forever Loops
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define a forever loop and explain its purpose.
Identify the block code for a forever loop.
Create a simple program that uses a forever loop for a continuous action.
Differentiate between a forever loop and a counted loop (like a 'repeat 10' loop).
Use a conditional 'if' statement inside a forever loop to check for an event.
Explain why a forever loop can sometimes cause a program to get stuck.
Have you ever played a video game where your character is always ready for you to press a button, no matter how long you wait? 🤔 How does the computer keep listening?
Today, we're learning about a special kind of loop called a 'forever loop'. This powerful tool tells the computer to repeat a set of instructions over and over, wit...
2
Key Concepts & Vocabulary
TermDefinitionExample
LoopA block of code that repeats a set of instructions. We've used counted loops like 'repeat 10 times' before.A loop could tell a character to 'take 10 steps'.
Forever LoopA special loop that repeats its instructions forever, or until the whole program is stopped.A forever loop could tell a star to 'twinkle' continuously throughout a game.
Infinite LoopAnother name for a forever loop. 'Infinite' means without end.A program that counts up from 1 and never stops is in an infinite loop.
ConditionA question the computer checks to see if it is true or false. We often use conditions inside loops.'if the mouse button is pressed' is a condition.
Program FlowThe order in which the computer runs the lines of code in a pro...
3
Core Syntax & Patterns
The Forever Block
forever {
// code to be repeated goes here
}
This is the basic structure. Any code you put inside the curly braces { } will be repeated over and over again as long as the program is running.
The 'Listen Forever' Pattern
forever {
if (condition is true) {
// do something
}
}
This is a very common and powerful pattern. The forever loop runs continuously, and the 'if' statement inside constantly checks for a specific condition, like a mouse click or a character touching a wall.
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 loop is defined as `while x > y:`. If the code inside the loop always increases `x` by 1 and decreases `y` by 1, what will happen?
A.The loop will run forever, assuming `x` started greater than `y`.
B.The loop will stop when `x` and `y` are equal.
C.The loop will run until `y` becomes a negative number.
D.The program will have an error because two variables are in the condition.
Challenging
Which code block correctly uses a forever loop to count mouse clicks, stopping only when a 'stop_button' is also clicked?
A.clicks = 0; while clicks < 10: if mouse_is_clicked: clicks = clicks + 1
B.clicks = 0; while True: if stop_button_is_clicked: clicks = 0
C.clicks = 0; while stop_button_is_clicked: clicks = clicks + 1
D.clicks = 0; while True: if mouse_is_clicked: clicks = clicks + 1; if stop_button_is_clicked: break
Challenging
The operating system on your computer has background processes that check for new emails or messages. These processes must always be running. This is a real-world, advanced example of a useful...
A.accidental loop that freezes the computer.
B.counting loop that runs 1000 times a second.
C.intentional and necessary forever loop.
D.loop that can only be written in binary code.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free