Variables, Conditionals, and Functions - Lesson 1: Variables Explore

Overview

To begin the lesson students explore sample apps similar to the ones they'll be able to build by the end of the unit. Then students complete an unplugged activity with plastic baggies and sticky notes to build a mental model of how variables are used to move and store information. The lesson ends with a synthesizing discussion and students adding key vocabulary to their journal.

Goals

Students will be able to:

  • Use appropriate vocabulary to describe variables, expressions, and variable assignment.
  • Evaluate expressions that include numbers, strings, and arithmetic operators.
  • Trace simple programs that use variables, expressions, and variable assignment.

Purpose

The warm up activity is designed to provide context for the coming unit and motivate the reasons students will want to learn the concepts covered in Unit 4. The subsequent activity provides students a physical mental model they will be able to use when they start programming with variables in the subsequent lessons.

Preparation

  • Collect for each pair of students:
    • - 3 sandwich baggies
    • - packs of red and yellow stickies
    • - pens / pencils
    • - 1 dry erase marker per four students (pairs can share)
  • Review the sample apps shown in the warm up
  • Review the rules and vocabulary used in the activity

Resources

Teaching Tip

This is the first official "Explore" lesson in the EIPM model. Review the EIPM model in the EIPM: A Short Introduction - Resource

Overview: Students explore the new concept through a teacher-led hands-on group activity.

  • Typically uses physical manipulatives
  • Teacher leads with support of slides and activity guides

Goal: Students begin to develop a shared mental model and understand the main ideas of the new concept.

A sketch showcasing students performing an unplugged activity to explore the concept of variables.

Watch the following videos:

Getting Started (5 minutes)

Explore Sample Apps

Group: Place students in pairs.

Do This: Give students 3-5 minutes to explore the sample apps (beginning with Lesson1_App1 in Unit4 of the CSP-Widgets repository). Students can explore one or more depending on time.

Discuss: These are samples of the kinds of apps you'll be able to build by the end of this unit. As you go through them, write down at least two examples where the app seems to be keeping track of a piece of information or using it to make decisions.

Discussion Goal

Discussion Goal: Have volunteers share what they noticed with the room. Some ideas include:

  • The Pet Rock App keeps track of button selections and uses it to decide when the pet rock will "evolve"
  • The Poem App keeps track of the poem as you write it.

Preview the Unit

  • Last unit we built apps that mostly focused on input and output. For example:
    • - When button is selected, show a picture
    • - When button is selected, change screen
    • - When button is selected, play a sound
  • But we all want our programs to do more than that! In this unit we're going to learn how to build apps that keep track of information and use to make decisions and perform calculations. This is going to let us build much more powerful apps! So let's get started by diving in deep on what's involved with programming with information.

Activity (40 minutes)

Group: Group students in pairs.

Distribute: Give each pair of students

  • A small stack of red and yellow sticky notes
  • A pen / pencil
  • 3 plastic baggies
  • A dry erase marker to share with another group
Teaching Tip

Supplies Substitutions: There's no need to use stickie notes if you have other scraps of colored paper. Also consider cutting stickies in 4 to make them go further. If you don't have dry erase markers handy consider using pieces of masking tape on the baggies.

Alternatives for Accessibility or Distance Learning: When working with students remotely or students who are blind or visually impaired, consider having students type into an online document rather than using sticky notes or baggies. In this case, individual values can be represented on a single line, where strings are denoted by quotes and numbers don't have quotes. To represent variables, have students write the variable name followed by its value on a single line. Students form expressions by combining standalone values, variables, and operators on a single line.

Using an online document in this way will produce lines of text that will look a lot like code. The important part of the lesson is bringing attention to the concept of storing information in one place, using it again later, or replacing the information in that place with new values.

Guided Activity: Today's activity introduces students to the concept of variables. As a visual aid, you can use Code.org's presentation slides for Unit 4, Lesson 1: Variables Explore. These slides include animations. The notes below describe when to move to the next slide or click through an animation -- if you aren't using the slides, you can ignore these prompts.

Teaching Tip

Running the Activity: This activity asks students to follow along as a number of core concepts for programming are introduced. The model is typically that a term or concept is introduced and modeled and then afterwards students are encouraged to try it out on their own. Trying it out typically means they are writing information on a sticky note and sharing it with another group before discussing the results with the whole class.

Slides with animations have an icon in the bottom left corner to let you know you need to click to reveal more of the slide's content.

To help you more easily prepare the activity and keep track of your instructions, detailed instructions have been included as speaker notes in the presentation. Here are some tips to help you throughout the presentation.

  • There are opportunities throughout the presentation for students to actively engage. At these moments students should be making things with their manipulatives or using them to answer questions. Use these opportunities to check progress.
  • There is a fair amount of new vocabulary introduced but it is introduced gradually and with intentional repetition. Make a point of actively modeling the use of new terms.
  • The most important goal here is building a mental model. It is ok if students have some open questions that will get resolved over the subsequent conditional lessons.
  • Both you and students can use the "Key Takeaways" to check your understanding at the end.

Guided Activity

Follow this lesson with the supplemental slides.

