Information Technology Topic
Variables and Constants
- Read notes
- Flashcards
- Quiz
- Confident
Study focus - 3 things
- A variable is a named place in memory whose value may be changed while the program runs, and a constant is a value that never changes
- Store works out the calculation (the part before the word in) first using the old values, then replaces the old value in the named place: Store count + 1 in count adds one
- A named constant is changed in one place only, which is why fixed values such as a price or a fee are kept in constants
Section 1Values need a home
While a program runs it has to remember things: the price typed in, the running total, the number of customers so far. Each of these is kept in a storage area in memory, and the program refers to the area by a name such as price or total. The name stays the same, but what is inside can differ. That gives two kinds of named storage: variables and constants. Here's the trick: think of a labelled tin. The label is the name and the tin is the storage area. A variable is a tin whose contents you may swap. A constant is a tin sealed shut.
Section 2Variables: values that can change
A variable is a named place in memory whose value may be changed while the program runs. It holds one value at a time. Examples: the number of customers served so far, the running total of a till, the mark a pupil has just typed in. Each of these is different at different moments in the run. When a new value is stored in a variable, it replaces the old one. The old value is lost, unless you saved it somewhere else first.
Section 3Constants: values that never change
A constant is a value that never changes while the program runs. The number of days in a week is 7 and the number of minutes in an hour is 60, so both are natural constants. A fixed price or a fixed fee also works as a constant, because the program never changes it. If the shop changes its price next year, a person edits the one line that sets the constant, and the program still never changes it during a run. Easy to mix up: a constant is not 'a number'. It is a named value that the program never changes. A running total is a number, but it keeps changing, so it is a variable.
Section 4Store: how a variable changes
In pseudocode, Store puts a value into a named place: Store 5 in count. A new Store into the same name replaces the old value. The calculation (the part between Store and in) is worked out first, using the values the names hold at that moment, and then the answer is stored. So after Store 5 in count, the line Store count + 1 in count works out 5 + 1 = 6 and stores 6 in count. Watch out: Store count + 1 in count is an instruction, not an equation. As an equation it could never be true. As an instruction it simply adds one. In Pascal-style code the same idea is written count := count + 1;
Section 5Tracing the values after each line
To follow a program, keep a note of what each name holds after every line. Store 10 in total: total is 10. Store total + 5 in total: total is 15. Store total * 2 in total: total is 30. Only the line you are on matters, but it uses what the earlier lines left behind. Swapping two values needs a third place. Starting with a = 4 and b = 9: Store a in temp (temp is 4), Store b in a (a is 9), Store temp in b (b is 4). Without temp, Store a in b would overwrite 9 and both would end up holding 4.
Section 6Why keep fixed values in named constants?
Suppose a Mandeville hardware shop types 2400 for the price of a zinc sheet into three different lines of a program. When the price rises to 2600, all three lines must be found and edited, and missing one gives a wrong bill. If the program starts with Store 2400 in price and every later line uses the name price, the change is one edit. The name price also tells a reader what the number means. In Pascal-style code a constant is declared with const, for example const price = 2400; and the compiler refuses any later attempt to assign a new value to it.
Section 7Good names and giving a value first
Choose names that say what the storage holds: sheets, cost and bill are much clearer than x, y and z. A name must be given a value before it is used. A line such as Display total, when nothing has been stored in total, has nothing correct to show and will cause an error or a meaningless value. In Pascal-style code, variables are declared in a var section as name, colon, type, for example sheets : integer; and constants in a const section. Here's the trick: set the starting values at the top of the program, for example Store 0 in total, so every name has a value before it is used.
Examples
A program runs these lines: Store 10 in total Store total + 5 in total Store total * 2 in total Display total (a) State the value that total holds after each Store line. (b) State the value displayed. (c) Is total a variable or a constant? Give a reason.
(a) After Store 10 in total, total is 10. The second line works out 10 + 5 = 15 and replaces the 10, so total is 15. The third line works out 15 * 2 = 30 and replaces the 15, so total is 30. (b) Display total shows 30, the value left by the last line. (c) total is a variable, because its value changed from 10 to 15 to 30 during processing. A constant would have kept the same value from the first line to the last.
A tuck shop in Portmore sells patties at 250 dollars each. The program runs these lines: Store 250 in price Store 0 in sold Store 0 in takings Store sold + 4 in sold Store takings + 4 * price in takings Store sold + 3 in sold Store takings + 3 * price in takings Display sold Display takings (a) Trace the values of sold and takings after each line that changes them. (b) State the two values displayed. (c) Name the variables and the constant, and explain your choice. (d) The price rises to 280 dollars. State how many lines must be edited and calculate the new takings.
(a) After the first four setting lines: price is 250, sold is 0 and takings is 0. Then sold + 4 makes sold 4, and takings + 4 * 250 makes takings 0 + 1000 = 1000. Then sold + 3 makes sold 7, and takings + 3 * 250 makes takings 1000 + 750 = 1750. (b) The program displays 7 and then 1750. (c) sold and takings are variables, because their values change as sales are made. price is a constant, because it is set once as 250 and never changes while the program runs. (d) Only one line must be edited: Store 280 in price. The new takings are 4 * 280 = 1120 plus 3 * 280 = 840, which is 1960. Every other line already uses the name price, so none of them needs editing.
Now let's water it
Session runs notes to flashcards to quiz back-to-back.
Where this comes from
- CSEC Information Technology Syllabus (CXC 30/G/SYLL 08, revised 2000, 2008, 2013) · Caribbean Examinations Council (CXC) · Section 2, Specific Objective 3 · Syllabus scope
- The Python Tutorial (sections 3, 4, 8) and The Python Standard Library, Built-in Types (version 3.15 pages) · Python Software Foundation · Chapter 3, An Informal Introduction to Python, using Python as a calculator (language-neutral: a value assigned to a name can be used later, and a name with no value is an error), checked against (https://docs.python.org/3/tutorial/introduction.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 2, Ordinary constants (declared with const; assigning to one is a compile error), checked against (https://www.freepascal.org/docs-html/ref/refse9.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 13, Assignments (the right-hand expression is evaluated and stored in the variable, replacing the old value), checked against (https://www.freepascal.org/docs-html/ref/refsu52.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 4, Variable declaration (name, colon, type), checked against (https://www.freepascal.org/docs-html/ref/refse22.html) · Facts