CRCD.T1.M2.02: Variables

Introduction to Variables

How do we change parameters?

In this lesson, we are going to explore Variables,which are placeholders that hold information that can change.  For instance, have  you ever dyed your hair, say, purple on one day, and pink on the next?  Your hair could be represented by a color variable!

Variables

Variables hold information and an be accessed using their labels.

Vocabulary

  • Variable - A label for a piece of information that can change

New Code

  • Declare and assign a value to a variable
  • Declare a variable

 

Naming Variables

Rules for Naming Variables

Variable labels should be meaningful, but you can choose almost any label you like. There are just a few rules to be aware of.

CamelCase

Labels with multiple words can be easier to read in camelCase. A camelCase label looks like sizeOfRectangle or aReallyLongLabelName. The first letter of the variable name is usually lower case, each new word starts with a capital letter. This helps you see the start of new words without using spaces, which are not allowed in variable names.

Naming Rules

There are a few rules when choosing labels:

  • Labels cannot include spaces. For example, width of rectangle would generate an error.
    • width of rectangle  becomes widthOfRectangle
  • Labels cannot begin with a number. 4sides and 2morrow will generate errors.
    • 4cat becomes fourCat
  • Labels CAN end with a number
    • cat 4 becomes cat4
    • big dog 2 becomes bigDog2
  • Be very careful with spelling. If labels are not spelled exactly the same way, the computer will not realize that they refer to the same variable.
    • yLocation is NOT the same as YLocation
  • Labels are case-sensitive. size is not the same as Size or SIZE.
    • cat, CAT, and Cat are 3 different variables (even if 2 of them are named incorrectly!)

 

Want to Practice?

Naming Variables Practice Sheet

Naming Variables Practice Sheet Answer Key

 

Note: Most Instructional content is taken from the Code.org Curriculum in CS Discoveries 2021-2022 Links to an external site.