Variables

This lesson introduces the use of variables to store data or the results of mathematical operations. Students will practice giving variables unique and meaningful names. We will also introduce the basic mathematical operations for adding, subtracting, multiplying, and dividing variables.

OVERVIEW

Activity Overview:

This lesson introduces the use of variables to store data or the results of mathematical operations. Students will practice giving variables unique and meaningful names. We will also introduce the basic mathematical operations for adding, subtracting, multiplying, and dividing variables.

Meta description

  • Subject Area: Computer Science, Mathematics
  • Grade Level : 6-8
  • Computer Science Domains:
    • Algorithms and Programming
  • Computer Science Principles:
    • Fostering an Inclusive Computing Culture, Collaborating Around Computing, Recognizing and Defining Computational Problems, Creating Computational Artifacts, Testing and Refining Computational Artifacts, Communicating About Computing
  • Materials:
  • Considerations:
    • You should know what variables are and how to clearly name them.

Lesson Plan

Overview

This lesson introduces the use of variables to store data or the results of mathematical operations. Students will practice giving variables unique and meaningful names. We will also introduce the basic mathematical operations for adding, subtracting, multiplying, and dividing variables.

ASSESSMENT PRE/POST-TEST

What is a variable? How do you list a variable in a meaningful way? How do you use a mathematical operation with a variable? Are you able to use a mathematical operation on at least two variables in a way that is integral to your program?

Post: What is a variable? How do you list a variable in a meaningful way? How do you use a mathematical operation with a variable?

OBJECTIVES

Understand what variables are and why and when to use them in a program.
Learn how to create a variable, set the variable to an initial value, and change the value of the variable.
Learn how to create meaningful and understandable variable names. Understand that a variable holds one value at a time. Understand that when you update or change the value held by a variable, the new value replaces the previous value. Learn how to use the basic mathematical formulas for adding, subtracting, multiplying, and dividing variables. Apply the above knowledge and skills to create a unique program that uses variables as an integral part of the program.

CATCH/HOOK

Computer programs process information. Some of the information that is input, stored, and used in a computer program has a value that is constant, meaning it does not change throughout the course of the program. An example of a constant in math is ‘pi’ because ‘pi’ has one value that never changes. Other pieces of information have values that vary or change during the running of a program. Programmers create variables to hold the value of information that may change. In a game program, a variable may be created to hold the player’s current score, since that value would change (hopefully!) during the course of the game.

Ask the students to think of some pieces of information in their daily life that are constants and others that are variables.

What pieces of information have values that don’t change during the course of a single day (constants)? What pieces of information have values that do change during the course of a single day (variables) Constants and variables can be numbers and/or text.

ACTIVITY INSTRUCTIONS

Students will play a game of Rock, Paper, Scissors and keep track of their scores on paper. After about a minute ask the students to add up their scores and how many ‘rounds’ they played. Tell students they will now start over and play again for another minute. When done, ask the students to add up their scores and how many ‘rounds’ they played.

Ask some students to share how they kept track of player scores. There may be some variety, but most will have written down the players’ names and then beside or below the names, marks representing the ‘wins’ of each player. And they may have made a separate place for recording ties.

Ask the students what parts of the score sheet represent constants, values that do not change through the course of a gaming session.

Ask the students what parts of the score sheet represent variables, values that do change through the course of a gaming session.

This micro:bit activity guides the students to create a program with three variables that will keep score for their Rock Paper Scissors game.

Tell the students that they will be creating a program that will act as a scorekeeper for their next Rock Paper Scissors game. They will need to create variables for the parts of scorekeeping that change over the course of a gaming session. What are those variables?

The number of times the first player wins The number of times the second player wins the number of times the players tie Creating and naming variables: Lead the students to create meaningful names for their variables.

What would be a unique and clear name for the variable that will keep track of the number of times Player A wins? Student suggestions may be: PAW, PlayerA, AButtonPress, AButtonCount, PlayerAWins… Discuss why (or why not) different suggestions make clear what value the variable will hold. In general, variable names should clearly describe what type of information they hold. In MakeCode, from the Variables menu, make and name these three variables: PlayerAWins, PlayerBWins, PlayersTie.

Add on mathematical operations: There is more we can do with the input we received using this program. We can use mathematical operations on our variables.

Example: Perhaps you’d like to keep track of, and show the player the total number of ‘rounds’ that were played. To do this, we can add the values stored in the variables we created to keep track of how many times each player won and how many times they tied.

In order to do this, we can add the code to our program under the ‘on shake’ event handler.

First, display a string to show the player that the following sum represents the total number of rounds played. Our program will add the values stored in the variables PlayerAWins, PlayerBWins, and PlayersTie and then display the sum of this mathematical operation. The blocks for the mathematical operations adding, subtracting, multiplying, and dividing are listed in the Math section of the Toolbox.

This is an assignment for students to come up with a micro:bit program that counts something. Their program should keep track of input by storing values in variables, and provide output in some visual and useful way. Students should also perform mathematical operations on the variables to give useful output.

Supplements

Any items in this section are the property & under the license of their respective owners.

REVIEW

Have students write a reflection of about 150–300 words, addressing the following points:

What was the problem you were trying to solve with this project? What were the Variables that you used to keep track of information? What mathematical operations did you perform on your variables? What information did you provide? Describe what the physical component of your micro:bit project was (e.g., an armband, a wallet, a holder, etc.) How well did your prototype work? What were you happy with? What would you change? What was something that was surprising to you about the process of creating this project? Describe a difficult point in the process of designing this project, and explain how you resolved it.

STANDARDS

TypeListing
CS DomainsAlgorithms and Programming
CS PrinciplesFostering an Inclusive Computing Culture, Collaborating Around Computing, Recognizing and Defining Computational Problems, Creating Computational Artifacts, Testing and Refining Computational Artifacts, Communicating About Computing
Other Content StandardsCL.L2-03 Collaborate with peers, experts, and others using collaborative practices such as pair programming, working in project teams, and participating in group active learning activities CT.L1:6-01 Understand and use the basic steps in algorithmic problem-solving CT.L1:6-02 Develop a simple understanding of an algorithm using computer-free exercises CPP.L1:6-05 Construct a program as a set of step-by-step instructions to be acted out 2-A-5-7 Create variables that represent different types of data and manipulate their values.