🧮 Counter
4th Grade 💻 Coding
A counter is a variable that counts: it usually starts at 0 and goes up by one every time something happens — a trip through a loop, a click or a point scored.
In a Python for loop, the variable i is a counter: it tells you which repeat you’re on. But you can also make a counter yourself: clicks = 0 at the start, and clicks = clicks + 1 on every click. Python has a shortcut: clicks += 1 does exactly the same thing.
Counters are everywhere: the number of tries in a guessing game, the number of views on a video, the number of coins collected. A counter can also go down — like a timer counting backward. The important thing is to reset it in the right place: before the loop, not inside it.
For example
- In a guessing game, the counter
triesgoes up with every guess, and at the end the game shows “You got it in 5 tries!” - A rocket launch countdown: the counter starts at 10 and goes down by one until it reaches 0.
- In Scratch you make a variable called “score,” set it to 0 when the green flag is clicked, and change it by 1 every time the sprite touches a star.
📌 Remember: Counter = a variable that counts. Reset before, add inside.
Quick quiz
What is a counter?
What does clicks += 1 do?
Where do you reset a loop counter?