AQA Computer Science GCSE
Programming Concepts - Data and Variables
Computer programs using data. The data can be stored, changed, manipulated and transferred within the program.
To store data we need to use variables - named areas of computer memory in which data can be stored.
Variable names should be easy for other programmers to understand what they represent. Most programmers start their variable names with a lowercase letter and then use an uppercase letter for each new word - but with no spaces. There are really good reasons for this - and in particular for not using a capital letter at the start.
catName <- "Tiddles"
catAge <- 7
catFoodFave <- "Salmon chunks"
catDances <- True
These names are all meaningful and all use a standard naming structure.
Using meaningful variable names is important. Modern computing often involves teams of programmers working on the same project. If names don't make sense or conform to a style that everyone understands it is much harder for programmers to work together.
Code also has to be maintained - kept working, often over a period of years. It's likely that this will need to continue using new programmers, so again, using names that mean something helps massively.
Once you start to use subroutines it gets slightly harder to deal with variables. You can read more about local and global variables on the subroutines page
A specific type of variable you may come across is a constant. Constants are simply variables which cannot be changed - their value always stays the same after they are declared.
When constants are declared the standard way of naming them is to use CAPITALS for their names. This means that it's really easy for other programmers to know what the variable is a constant and not a variable. In pseudocode they also get the word "constant" put in front of them.
constant PI <- 3.1415927
constant GRAVITY <- 9.8
constant YEARGROUP <- 11
Using Python it makes no difference whether a variable is a constant or not - any variable in Python can be changed. In other languages the idea of a constant is much stronger and constants can not be changed.
Declaring and assigning values in Python works in a similar way.
SOME_CONSTANT = 42
It's usual to use UPPERCASE for constant names. This means that other programmers looking at your work will know that a variable is a constant straightaway.
Data Types:
Each piece of data stored needs to belong to one of five main data types:
- integer - whole number
- real number - decimal number
- character - a single keyboard character
- string - a sequence of characters which together make words, sentences or even whole books
- Boolean value - a special data type which can only have the value True or the value False
The data type of a variable limits what can be done with the data - you can't add an integer to a string, for example ("Doris" + 56 doesn't make any sense).
You can read more about strings, including how they work and some interesting things to do with them, on the String Handling page.