Variables and Data Types: Storing Values in Memory
The Daily-Life Doorway: Labeled Storage Boxes
Imagine you have a row of plastic storage boxes on a shelf in your room. On the outside of each box, you stick a bright label like "Age", "Player Name", or "High Score". Inside the box labeled "High Score", you slip a piece of paper with the number 100 written on it.
When you beat your record, you do not throw away the box. You take out the paper showing 100, erase it, and put in a new paper showing 150. The box label stays the same, but the item inside changes. In computer programming, a variable is simply a labeled memory box that holds one piece of information at a time.
What Is a Variable and What Is a Data Type?
Your computer's memory is a massive grid of billions of tiny storage slots. A variable gives a human-friendly name to one of those storage slots so you can find it later. Every piece of information stored inside a variable belongs to a specific data type.
Just as you would not store soup in a cardboard box, a computer needs to know what kind of data it is holding so it knows how to handle it. The four primary data types used in almost every program are:
- Integer: Whole numbers without decimal points, like
12,0, or-5. - Floating-Point (Float): Numbers that contain decimal points, like
19.99or3.14. - String: Text characters wrapped inside quotation marks, like
"Aarav"or"Hello World". - Boolean: A simple logical flag that can only be
TrueorFalse.
Core Rules of Variables
To use variables correctly, you must follow three fundamental rules of computer memory:
- Assignment side: The variable name always sits on the left side of an equals sign, and the value sits on the right.
- Overwriting: Putting a new value into a variable erases whatever was inside that box before.
- Quotes dictate type: Any text inside quotation marks becomes a string, even if the characters inside look like a number.
Pseudocode Walkthrough: Storing and Updating Memory
Let us trace how a computer handles variables step by step in plain pseudocode.
// Step 1: Create a variable named score and put 10 inside it
SET score TO 10
// Step 2: Add 5 to the current value of score and store the new result back in score
SET score TO score + 5
// Step 3: Create a string variable for the player name
SET player_name TO "Aarav"
// Step 4: Create a boolean flag for the game status
SET is_game_over TO False
In Step 1, the computer reserves a memory slot, labels it score, and marks it as an integer holding 10. In Step 2, the computer reads the current value (10), adds 5 to make 15, and overwrites the old 10 with 15 inside the same box.
Comparing Data Types
Understanding how data types differ helps you choose the right container for your program's data.
| Data Type | Example Value | What the Computer Sees | Common Use Case |
|---|---|---|---|
| Integer | 12 |
A whole number ready for math | Counting items, age, player score |
| Float | 19.99 |
A decimal value with fractional parts | Prices, exact measurements, weight |
| String | "12" or "Aarav" |
A sequence of visual text characters | Usernames, email addresses, messages |
| Boolean | True |
A binary 1 or 0 logical switch | Checking if logged in, game over flags |
Where This Breaks in Real Programs
A classic issue occurs when you mix text strings with mathematical numbers. To a human, "5" and 5 look identical. To a computer, 5 is a number that can be added, while "5" is a picture of a character.
If you try to perform math on a string, such as adding "10" + 5, the program will crash or produce unexpected results. You must convert the string into a whole number before performing arithmetic operations.
Where This Sits in a Real Career
Every software engineer, from game developers building physics engines to data analysts tracking financial markets, starts by picking the correct data type. Selecting an inefficient or incorrect data type can lead to memory leaks, slow performance, or critical calculation bugs in real-world applications.
Deeper Rules and Hidden Exceptions
As you build larger programs, variables behave in ways that can surprise you if you do not know what happens inside computer memory.
Imagine you have a piece of paper on your desk where you write down a math answer. If a friend copies that number onto their own sheet of paper, changing the number on your paper later does not automatically change what is written on your friend's paper. They are two separate sheets of paper.
In programming, calculation variables work the exact same way. Look at this pseudocode:
// Step 1: Set item_price to 100
SET item_price TO 100
// Step 2: Calculate total_cost based on item_price
SET total_cost TO item_price + 20
// Step 3: Change item_price to 50
SET item_price TO 50
A common myth is that changing item_price to 50 in Step 3 will automatically update total_cost to 70. It does not. When Step 2 runs, the computer calculates 100 + 20 and puts 120 inside the total_cost box. Step 3 changes item_price, but total_cost stays at 120 unless you run the calculation step again.
Another surprise involves floating-point numbers. Imagine trying to divide a single apple equally among three friends. Each person gets roughly 0.3333 apples, but no matter how many decimals you write down, you can never write the exact fractional amount using simple decimal numbers. Computers face the same problem when converting decimal math into binary chips. Calculating 0.1 + 0.2 in a program often produces 0.30000000000000004 instead of exact 0.3. When writing software for banks, programmers avoid simple floating-point numbers to prevent losing fractions of a cent.
Mistakes That Cost Marks
When writing code in exams or practical tests, small structural mistakes can stop a program from running completely. Here are four common mistakes and how to fix them:
Mistake 1: Flipping the Assignment Operator
Wrong:
100 TO score
Right:
SET score TO 100
Why: The computer always expects the variable name on the left side of the assignment sign, receiving a value from the right side.
Mistake 2: Adding Spaces inside Variable Names
Wrong:
SET user age TO 15
Right:
SET user_age TO 15
Why: A computer reads a space as a separator between two different commands. Use an underscore (_) to connect words in a single variable name.
Mistake 3: Mixing String Characters with Math Numbers
Wrong:
SET final_score TO "10" + 5
Right:
SET final_score TO 10 + 5
Why: "10" is stored as text characters, while 5 is a mathematical integer. You must use whole numbers without quotation marks to perform math operations.
Mistake 4: Lowercase Boolean Keywords
Wrong:
SET is_passed TO true
Right:
SET is_passed TO True
Why: Most strictly structured systems require boolean flags like True and False to start with a capital letter.
How You Are Tested in Exams and Practical Work
School exams test your ability to trace code step by step without running it on a computer. You will often be given a block of pseudocode with multiple reassignments and asked to write down the final value stored inside each memory box at the end.
In technical interviews and professional projects, companies test your judgment in choosing the right data type for efficiency. Storing an age as a string wastes unnecessary memory space, while using an integer for a phone number removes leading zeros like 01234, breaking the contact data.
Ready to Put Your Memory Boxes to the Test?
You now understand how computer memory reserves named slots, how values overwrite each other, and how data types govern what operations you can perform. Move on to the practice questions to test your skill in identifying data types and tracing variable values!
