Declaring Variables
You'll learn how to properly introduce a new variable into your program so the computer knows it exists and can store data in it.
What Does "Declaring" Mean?
In the previous lesson, you learned that a variable is like a labeled box that holds data. Declaring a variable means officially announcing to the computer: "I'm going to need a box with this name, and here's what kind of data it will hold."
Think of it like reserving a parking spot. Before you can park your car (store data), you need to tell the parking lot manager, "I need spot #42, and it's for a sedan" (declare the variable with a name and type).
The Basic Syntax
Most programming languages require you to declare variables before using them. The declaration typically includes:
- The data type (what kind of value: number, text, true/false, etc.)
- The variable name (what you'll call it)
Here's the general pattern:
type variableName
For example, if you wanted a variable to hold a whole number representing someone's age, you might declare:
integer age
This tells the computer: "Create a variable called age that will store integer (whole number) values."
Naming Rules
Variable names usually must:
- Start with a letter (not a number)
- Contain only letters, numbers, and underscores
- Not be a reserved word the language already uses
- Be descriptive (use
studentCountrather thanx)
Key Takeaway: Declaring a variable is how you formally introduce it to your program, specifying both its name and the type of data it will hold—like reserving a labeled storage space before you need it.