Programming Fundamentals
Data types, variables and constants, every operator AQA can ask about, and the three programming constructs that between them can express any algorithm ever written.
Data types
A data type tells the computer what kind of value a variable holds, which decides how many bits it uses, what operations are legal on it and how the bits are interpreted.
AQA names five data types. Learn the names exactly.
| Data type | Holds | Example |
|---|---|---|
| Integer | A whole number, positive or negative | -40, 0, 2025 |
| Real | A number with a fractional part | 3.14, -0.5, 2.0 |
| Boolean | One of exactly two values | True, False |
| Character | A single character | 'K', '7', ' ' |
| String | A sequence of characters | "Hello", "7", "" |
The integer 7 can be multiplied. The string "7" is a single character that happens to look like a digit, and multiplying it either fails or repeats it. Nearly every "why does my program crash" question in an exam turns on this.
Why the type matters to the machine
The bit pattern 01000001 is the integer 65, the character A, or part of a real number, depending entirely on what type the program says it is. The bits alone mean nothing. The type is what gives them meaning.
That is also why type errors are so common: the computer will happily do something with a value, it just may not be the thing you meant.
- Five data types: integer, real, Boolean, character and string.
- A phone number is a string: you never add them, and a number would lose the leading 0.
- Casting changes a value from one type to another: int(), str(), float().
- int() turns text into a whole number.
- int() on a float cuts off the decimals. It does not round.
- str() turns a number into text.
- float() turns text into a decimal number.
- input() always returns text, so convert it before doing sums.
- A conversion that does not make sense raises an error, so validate first.
Variables and constants
A variable is a named location in memory whose value can change while the program runs. A constant is a named value that is fixed when the program is written and cannot change while it runs.
CONSTANT vat ← 0.2
price ← 40
total ← price + (price * vat)
OUTPUT totalWhy constants exist at all
You could simply write 0.2 everywhere it is needed. Three reasons not to:
- Readability.
price * vatsays what is happening.price * 0.2does not. - One place to change. If the rate changes, you edit one line rather than hunting through a program for every
0.2, and you cannot miss one. - Safety. The compiler or interpreter will stop you accidentally assigning to a constant, which catches a whole class of bug before the program ever runs.
The test is not "does it change" but "should the program be allowed to change it". A VAT rate, the number of players in a game, the maximum length of a password: all constants.
Assignment
The ← symbol in AQA pseudo-code means assignment: work out the value on the right, then store it in the name on the left. It does not mean equality.
count ← count + 1As mathematics that is nonsense. As assignment it is perfectly sensible: take the current value of count, add one, put the answer back into count.
- = stores a value in a variable: the name on the left, the value on the right.
- A variable is a labelled box with a name, a value and a data type.
- The value can change. age = age + 1 takes the 15 out and puts 16 in.
- A constant holds a value that must never change. By convention it is written in capitals.
- Names use letters, digits and underscores. No spaces, and no digit first.
Operators
AQA divides operators into three groups, and the specification names all of them.
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Add | 7 + 2 | 9 |
- | Subtract | 7 - 2 | 5 |
* | Multiply | 7 * 2 | 14 |
/ | Real division | 7 / 2 | 3.5 |
DIV | Integer division, the whole part only | 7 DIV 2 | 3 |
MOD | Modulus, the remainder | 7 MOD 2 | 1 |
DIV and MOD are examined constantly, because together they let you take a number apart. seconds DIV 60 gives whole minutes and seconds MOD 60 gives the seconds left over. n MOD 2 = 0 tests whether n is even. n MOD 10 gives the last digit.
Relational operators
= equal to, ≠ not equal to, < less than, > greater than, ≤ less than or equal to, ≥ greater than or equal to.
Every one of these produces a Boolean. That is the whole point of them: they turn a comparison into something selection and iteration can test.
Boolean operators
AND is true only when both sides are true. OR is true when at least one side is true. NOT reverses.
IF age ≥ 13 AND age ≤ 19 THEN
OUTPUT 'teenager'
ENDIFIF grade = 'A' OR 'B' does not work. The right hand side of the OR must be a complete comparison, so it has to be IF grade = 'A' OR grade = 'B'. Examiners set this deliberately.
- Arithmetic: + - * / then DIV, MOD and power.
- DIV is how many whole times it goes in. MOD is what is left over.
- Comparisons ask a question, and the answer is always True or False.
- = stores a value. == asks whether two values are equal.
The three constructs
Every program ever written is built from three things.
Sequence
Statements run in the order they are written, one after another. Order matters: you cannot use a variable before you have given it a value.
Selection
The program chooses between paths based on a condition.
IF mark ≥ 70 THEN
grade ← 'Distinction'
ELSE IF mark ≥ 50 THEN
grade ← 'Merit'
ELSE IF mark ≥ 40 THEN
grade ← 'Pass'
ELSE
grade ← 'Fail'
ENDIFThe order of those conditions is doing real work. A mark of 80 satisfies all three tests, and it gets Distinction only because that test comes first. Reverse the order and every passing student gets a Pass.
Iteration
The program repeats statements. AQA distinguishes two kinds, and the distinction is examined.
Count controlled iteration repeats a number of times known before the loop starts.
FOR i ← 1 TO 5
OUTPUT i * i
ENDFORCondition controlled iteration repeats while a condition holds, and nothing decides in advance how many times that will be.
REPEAT
password ← USERINPUT
UNTIL password = storedA WHILE loop tests the condition before the body, so it may run zero times. A REPEAT UNTIL loop tests afterwards, so it always runs at least once. That difference is worth a mark on its own.
"The number of items in the list is known, so a count controlled FOR loop is appropriate" earns the mark. "I would use a for loop" does not.
- Sequence: instructions run in order, each one once, from the top down.
- Selection: a condition is tested and only one branch of the program runs.
- Iteration: a block is repeated, a set number of times or until a condition changes.
Key terms you must be able to define
- Variable
- A named memory location whose value can change while the program is running.
- Constant
- A named value fixed when the program is written, which cannot be changed while it runs.
- Assignment
- Working out the value of an expression and storing the result in a named location.
- Data type
- A classification that tells the computer how to interpret and store a value.
- Integer
- A whole number, with no fractional part.
- Real
- A number that may have a fractional part.
- Boolean
- A data type with exactly two possible values, True and False.
- DIV
- Integer division, giving only the whole number part of the result.
- MOD
- The modulus operator, giving the remainder after an integer division.
- Sequence
- Statements carried out one after another in the order written.
- Selection
- Choosing between different paths through a program based on a condition.
- Iteration
- Repeating a section of a program, either a set number of times or while a condition holds.
- Count controlled iteration
- A loop that repeats a number of times known before it starts.
- Condition controlled iteration
- A loop that repeats while a condition is true, for an unknown number of repeats.
Flashcards
Say the definition out loud before you flip the card. Retrieving it from memory is what builds recall, reading it again is not.
Reach the top grade on this topic
Top answers on this topic are precise in three ways.
They name the construct and justify it. Not "use a loop" but "use a condition controlled loop, because the number of attempts the user needs is not known in advance".
They talk about types as constraints, not labels. A grade 9 answer says that casting the input to an integer is necessary because USERINPUT returns a string and a string cannot be compared numerically. A grade 5 answer says "you need to change it to a number".
They read code before judging it. Most "explain why this program does not work" questions have their answer in the order of the lines or the direction of a comparison. Trace two or three values through by hand before writing anything.
Mistakes that cost marks here
- Writing IF x = 1 OR 2. Each side of a Boolean operator must be a complete comparison.
- Confusing DIV and MOD. DIV gives the whole number of times it goes in, MOD gives what is left over.
- Treating = as equality in an assignment. In pseudo-code AQA uses an arrow precisely to stop this confusion.
- Saying a WHILE loop always runs at least once. It is REPEAT UNTIL that guarantees one pass.
- Calling something a constant because it does not happen to change. A constant is declared as one and cannot be changed.
The three construct rule is not a teaching simplification. In 1966 Bohm and Jacopini proved mathematically that sequence, selection and iteration are enough to express every computable function. Nothing else is strictly necessary.
Knowledge check
Exam-style questions
Five written questions in the style of the real paper. Write a full answer, then let the marker check it against the mark scheme.
- LA variable can change value while the program is running
- LA constant is fixed when the program is written and cannot change while it runs
A variable is a named memory location whose value can be changed while the program is running. A constant is a named value that is set when the program is written and cannot be changed while the program runs.
- LUses DIV for the minutes, for example 500 DIV 60
- LGives 8 minutes
- LUses MOD for the seconds, for example 500 MOD 60
- LGives 20 seconds
The number of whole minutes is 500 DIV 60, which is 8, because sixty goes into five hundred eight complete times. The seconds left over are 500 MOD 60, which is 20, because eight lots of sixty is four hundred and eighty and five hundred minus four hundred and eighty is twenty.
- LCount controlled iteration repeats a number of times known before the loop starts
- LCondition controlled iteration repeats while a condition is true, for an unknown number of repeats
- LGives a sensible count controlled example, such as processing every item in a list of known length
- LGives a sensible condition controlled example, such as repeating until valid input is entered
Count controlled iteration repeats a fixed number of times that is known before the loop begins, so a FOR loop is used. Working through all forty names in a list is a count controlled task, because the length of the list is known. Condition controlled iteration repeats for as long as a condition remains true, and the number of repeats is not known in advance. Asking a user to re-enter a password until it matches is condition controlled, because there is no way to know how many attempts they will need.
- LIt is true when the temperature is above 30
- LIt is also true when the temperature is below 10
- LIt is false for any value from 10 to 30 inclusive, for example 20
The condition uses OR, so it is true when at least one of the two comparisons is true. It is therefore true when the temperature is greater than 30 and true when the temperature is less than 10, which means it detects any temperature outside the comfortable range. It is false for any value from 10 to 30 inclusive, so a temperature of 20 would make it false.
- LUSERINPUT or the equivalent returns a string
- LThe program is comparing a string with an integer, which does not behave as expected
- LThe input must be cast or converted to an integer
- LThen the numeric comparison works correctly
The most likely cause is that input is returned as a string, so the variable holds the two characters '1' and '8' rather than the number eighteen. Comparing a string with an integer either fails outright or compares character by character, so the test does not behave as intended and never succeeds. The correction is to cast the input to an integer as soon as it is read, for example age ← STRING_TO_INT(USERINPUT), after which the comparison age ≥ 18 works on two numbers and gives the right answer.