Choose a country
Grade 6Computer scienceProgramming

Variables in a program

Lesson goal: Explain what a variable is, declare one and give it a proper name, understand the assignment sign, and make a program more efficient by replacing numbers with variables.
← Programming

About this lesson

A quantity whose value can change while a program runs is a variable; one that does not change is a constant. A variable is like a labelled box: the label is the variable’s name and what is inside is its value. Every variable used in a program must be declared first. In ALPLogo you write the keyword for “variable” and then the name: variable x, variable sum1. Naming rules: Latin letters (upper and lower case), digits and the underscore (_) are allowed; the name must start with a letter; no spaces; and it must not match a command name. Correct: x, sum1, star_colour. Wrong: 1x, star colour. A value is given with the assignment sign (=): x = 60. Read it as “assign the value 60 to x” — it is not a mathematical equation! The expression on the right is worked out and the result is stored in the variable on the left, replacing the old value. For example, after a = 5 and then a = a + 2, a holds 7. If the same number appears in many places, it is handy to replace it with a variable: in a program drawing a zigzag, write “forward y, right x” instead of “forward 50, right 60”, with x = 60 and y = 50 at the top — then changing the size means editing just two lines.

Lesson goal: Explain what a variable is, declare one and give it a proper name, understand the assignment sign, and make a program more efficient by replacing numbers with variables.

What the book shows

In grade 6 the computer science textbook covers how a computer is built and works, encoding numbers, electronic presentations, the properties of algorithms and loops, variables in programs, and email. Each topic gives a short explanation, an example and a task done on or off the computer.

Textbook link:

This is not the textbook’s text but a short description of the topic. Check the exact page in your own textbook.

The lesson step by step

  1. 1

    Warm-up5 min

    Labelled boxes.

  2. 2

    New topic10 min

    Variables and constants.

  3. 3

    Practice10 min

    Declaring and naming.

  4. 4

    Game10 min

    Assignment.

  5. 5

    Wrap-up5 min

    Improving a program.

Let’s understand

For the student Simple language

IdeaWhat it isExample
variablea value that can changex, sum1
declarationannouncing it firstvariable x
assignmentright side stored on the leftx = 60
new valuethe old one is replaceda = a + 2
names start with a letter, no spaces

Going deeper

Scientific language: for older students, parents and teachers.

In many programming languages a variable also has a type — whole number, decimal, text — which sets what values it can hold.

Play and check

Is the variable name valid?

How to write it down

  1. Write 5 valid and 3 invalid variable names and say why the invalid ones are wrong.
  2. After a = 3, b = a + 4, a = b × 2, find a and b.
  3. Write the square program with the side length as a variable.

How the classwork looks in the notebook

15 November1Classwork2variable = a value that can change · constant = does not change · declare: variable x · names: start with a letter; Latin letters, digits, _; no spaces · assignment: x = 60 (right → left) · old value replaced: a = a + 2 · replace repeated numbers with variables3
Writing order: 1 Date 2 Heading 3 Line 3: variable = a value that can change · constant = does not change · declare: variable x · names: start with a letter; Latin letters, digits, _; no spaces · assignment: x = 60 (right → left) · old value replaced: a = a + 2 · replace repeated numbers with variables
Sample: the date and heading follow this country’s notebook rules.

Open notebook

Watch out: common mistakes

Reading “x = 5” as an equation. It is assignment: 5 is stored in x.
Using a variable without declaring it. Declare it first.

Where it helps in life

  • Email

At home

Core

  1. Which variables might your favourite game use? Write 4 names with sample values.

Extra ⭐

  1. t = 10, t = t − 3, t = t × 2 — what is t at the end?

How to do it

  1. Do the task with a grown-up.
  2. Explain in words what you noticed.
Answers for parents

a = 3; b = 3 + 4 = 7; a = 7 × 2 = 14, so a = 14, b = 7. t: 10 → 7 → 14. Square: declare s, s = 80, then 4 times “forward s, right 90”.

Step-by-step homework solution — Show the solution (try it yourself first)
  1. Let’s improve a program

    1. A program has the numbers 50 and 60 in 6 places; a bigger shape would mean changing all 6. Solution: declare x and y; x = 60; y = 50; then write “right x, forward y”. Now to enlarge the shape you only write y = 100. The program is 4 lines longer but far more efficient.

How the homework looks in the notebook

15 November1Homework2Variables3
Writing order: 1 Date 2 Heading 3 Line 3: Variables
Sample: the date and heading follow this country’s notebook rules.

Check yourself