Variables & Constants (Cambridge International IGCSE Computer Science)
Exam board Cambridge International · IGCSEUpdated 2024-12-03
Spec point —
8.1.1· 1 Declare and use variables and constants
Variables & Constants
What is a variable?
-
A variable is an identifier that can change in the lifetime of a program
-
Identifiers should be:
- In mixed case (Pascal case)
- Only contain letters (A-Z, a-z)
- Only contain digits (0-9)
- Start with a capital letter and not a digit
-
A variable can be associated a datatype when it is declared
-
When a variable is declared, memory is allocated based on the data type indicated
| Pseudocode |
|---|
| DECLARE : |
DECLARE Age : INTEGER |
DECLARE Price : REAL |
DECLARE GameOver : BOOLEAN |
| Python |
|---|
score = int() # whole number |
cost = float() # decimal number |
light = bool() # data can only have one or two values |
What is a constant?
- A constant is an identifier set once in the lifetime of a program
- Constants are generally named in all uppercase characters
- Constants aid the readability and maintainability
- If a constant needs to be changed, it should only need to be altered in one place in the whole program
| Pseudocode |
|---|
| CONSTANT ← |
CONSTANT PI ← 3.142 |
CONSTANT PASSWORD ← "letmein" |
CONSTANT HIGHSCORE ← 9999 |
| Python |
|---|
PI = 3.142 |
VAT = 0.20 |
PASSWORD = "letmein" |
Was this revision note helpful?
Build on this topic