Slide: Variables Explore

Say: Today we are going to explore Variables.

Slide: Value: Numbers, Strings

Say: We're going to be thinking about how computers work with information. We're going to call one "piece" of information a "value". Right now there are two different types of values: numbers and strings. There are a few ways to tell them apart. Numbers work the way you normally think of numbers. They are made of the digits 0 through 9. We are going to put numbers on yellow sticky notes. When we write numbers you don't need quotes. Strings are made of any characters you can see on the keyboard which means all these examples count as strings. Strings go inside double quotes. You can see how it would be important to tell the number 123 apart from the string "123".

Do This: Make one string and one number and hold it up at your table.

Slide: Operators

Say: Operator is just a fancy name for the plus, minus, multiply, and divide symbols. An expression is a combination of two sticky notes and an operator. When I ask you "what's 2 plus 3" you say "5"! You just "evaluated" the expression 2+3, or figured out what the value is. Since 5 is a number, we put it on a yellow sticky. When evaluating an expression, we follow order of operations - just like in math class.

Do This: Evaluate the expression 5-1 and make a sticky note. Make sure you pick the correct color and decide if you need quotes or not!

Slide: Expression Table

Say: This table shows the different ways we can create expressions. We can use all four operators with numbers the normal way. When you want to use strings, you can only use the + operator and it connects the two words together. If you're connecting a number and a string, the number first gets converted to a string.

  • 3 + 4 evaluates to 7
  • 5 - 2 evaluates to 3
  • 11 * 2 evaluates to 22
  • 10 / 2 evaluates to 5
  • "for" + "ever" evaluates to "forever"
  • "gr" + 8 evaluates to "gr8"
  • 2 + "day" evaluates to "2day"

Do This: With your partner make a sticky note for each of these 5 expressions and we'll share as a class.

  1. 4 + 5
  2. 10 - 9
  3. "tree" + "house"
  4. "you" + "r"
  5. 3 + "D"

Click for animation: Click through to see the answers on the slide.

Slide: Variables

Say: We're going to call the plastic baggies on your table "variables". Variables can hold at most one value, or sticky note. They have names that use no quotes, include no spaces, and must start with a letter. For now just practice making a variable with your partner.

Do This: Make one variable with any name you like. Share it with another group. Make sure you use a whiteboard marker so we can reuse the baggies later.

Slide: Variables and Expressions

Say: We can evaluate expressions that include variable names. To do that, first make a copy of the sticky note inside the variable. Then evaluate the expression the way you normally would. These two examples show you how.

  • Example #1:
    1. - zip = 5
    2. - 3 + zip evaluates to 3 + 5
    3. - 3 + 5 evaluates to 8
  • Example #2:
    1. - bop = "hi"
    2. - bop + zip evaluates to "hi" + 5
    3. - "hi" + 5 evaluates to "hi5"

Click for animation: Click through to see the answers to the examples on the slides.

Slide: Do This: Evaluate these expressions.

Do This: Evaluate these expressions. Make sure to pay attention to whether it evaluates to a string or a number.

Variables:

  • boo = 4
  • rar = "be"

Expressions:

  1. 3 * boo
  2. rar + "ep"
  3. rar + boo

Note: Have students share their answers by holding up the sticky notes for each solution.

Click for animation: Click through to see the answers.

Slide: Let's start writing programs...

Say: Now let's write programs. We're going to stop using sticky notes, but will highlight strings and numbers to help you remember the difference.

Slide: var

Say: Here's what a program looks like. The var command tells the computer to create a variable.

  • var pow : creates a new variable. Grab a new baggie and write the variable's name (in this case, "pow") on it.
Teaching Tip

Teaching Tip: The slides show code examples that reflect how variables are used in Javascript, which is used for Code.org's App Lab. At this point, the concept of variables is more important than the exact code. There will be opportunities in later slides to discuss the differences between how the two languages handle variables.

Slide: "Assignment operator"

Say: The left arrow is called the "assignment operator". That's just a fancy word for "put this value in the baggy". If we wanted to read line 01 we would say "pow gets 3". We know that variables can only hold one sticky note or value. So if we try to assign a variable that already has a value in it, we just throw the old one away.

Do This: Run this program. What is the result?

  1. var pow
  2. pow <- 3
  3. pow <- 5

Click for animation: Click through to run the program.

Say: In a computer you don't actually put the old number in a trash can, it's totally deleted! The numbers are stored as electrical charges somewhere in your computer's memory. When you assign a new number to a variable it actually erases the old number, and stores the new number in its place.

Slide: Do This...

Do This: Run this program. Compare your results with another group.

  1. var pizza
  2. pizza <- 3
  3. var tacos
  4. pizza <- "yum"
  5. tacos <- "the best"

Note: Walk around the room making sure students are following the rules correctly. No baggies should have more than one sticky note in them.

Click for animation: Click through to see the answers.

Teaching Tip

Teaching Tip: This is a good opportunity to highlight the difference between variables in Javascript and Quorum. In Javascript, a single variable can hold different types of values over a program -- for example, the "pizza" in this program stores a number, then replaces it with a string. In Quorum, this isn't allowed, because variables must store a specific type. Rather than declaring "var pizza", we might've declared "number pizza" or "text pizza, but it couldn't store both.

