A programmer writes 'score = 0' at the start of a game's code, then later writes 'score = score + 10' every time points are earned. What does this show about what a 'variable' is in programming?
- A variable is a fixed value that, once set in the code, can never be changed while the program runs
- A variable is a named container that stores a value which can be read and changed while the program runs, letting 'score' hold different numbers at different points in the game
- A variable is a comment left in the code to explain what a line does, which the computer ignores when running the program
- A variable is a fixed instruction telling the computer to display a value on the screen
Why B? And why not the others?
Correct answer: B. A variable is a named container that stores a value which can be read and changed while the program runs, letting 'score' hold different numbers at different points in the game
A variable is a named storage location whose value can be read and updated while a program runs; 'score = 0' creates the variable and gives it a starting value, and 'score = score + 10' later reads the current value, adds ten to it, and stores the updated result back under the same name, which is only possible because a variable's contents are allowed to change over time. The option describing a value that can never change once set actually describes a constant, the opposite of what this example shows, since 'score' clearly changes as points are earned. The option describing a comment is wrong because comments are notes for human readers that the computer skips entirely when running the code, and they never store or affect any value like a score. The option describing an instruction to display a value is wrong because that describes an output or print statement, which shows a value on screen but does not itself store or update any data the way a variable does.
Source: BBC Bitesize GCSE Computer Science: Programming fundamentals - variables