Lesson 5: Variables
45 minutes
Overview
In this lesson, students learn how to use variables to label a value. Students begin the lesson with a very basic description of the purpose of a variable within the context of the storage component of the input-output-storage-processing model. Students then complete a level progression that reinforces the model of a variable as a way to label or name a number. Students should leave this lesson knowing that variables are a way to label a value in their programs so that they can be reused or referenced later. In the following lesson, students will be introduced to random numbers, in which they will see a more powerful use for variables, and in later lessons, students will continue to expand their understanding of variables and experience more advanced ways they can be used.
Question of the Day: How can we use variables to store information in our programs?
Standards
AP - Algorithms & Programming
- 2-AP-11 - Create clearly named variables that represent different data types and perform operations on their values.
- 2-AP-13 - Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
- 2-AP-17 - Systematically test and refine programs using a range of test cases.
- 2-AP-19 - Document programs in order to make them easier to follow, test, and debug.
Agenda
Objectives
Students will be able to:
- Identify a variable as a way to label and reference a value in a program
- Use variables in a program to store a piece of information that is used multiple times
Preparation
- Review the level progression in Code Studio
- Check the "Teacher's Lounge" forum for verified teachers to find additional strategies or resources shared by fellow teachers
- If you are teaching virtually, consider checking our Virtual Lesson Modifications
Links
Heads Up! Please make a copy of any documents you plan to share with students.
For the teachers
- Naming Variables - Resource
- Variables - Slides
For the students
- Introduction to Variables - Video (Download)
- Variables - Resource
- Variables - Video
Vocabulary
- Variable - A label for a piece of information used in a program.
Introduced Code
Teaching Guide
Warm Up (5 minutes)
Input-Output-Storage-Processing
Prompt: All computers do four things: input, output, processing, and storage. Where do you see input, output, storage, and processing in Game Lab?
Input, Output, Processing, and Storage: These terms are introduced in the Problem Solving and Computing unit in CS Discoveries, so students may already be familiar with them. However, if they need a refresher, you may decide to show the What Do Computers Do? video which highlights these four functions.
Share: Allow students to share out their answers.
Discussion Goal: Allow students to share out their different ideas, but eventually bring the conversation back to storage to tie into today's lesson. Students’ answers may include:
- input: values passed as parameters, typing into the Game Lab workspace
- output: shapes shown on the Game Lab screen
- storage: remembering the code
- processing: the If/then and matching that turn the code into the pictures on the screen
Remarks
Today we're going to focus on storage. We're going to look at variables, which are a very common way for computers to store information in a program.
Key Vocabulary: variable - a label for a piece of information used in a program
Question of the Day: How can we use variables to store information in our programs?
Activity (35 minutes)
Programming with Variables
Transition: Send students to Code Studio.
Guide to Programming Levels: Additional guidance for programming levels is provided in the CSD Guide to Programming Levels. This document includes strategies and best-practices for facilitating programming levels with students.
Discussion Goal: Students may notice that the term xPosition
is associated with the value 50, and so when we see the term xPosition
farther down in the code, they may think it will use the value of 50 in that spot and draw the circle on the left side of the screen. When describing this relationship, they may not have the correct words to describe what's happening and may wonder what the purple var
keyword means. Encourage students to use other familiar words when describing this relationship, like "container" or "placeholder", before introducing vocabulary
Video: Show students the Introduction to Variables video in the slides.
To encourage active engagement and reflection, use one or more of the strategies discussed in the Guide to Curriculum Videos.
Questions to Consider with Video:
- What are variables used to do?
- How do you create a variable and assign it a value?
- What can go into a variable?
Discussion Goal: Students should understand that variables hold information and can be accessed using their labels. With simple drawings, students may not see the power of variables, so you may want them to think of some different apps that they use and what information needs to be stored for the app to work, or think about a more complex program that they want to use variables for.
Numbers, text, and colors can all go into variables, as well as more complicated data structures that students will see later in the course.
Discussion Goal: During this discussion, encourage students to use the vocabulary they saw in the previous video, referring to xPosition
and whatsTheY
as variables and describing the equal sign as "gets the value of". The concept is the same as the first predict level, with the added emphasis on using correct vocabulary. For example: "I see that the variable xPosition
gets the value of 300, so I know it will be drawn on the right side of the screen"
Level 5: If students use variable names that start with numbers, include spaces, or break other rules, the code may be forced into text mode the next time that they go to that level or refresh the page. To get back into block mode, students will first need to fix the problem with the variable names. Use the red error squares to see where the bugs most likely are, and once they are gone, click the "block mode" button at the top right of the workspace.
Formative Assessment: This level can be used as a formative assessment. A rubric is provided in the level, and written feedback can be given to students. Click here to learn more about giving feedback to students.
Wrap Up (5 minutes)
Reflection
Question of the Day: How can we use variables to store information in our programs?
Prompt: Give students the following prompts:
Use this discussion to assess students' mental models of a variable. You may wish to have students write their responses so you can collect them to review later. You should be looking to see primarily that they understand that variables can label or name a number so that it can be used later in their programs. While there are other properties of a variable students may have learned, this is the most important thing they should understand before moving on to the next lesson.
- What is your own definition of a variable?
- Why are variables useful in programs?
Discuss: Have students silently write their ideas before sharing in pairs and then as a whole group.
This work is available under a Creative Commons License (CC BY-NC-SA 4.0).
If you are interested in licensing Code.org materials for commercial purposes contact us.