GCSE Computer Science/Constants, variables and data types
Constants, variables and data types
A constant is a value used in programming which will not change throughout the program.
For example: age = 20; In this example, 'age' is a constant because we will not change it
A variable is a value used in programming which normally changes as the program is executed. The value of a variable can be changed in a number of ways:
- User Input;
- Read from an external file
- Program assignment e.g. a counter in a loop;
- As the result of a calculation within a program.
The variable name can usually be any combination of letters and numbers. Some words and letter combinations may not be used as variable names , these are normally called reserved words. Good programming style suggests that variable names are descriptive and reflect the data that is assigned to them e.g. pupil_name, car_reg, patient_number. Variables can represent a number of Data Types The two most common data types are Numeric (numbers) and String (text)
Most computer systems distinguish between different data types. This is important as it can help to make data storage more efficient and data processing more effective. The most common data types are:
- Numeric Integer - Whole numbers e.g. 24, 67
- Numeric Decimal (Real, Floating Point) - Decimal (Real) numbers e.g. 3.14, 6.58
- String - Alpha-numeric text e.g. 32 Acacia avenue; Exam syllabus code 45; etc.
- Character - A single character of Alpha-numeric data which takes up 1 byte of storage space
- Boolean - A variable which can only have one of two values e.g. true/false; 1/0, yes/no
In some programming languages e.g. Java , C#, Python the data type of each variable must be declared before it can be used during the program. For example in Java if you wanted to use an integer variable called age and a assign a value to it. You would have to use the commands
int age; age = 64;
These are called Strongly Typed languages - N.B. the "typed" refers to setting the data type not typing on a keyboard
Other languages such as Basic do not require you to set the data type of variables. These are called "Weakly Typed" languages