💻 TechnologyGrade 5Lesson 27
← Back🔒 PDF with full access

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).

Learning Objectives

  • 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.

  1. Reproduce — run the program until you see the bug again.
  2. Inspect — read the code and check the values of your variables.
  3. Hypothesize — make a guess about which line is wrong and why.
  4. 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.
Variables and ConstantsVariable vs ConstantVariable: value can changescore7score = score + 1score8Constant: value never changesMAX_LIVES= 3lockedforeverBoth have a name. Only one can change.
Debugging LoopDebugging: 4-Step Loop1ReproduceRun it again2InspectRead the code3HypothesizeGuess the line4FixChange one thingtestagainCalm, slow, one change at a time.
💡

Tip: when a bug surprises you, print the value of your variable right before the broken line. Nine times out of ten the value is not what you thought it was.

Vocabulary
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.
Free · 7 days · No credit card

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,680 lessons, Grades 1–6, every subject
  • ✓ Profiles + saved progress for every kid
  • ✓ Printable lessons with answer key
Start your 7-day free trial →
← Lesson 26: Conditions, Loops & EventsLesson 28: Make a Mini-Game →