Variables vs. Constants
Understanding how programs store and manage data — a fundamental CSEC IT skill!
Data Storage: The "Memory Box" Concept
The Metaphor: Imagine computer memory as a giant wall of labeled post-office boxes. Each box can hold one piece of information at a time, and you can find your data later by looking at the label!
Identifiers: The Labels
Variables and Constants are simply names we give to storage locations in memory. These names (called identifiers) help us find and use our data without remembering complex memory addresses.
- Memory Address: The actual physical location in computer RAM (like "Box #4521")
- Identifier: A meaningful name we use instead (like "Student_Age")
- Value: The actual data stored in that location
Click a box, give it a label and value to store data!
Identifier
The name given to a storage location (like "Score" or "PI")
Value
The actual data stored in memory (like "100" or "3.14")
Memory
Computer storage (RAM) where programs keep their data
What is a Variable? (The Changing Value)
Definition: A variable is a named storage location in memory whose value can change (vary) during the execution of a program. Think of it like a container where you can put different things over time.
Real-World Examples
- Current_Score in a video game — increases or decreases as you play
- Temperature on a weather app — changes as weather conditions update
- User_Password during a login attempt — typed differently each time
- Shopping_Cart_Total — changes as you add or remove items
Why Variables Matter
Programs need to remember things that change: user input, calculated results, game states, and more. Variables give us a way to store and update this dynamic information.
Click the buttons to see how a variable's value changes!
Variable
A storage location whose value can change during program execution
Assignment
Setting or changing a variable's value (e.g., Score = 10)
Mutable
Another word for "can be changed" (variables are mutable)
What is a Constant? (The Fixed Value)
Definition: A constant is a named storage location whose value remains the same throughout the entire program. Once set, it cannot be changed.
Why Use Constants?
- Prevents accidental changes: Protects critical values like tax rates
- Makes code readable: Using "PI" is clearer than "3.14159"
- Easier to update: Change one constant instead of many formulas
- Self-documenting: Shows that certain values should never change
Common Examples
- PI = 3.14159 — The ratio of a circle's circumference to its diameter
- GRAVITY = 9.8 — Earth's gravitational acceleration
- SALES_TAX = 0.15 — Jamaica's 15% GCT rate
- DAYS_IN_WEEK = 7 — Always 7, never changes
Try to change the constant value to see what happens!
Constant
A storage location whose value never changes during program execution
Immutable
Another word for "cannot be changed" (constants are immutable)
Literal Value
The actual number or text written in code (like 3.14 or "Hello")
Declaration and Initialization
Before using a variable or constant, you must tell the computer about it. This happens in two steps:
Declaration
Declaration tells the computer the name and type of the storage location. You're creating the "box" and putting a label on it.
DECLARE Total_Score AS Integer
DECLARE PI AS Real
Initialization
Initialization gives the variable or constant its very first value. For constants, this happens at declaration. For variables, it can happen later.
SET PI = 3.14159
SET Total_Score = 0
Naming Rules (Identifiers)
- Must start with a letter (A-Z or a-z)
- Can include numbers but not at the start (e.g., score1 is OK, 1score is not)
- Cannot contain spaces (use Student_Name or studentName)
- Cannot use reserved words like IF, THEN, FOR as names
- Should be descriptive (use Player_Score instead of x or y)
- Special characters allowed: Only underscore (_) is typically allowed
Click each identifier to judge if it's valid or invalid!
Variable vs Constant Declaration
Variable: DECLARE Score AS Integer
Constant: CONSTANT TAX_RATE = 0.15
Choosing the Right Tool: Variable or Constant?
Ask yourself this simple question: "Will this value ever need to be updated while the program is running?"
Decision Logic
Will the value change during program execution?
✅ YES → Use a VARIABLE
❌ NO → Use a CONSTANT
Examples in Context
- User's Age — Variable (might update on birthday)
- Number of Hours in a Day — Constant (always 24)
- Price of Bread — Variable (changes over time)
- Speed of Light — Constant (never changes)
- Game Level — Variable (increases as player progresses)
- Days in a Week — Constant (always 7)
Drag each item to the correct category!
CSEC Exam Tip: Avoiding "Hard-Coding"
What is "Hard-Coding"? Writing actual values directly in your code (like typing "0.15" in every formula).
Bad Practice (Hard-Coding):
Total = Subtotal * 0.15
Tax2 = Item2 * 0.15
Tax3 = Item3 * 0.15
Best Practice (Using Constants):
CONSTANT SALES_TAX = 0.15
Total = Subtotal * SALES_TAX
Tax2 = Item2 * SALES_TAX
Tax3 = Item3 * SALES_TAX
Why is this better?
- If the tax rate changes to 0.16, you only change ONE line (the constant declaration)
- If you hard-coded "0.15" in 100 places, you'd have to change all 100!
- Constants make your code self-documenting and easier to maintain
When to Use Constants in Your SBA
Tax Rates
GCT, income tax rates that might change
Configuration Values
Maximum limits, timeouts, buffer sizes
Physical Constants
PI, conversion rates, scientific values
Business Rules
Minimum order amounts, discount percentages
Knowledge Check: Memory Master Quiz
Question 1: Logic Question
If you are writing a program to calculate the area of a circle, which value should be a constant and which should be a variable?
Question 2: Syntax Question
Identify the error in this pseudocode: SET 2024_Year = "January"
Summary: Variables and Constants
Great job completing this module! You now understand the fundamental difference between variables and constants.
Key Differences
📦 Variable
Can change during program execution. Used for dynamic data like scores, temperatures, and user input.
🔒 Constant
Cannot change once set. Used for fixed values like PI, tax rates, and unchanging rules.
Remember These Rules
- Declaration: Tell the computer the name and type
- Initialization: Give the first value
- Naming: Must start with a letter, no spaces, be descriptive
- Choice: Ask "Will this change?" — Yes = Variable, No = Constant
🧠 Memory Tip:
"VARIABLE = Varies, CONSTANT = Consistent"
