Variables and constants
- A variable is a named location in memory that stores a value, which can change while the program runs. A constant stores a value that does not change, such as VAT = 0.2.
- Assignment gives a variable a value. AQA pseudocode writes score ← 0; OCR's Exam Reference Language and Python write score = 0.
The three programming constructs
- Sequence: instructions run one after another. Selection: IF … ELSE (or CASE / SWITCH) chooses a path. Iteration: loops repeat code.
- Count-controlled (definite) iteration: a FOR loop repeats a set number of times. Condition-controlled (indefinite) iteration: WHILE and REPEAT … UNTIL loops repeat until a condition changes. (OCR writes DO … UNTIL; Python only has WHILE.)
- A WHILE loop checks its condition at the start, so it might not run at all. A REPEAT … UNTIL loop checks at the end, so it always runs at least once.
- Loops and IF statements can be nested inside each other.
Operators
- Arithmetic: + − * / and also integer division (DIV) and remainder (MOD). 17 DIV 5 = 3 and 17 MOD 5 = 2. In Python these are // and %.
- Comparison: = (== in Python), ≠ (!= in Python), <, >, ≤ (<=) and ≥ (>=). Boolean operators: AND, OR and NOT.
- Powers: OCR writes 2 ^ 3 and Python writes 2 ** 3. Both mean 2³ = 8.
Input, output and strings
- Programs take input from the user and display output. Input from the keyboard arrives as a string.
- String handling includes finding the length, taking a substring, concatenation (joining strings) and changing the case. "Hello" has length 5.
- Programs can also generate random numbers, such as a random integer from 1 to 6 for a dice.
File handling (OCR and Edexcel)
- Programs can save data in a text file, so it is kept after the program closes.
- The steps are: open the file (to read, write or append), read or write the data, then close the file.
- Writing replaces what is already in the file; appending adds to the end of it.
- In Python, open("scores.txt", "a") opens a file for appending, and file.close() closes it.
Key terms
- Variable
- A named memory location that stores a value that can change.
- Constant
- A named value that does not change while the program runs.
- Assignment
- Giving a variable a value.
- Count-controlled loop
- A loop that repeats a set number of times, such as FOR.
- Condition-controlled loop
- A loop that repeats until a condition changes, such as WHILE.
- DIV
- Integer division: how many whole times one number goes into another.
- MOD
- The remainder after integer division.
- Concatenation
- Joining two or more strings together.