Variables & Constants
Variables and constants are used to store a single item of data in a program. This can be accessed through the identifier. Variables can be changed during program execution while constants remain the same.
Declaring Variables and Constants
- Variables are declared using a data type, a name and a value (optional)
- Constants are declared using the 'const' keyword, a name and a value
- In all programming languages, variable names should follow certain rules, such as:
- Starting with a letter
- Not containing spaces
- Can contain letters, numbers, _ or $
- Not using reserved words (like if, while, for etc.)
- Examples of data types include integer, float, boolean, and string
Examples in Pseudocode:
Declare a variable called 'score' with a value of 10
score ← 10
Declare a constant called 'PI' with a value of 3.14
const PI ← 3.14
Examples in Python:
Declare a variable called 'score' with a value of 10
score = 10
Declare a constant called 'PI' with a value of 3.14
PI = 3.14
Examples in Java:
Declare a variable called 'score' with a value of 10
int score = 10;
Declare a constant called 'PI' with a value of 3.14
final double PI = 3.14;
Examples in Visual Basic:
Declare a variable called 'score' with a value of 10
Dim score As Integer = 10
Declare a constant called 'PI' with a value of 3.14
Const PI As Double = 3.14
Exam Tip
- Exam questions will ask you to write pseudocode statements, rather than in a specific language
Worked example
The variables 'name' and 'age' are used to store data in a program:
- name stores the user’s name
- age stores the user’s age
Write pseudocode statements to declare the variables name and age.
[2]
DECLARE name : STRING [1]
DECLARE age : INTEGER [1]