Alternately you can use the Quorum code found in Lesson1_SlideAnswers project folder in the CSP-Widget repository.

If you want to highlight this, you can use this optional prompt:

Discuss: In our program, we can store either a string or a number in the same variable. What if we said each variable could only hold one type of value instead? What are some of the advantages or disadvantages of that approach?

  • Allowing variables to hold any type makes them more flexible and makes them easier to reuse.
  • Restricting what types each variable can hold makes them more consistent and predictable -- a text variable always contains text, for example.
  • These two approaches are known as dynamic typing andstatic typing, respectively, and both are very common in real-world programming languages!

Slide: Assign a Variable with Expression

Say: Now let's combine what we've learned. You can use assignment with expressions. In order for this to work you need to evaluate first, then assign. This makes sense because we know we can only put one sticky note in the variable baggy.

Slide: Do This...

Do This: Run the program. Compare your results with another group.

  1. var zow
  2. var fly
  3. fly <- 2 + "day"
  4. zow <- 4 - 1
  5. fly <- 3 * 3
  6. zow <- 4 + "now"

Note: Walk around the room making sure students are evaluating before they assign. Reinforce the language "gets" for the assignment operator. For example, line 02 would read "fly gets two plus day".

Click for animation: Click through to see the answers.

Slide: We're not going to...

Say: We're not going to highlight our strings and numbers anymore. We can just use double quotes around the strings to tell the difference. (NOTE: If you aren't using the slides or otherwise highlighting the values, you can skip this.)

Slide: Assign a Variable: Expressions with Variables

Say: Let's run this program. Evaluate the expression on the right first to get one value. Note that variables aren't "connected." Changing kit doesn't change boo.

Do This: Run through this program together as a class.

  1. var kit
  2. kit <- 1
  3. var boo
  4. boo <- kit + 1
  5. kit <- 5

Note:Reinforce:

  • Evaluate, then asssign.
  • As shown on line 04, there's no special "connection" made between variables. All we're doing is moving information around.
  • Variables only hold one value, the old one is "thrown away" (again, what's happening is that assignment replaces the electric charges somewhere inside the computer so the old value is actually "erased" or "deleted")

Click for animation: Click through to see the answers.

Slide: Do This:

Do This: Run this program. Compare your results with another group.

  1. var fuzz
  2. var clip
  3. fuzz <- 5
  4. clip <- fuzz + 2
  5. fuzz <- clip + 1
  6. clip <- "gr" + fuzz
  7. fuzz <- fuzz + 1
  8. fuzz <- fuzz + 1
  9. fuzz <- fuzz + 1

Note: Have students hold up their two baggies when they're done. Make sure students are evaluating and then assigning. Call out that last three lines which can be a little nutty to think about. You're using a variable's value as part of the assignment. This lets it "count up by one".

Click for animation: Click through to see the answers.

Slide: Key Takeaways

Do This: Review the key takeaways with students.

  • Numbers and strings are two different types of values
  • Expressions evaluate to a single new value
  • When variables are in the expression just make a copy, don't change the actual variable.
  • Variables are "assigned" a new value
  • Evaluate first, then assign
  • Old values are deleted forever.
  • Assignment just moves information around. It does not "connect" variables.

Slide: In some languages...

Say: In some programming languages (including Quorum and Javascript) the assignment operator is not written with an arrow but is written as an equal sign.

Slide: So the command...

Say: That means that in those languages, fuzz <- fuzz + 1 would be written as fuzz = fuzz + 1. We'll see more of this in the next lesson!

Note: In math "=" means "are equal forever." In programming "=" means" put this value in this variable".

Wrap Up (10 minutes)

Journal

Have students add the following words to their journals: Expression, Variable, Assignment Operator.

  • Expression: a combination of operators and values that evaluates to a single value.
  • Variable: an abstraction inside the program that can hold a value. Each variable has associated data storage that represents one value at a time.
  • Assignment operator: allows a program to change the value represented by a variable.

Remarks

  • This isn't the last time we'll look at these definitions and it's fine to update them as you get more experience. In fact, I'm sure you'll have a lot more to add once we see all of these concepts used inside of apps when we meet next time!

Assessment: Check for Understanding

Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.

For Students

Open a word doc or google doc and copy/paste the following question.

Question

What will the value of the variable score be when the following code is finished running?

var score
score <- 3
score <- score + 1
score <- "The score is: " + score

Standards Alignment

  • CSTA K-12 Computer Science Standards (2017): 2-AP-11
  • CSP2021: AAP-1.A.1, AAP-1.B.1, AAP-1.B.3, AAP-1.C.4
  • CSP2021: AAP-2.B.3, AAP-2.B.4, AAP-1.B.5, AAP-2.C.1, AAP-2.C.4
  • CSP2021: DAT-1.A.1

Next Tutorial

In the next tutorial, we will discuss CSP Unit 4 Lesson 2 Variables Investigate, which describes Investigate how variables are used.