Variables & Debugging
Lesson preview: the Mini Lesson and Vocabulary are open to everyone. Guided practice, exercises, the parent checklist and the printable PDF worksheet come with full access (free 7-day trial or membership).
What you will learn
- 1Define a variable as a labeled box that stores a value that can change
- 2Tell variables apart from constants
- 3Read and update a variable inside a simple program
- 4Find common bugs and apply a clear fix
Mini Lesson
A variable is a labeled box inside a program. It holds a value — like a score, a name, or how many lives are left — and that value can change as the program runs. A constant is also a labeled box, but its value never changes. Knowing the difference helps you write clearer code and find bugs faster.
Variable vs Constant
- Variable — value can change. Examples: score, lives left, time on the clock.
- Constant — value stays fixed. Examples: pi, the gravity number, the screen width.
- Both have a clear name (a label) so you can read and update them in your code.
How a Variable Updates
When the player scores a point you tell the program: score = score + 1. The box called "score" now holds one more than before. Read the value with the variable name. Change the value by assigning a new one.
What is Debugging?
A bug is a mistake in the code. Debugging is the calm process of finding it and fixing it. Good programmers do not panic when something breaks — they slow down and check the code one line at a time.
- Reproduce — run the program until you see the bug again.
- Inspect — read the code and check the values of your variables.
- Hypothesize — make a guess about which line is wrong and why.
- Fix — change one thing, run again, and see if the bug is gone.
Common Bugs to Watch For
- Score never updates — you forgot to write score = score + 1.
- Loop runs forever — there is no stop condition.
- Wrong message shows — you used = (assign) instead of == (compare).
- Variable misspelled — "scor" is not the same box as "score".
- Off-by-one count — you started counting at 1 when you meant 0.
- Sprite never moves — an event is not connected to a motion block.
Words to know
variable
A labeled box in a program that holds a value you can change.
constant
A labeled box whose value is fixed and never changes.
value
The piece of data stored inside a variable or constant.
bug
A mistake in the code that makes the program behave the wrong way.
debug
The process of finding a bug and fixing it.
Tip: when a bug surprises you, show the value of your variable on the screen right before the broken line. Very often the value is not what you thought it was.
Unlock the full HSEA system for 7 days.
Add your first kid profile and your 7 days start: every lesson in every grade, saved progress for each child. Nothing to set up, no card required, nothing to cancel.
- ✓ All 1,880 lessons, Grades 1–6, every subject
- ✓ Profiles + saved progress for every kid
- ✓ Printable lessons with answer key