Lesson 5: Variables
45 minutes
Overview
Question of the Day: How can we use variables to store information in our programs?
In this lesson students learn how to use variables to label a number. 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.
Purpose
This lesson is the first time students will see variables in the course, and they are not expected to fully understand how variables work by its conclusion. 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.
Using variables to manipulate drawings is a surprisingly challenging skill that requires a great deal of forethought and planning. While students will use or modify many programs in this lesson, they are not expected to compose programs that use variables to modify the features of a drawing. In later lessons, students will expand their understanding of variables and more advanced ways they can be used.
Assessment Opportunities
-
Identify a variable as a way to label and reference a value in a program
See the reflection prompt in the Wrap Up.
-
Use variables in a program to store a piece of information that is used multiple times
See Level 7 in Code Studio.
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 - Resource
- Variables - Slides
For the students
- Introduction to Variables - Video (Download)
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: At the beginning of the course, we learned that input, output, storage, and processing were common to all computers. Where do you see input, output, storage, and processing in Game Lab?
Share: Allow students to share out their answers.
Allow students to share out their different ideas, but eventually bring the conversation back to storage to tie into the lesson topic of variables. 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.
Facilitating Predict Levels: Predict levels are a great opportunity for students to think critically about code and engage in class discussions. Consider having students think of their own prediction and discuss with a partner before typing in their response. Once they're run the code, bring the class together for a full-group discussion to discuss how their predictions were similar or different from the resulting program. Use this as an opportunity to address any misconceptions that students may have had about the code initially.
Caution: Predict Entries Can't Be Deleted! Once you enter an answer into the predict box, you are not able to reset it. This can make it difficult to repeat this process with multiple classes. Instead, we recommend entering dummy text like "Class discussion" and running the code. This way, the answer isn't "spoiled" for future classes. However, this means you will need to keep track of predictions separately such as on a whiteboard or in a document.
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?
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.
Level 4: 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.