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.
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.
Quick questions on Programming fundamentals
- What is 17 MOD 5?
- 2: 17 = 3 × 5 + 2, so the remainder is 2.
- What is 17 DIV 5?
- 3: 5 goes into 17 three whole times.
- What is a variable?
- A named memory location whose value can change: For example, score or total.
- Why is VAT_RATE a good choice for a constant?
- Its value stays the same while the program runs: If the rate changes, the programmer only updates it in one place.
- Which type of loop is a FOR loop?
- Count-controlled: It repeats a set number of times.
- Which loop always runs at least once?
- REPEAT … UNTIL: It checks its condition at the end.
- Which construct is used to choose between paths?
- Selection: For example, IF … ELSE.
- What does concatenation mean?
- Joining strings together: "Hi " joined with "there" gives "Hi there".
- What is the length of the string "Hello"?
- 5: It has 5 characters.
- What is the result of NOT (5 > 3)?
- False: 5 > 3 is True, and NOT True is False.
- total starts at 0. A loop adds i to total for i = 1, 2 and 3. What is total at the end?
- 6: 0 + 1 + 2 + 3 = 6.
- This loop adds up numbers. What does it print?
- 6: range(1, 4) gives 1, 2 and 3: it stops before 4. So total is 1 + 2 + 3 = 6.
- What does this WHILE loop print?
- 12: count goes 3, 6, 9, 12. When it is 9 the condition is still true, so it adds 3 once more. At 12 the loop stops.
- How many times does this program print Hello?
- 6: i takes the values 2, 3, 4, 5, 6 and 7 (range stops before 8): that is 6 values.
- What does this program print using // and %?
- 3 2: // is integer division (DIV): 5 goes into 17 three whole times. % is the remainder (MOD): 17 − 15 = 2.
- What does this string slice print?
- Com: A slice [0:3] starts at index 0 and stops before index 3, so it takes characters 0, 1 and 2: C, o and m.
- What does this program print using len() and upper()?
- 6 PYTHON: len counts the characters (6), and upper() changes every letter to a capital.
- One variable is copied into another. What is printed?
- 3: y = x copies the value 3 into y. Changing x afterwards does not change y.
- This loop doubles n. What does it print?
- 16: The loop runs 4 times (i = 0, 1, 2, 3), doubling n each time: 2, 4, 8, 16.
- What does this program print using OR?
- Allowed: age >= 16 is False, but member is True. With OR, only one side needs to be True.
- How many stars does this program print?
- 6: The inner loop runs 2 times for each of the 3 runs of the outer loop: 3 × 2 = 6.
- Two strings are added together. What is printed?
- 53: a and b are strings, so + joins them (concatenation). To add them as numbers you would cast them: int(a) + int(b).
- Which grade does this program print?
- B: 72 is not ≥ 80, but it is ≥ 70, so grade is B. Once one branch of an IF … ELIF runs, the rest are skipped.
- What does the program print after these three lines?
- 12: Line by line: total is 10, then 10 − 4 = 6, then 6 × 2 = 12.
Programming fundamentals: exam-style questions and mark schemes
What is 23 MOD 4? [1 mark]
Answer: 3
Explain the difference between a variable and a constant. [2 marks]
- A variable's value can change while the program runs (1)
- A constant's value stays the same while the program runs (1)
Explain the difference between a WHILE loop and a REPEAT … UNTIL loop. [2 marks]
- A WHILE loop checks its condition at the start, so it may not run at all (1)
- A REPEAT … UNTIL loop checks its condition at the end, so it always runs at least once (1)
Write an algorithm that asks the user for 5 numbers and outputs their total. [4 marks]
- Sets a total to 0 before the loop (1)
- Uses a count-controlled loop that repeats 5 times (1)
- Inputs a number and adds it to the total inside the loop (1)
- Outputs the total after the loop (1)
State the output of this program. [2 marks]
- Outputs 2 and 4 (1)
- Outputs nothing else, in that order (1)
Write a program that asks the user to enter a whole number, then outputs "Even" if the number is even and "Odd" if it is odd. [4 marks]
- Takes input from the user and converts it to an integer (1)
- Finds the remainder after dividing by 2, using MOD or % (1)
- Uses selection (IF … ELSE) on the remainder (1)
- Outputs "Even" when the remainder is 0 and "Odd" otherwise (1)
Describe the three programming constructs, with an example of each from a program that asks for test scores until the user enters -1, then outputs the average score. [6 marks]
- sequence: instructions are carried out in order, one after another
- for example, setting total to 0 and count to 0 before the loop
- selection: a decision chooses which code runs, using IF
- for example, only adding the score to the total if it isn't -1
- iteration: code is repeated in a loop
- a condition-controlled loop (WHILE) suits this, because the number of scores isn't known
- a count-controlled loop (FOR) is used when the number of repeats is known
- average = total ÷ count, checking count isn't 0 first
How to answer them: exam technique and command words.
Programming fundamentals: questions and answers
- What is the length of the string "Hello"?
- It has 5 characters.
- What is the result of NOT (5 > 3)?
- 5 > 3 is True, and NOT True is False.
- total starts at 0. A loop adds i to total for i = 1, 2 and 3. What is total at the end?
- 0 + 1 + 2 + 3 = 6.