Variables and constants (Edexcel GCSE Computer Science): Revision Notes
Variables and constants
What are variables and constants?
Variables and constants are fundamental building blocks in programming and algorithms. Think of them as labelled storage boxes in a computer's memory where you can keep different pieces of information.
Variables are like containers that can hold different values throughout a programme. The data inside them can be changed or updated as the programme runs. For example, a variable called score might start at 0 but increase to 10, then 25, and so on.
Constants are similar containers, but once you put a value in them, that value cannot be changed while the programme is running. They're like locked boxes - once sealed, the contents stay the same. For example, a constant called PI would always contain the value 3.14159.
Both variables and constants represent specific locations in the computer's memory where binary data patterns are stored and manipulated by programmes. Understanding this concept is essential for grasping how programmes manage information.
Choosing good identifiers
Identifiers are the names you give to your variables and constants. Picking good names is crucial for writing clear, understandable code.
Tips for creating identifiers:
Best Practices for Naming:
- Avoid short, unclear names - names like t or x don't tell you what the data represents
- Use meaningful, descriptive names - totalScore and studentGrades are much better than ts and sg
- Make them explain the purpose - good identifiers help other programmers (and future you!) understand what the data is used for
Important rules:
- Identifiers cannot be the same as reserved words that the programming language already uses (like if, while, for)
- Choose names that clearly describe what information is being stored
Naming conventions
Programming languages have specific rules for how to write variable and constant names. Following these conventions makes your code easier to read and more professional.
For variables:
Use camel case - this means starting with a lowercase letter, then capitalising the first letter of each new word. Examples:
- firstName
- totalMarks
- averageTemperature
For constants:
Use ALL UPPER CASE letters. Examples:
- PI
- MAX_STUDENTS
- TAX_RATE
The identifier firstName follows variable naming conventions (camel case), while VAT follows constant naming conventions (all capitals).
Assignment - giving values to variables and constants
Assignment is the process of storing a value in a variable or constant. It's like putting data into your labelled storage container.
In programme code:
Use the equals symbol (=) for assignment:
firstName = "David"
score = 85
In flowcharts and written descriptions:
Use the word SET followed by TO:
SET firstName TO "David"
SET score TO 85
Getting input from users:
You can assign values that users type in:
GET name
name = input("Enter your name")
Displaying stored values:
When you want to show what's stored in a variable:
OUTPUT name
print(name)
This displays the actual value stored (like "David") rather than just the word "name".
Worked example
Worked Example: Calculating Area with Variables
Let's look at a practical example of using variables in an algorithm:
1. length = float(input("Length: "))
2. width = float(input("Width: "))
3. area = length * width
4. print(area)
The variables used are: length, width, and area
Purpose of each variable:
- length and width store numerical values that the user inputs
- area stores the calculated result of multiplying length by width
This example shows how variables can store both user input and calculated results, making programmes interactive and useful.
Practice opportunity
Practice Scenario: A student is writing an algorithm that lets users input marks for five different tests, then calculates the average mark.
Think about what variables would be needed to store the test data in this programme. Consider what information needs to be kept track of and how it might be used in calculations.
Remember!
Key Points to Remember:
- Variables can change their values during programme execution, while constants stay the same
- Use descriptive names for identifiers to make your code clear and professional
- Follow naming conventions: camelCase for variables, ALL_CAPS for constants
- Assignment gives values to variables using = in code or SET...TO in descriptions
- Variables store different types of data including user input, calculations, and programme results