🎯 Syllabus & Goals 3 min
Cambridge 8.1.4 · Totalling and counting 8.1.4 · String handling Paper 2 · Algorithms, Programming and Logic
By the end of this lesson you can:
- Write totalling and counting statements inside a loop, in pseudocode and Python.
- Use
LENGTH,SUBSTRING,UCASEandLCASE— and the Python equivalents — on a string. - Explain why pseudocode strings start at position 1 but Python strings start at index 0.
Textbook: Chapter 8, §8.1.4(d)–(e) (pp. 314–317)
Recap / Warm-Up 5 min
Last lesson: three loops — FOR … NEXT, WHILE … DO … ENDWHILE and REPEAT … UNTIL. Loops are where totalling and counting happen.
Quick starter
Before a loop adds numbers into Total, what value must Total be given — and why?
Reveal the answer
Total ← 0. It must be initialised to zero, or the first addition uses an unknown or old value.
🧠 Key Concept 14 min
1 · Totalling and counting
Total ← Total + Weight Count ← Count + 1
total = total + weight count = count + 1 count += 1 # Python shorthand, same effect
IF.2 · String handling
A string is a sequence of characters, from an empty string ("") up to a maximum set by the language. Each character has a position number. You must know four string operations:
| Operation | Pseudocode | Python | Result for "Space Station" |
|---|---|---|---|
| Length | LENGTH(Place) | len(place) | 13 (the space counts) |
| Substring | SUBSTRING(Place, 7, 7) | place[6:13] | "Station" |
| Upper case | UCASE(Place) | place.upper() | "SPACE STATION" |
| Lower case | LCASE(Place) | place.lower() | "space station" |
[start:end] stops before end.Worked Example 12 min
Example 1 · Totalling and two kinds of counting
A parcel depot inputs parcel weights until -1 is entered. Output the number of parcels, their total weight and how many weigh more than 10 kg. Test data: 4.5, 12, 7.5, -1.
- Initialise the total and both counters to 0 before the loop.they must start from nothing.
- Input the first weight before a
WHILEloop, and the next one at the end of the body.the-1is tested before it can be added or counted. - Every parcel is counted; only heavy parcels are counted inside an
IF.a conditional count.
Cambridge pseudocode
CONSTANT Heavy ← 10
DECLARE Weight, TotalWeight : REAL
DECLARE ParcelCount, HeavyCount : INTEGER
TotalWeight ← 0
ParcelCount ← 0
HeavyCount ← 0
OUTPUT "Parcel weight in kg (-1 to stop): "
INPUT Weight
WHILE Weight <> -1 DO
TotalWeight ← TotalWeight + Weight
ParcelCount ← ParcelCount + 1
IF Weight > Heavy
THEN
HeavyCount ← HeavyCount + 1
ENDIF
OUTPUT "Parcel weight in kg (-1 to stop): "
INPUT Weight
ENDWHILE
OUTPUT "Parcels: ", ParcelCount
OUTPUT "Total weight: ", TotalWeight, " kg"
OUTPUT "Heavy parcels: ", HeavyCountThe same in Python
# Parcel depot: total weight, number of parcels, number of heavy parcels HEAVY = 10 total_weight = 0 parcel_count = 0 heavy_count = 0 weight = float(input("Parcel weight in kg (-1 to stop): ")) while weight != -1: total_weight = total_weight + weight # totalling parcel_count = parcel_count + 1 # counting every parcel if weight > HEAVY: heavy_count += 1 # counting only some weight = float(input("Parcel weight in kg (-1 to stop): ")) print("Parcels:", parcel_count) print("Total weight:", total_weight, "kg") print("Heavy parcels:", heavy_count)
Output
Parcel weight in kg (-1 to stop): 4.5 Parcel weight in kg (-1 to stop): 12 Parcel weight in kg (-1 to stop): 7.5 Parcel weight in kg (-1 to stop): -1 Parcels: 3 Total weight: 24.0 kg Heavy parcels: 1
| Weight | TotalWeight | ParcelCount | HeavyCount | OUTPUT |
|---|---|---|---|---|
| 0 | 0 | 0 | ||
| 4.5 | 4.5 | 1 | ||
| 12 | 16.5 | 2 | 1 | |
| 7.5 | 24.0 | 3 | ||
| -1 | Parcels: 3 · Total weight: 24.0 kg · Heavy parcels: 1 |
Example 2 · String handling — usernames and letter counts
(a) A school makes a username from the first three letters of a student's first name and family name, in capitals.
DECLARE FirstName, FamilyName, UserName : STRING OUTPUT "First name: " INPUT FirstName OUTPUT "Family name: " INPUT FamilyName UserName ← SUBSTRING(FirstName, 1, 3) & SUBSTRING(FamilyName, 1, 3) UserName ← UCASE(UserName) OUTPUT "Your username is ", UserName OUTPUT "It has ", LENGTH(UserName), " characters"
# Make a username from the first 3 letters of each name first_name = input("First name: ") family_name = input("Family name: ") username = first_name[0:3] + family_name[0:3] username = username.upper() print("Your username is", username) print("It has", len(username), "characters")
First name: Alex Family name: Morgan Your username is ALEMOR It has 6 characters
SUBSTRING("Alex", 1, 3)→"Ale";SUBSTRING("Morgan", 1, 3)→"Mor".start at position 1 and take 3 characters.&joins them:"AleMor".&is concatenation in pseudocode; Python uses+on strings.UCASEgives"ALEMOR", andLENGTHis 6.the result is assigned back, so UserName really changes.
(b) Count how many times the letter "a" appears in a sentence, ignoring capitals. This combines a loop, LCASE, SUBSTRING and counting.
DECLARE Sentence : STRING
DECLARE Position, CountA : INTEGER
OUTPUT "Enter a sentence: "
INPUT Sentence
Sentence ← LCASE(Sentence)
CountA ← 0
FOR Position ← 1 TO LENGTH(Sentence)
IF SUBSTRING(Sentence, Position, 1) = "a"
THEN
CountA ← CountA + 1
ENDIF
NEXT Position
OUTPUT "The letter a appears ", CountA, " times"# Count how many times the letter a appears in a sentence sentence = input("Enter a sentence: ") sentence = sentence.lower() # so "A" and "a" both count count_a = 0 for position in range(0, len(sentence)): if sentence[position] == "a": count_a = count_a + 1 print("The letter a appears", count_a, "times")
Enter a sentence: A banana The letter a appears 4 times
Notice the loop ranges: pseudocode goes from 1 TO LENGTH(Sentence); Python goes from 0 up to (not including) len(sentence). Both visit every character exactly once.
Try It Yourself 12 min
Goal: Input your full name. Output its length, the first three characters, and the name in upper case and in lower case.
Goal: Input the weight of sacks of flour until -1 is entered. Output the number of sacks and the total weight. Trace your program with 25, 25, 20, -1.
Goal: Input a word and output it backwards (e.g. "robot" → "tobor"), then say whether it is a palindrome.
Hint
Start with an empty string Reversed ← "". Loop Position from 1 to the length and build Reversed ← SUBSTRING(Word, Position, 1) & Reversed — each new letter goes on the front. Compare LCASE versions of both.
📝 Exam Practice 10 min
Word ← "Keyboard". State the value returned by: (a) LENGTH(Word) (b) SUBSTRING(Word, 4, 5) (c) LCASE(Word)
Mark scheme
- (a) 8 (1)
- (b) "board" (1)
- (c) "keyboard" (1)
Describe totalling and counting, giving a pseudocode statement for each.
Mark scheme
- Totalling keeps a running total that values are added to (1).
- e.g.
Total ← Total + Price(1). - Counting keeps track of how many times something happens / adds one each time (1).
- e.g.
Count ← Count + 1(1).
Write a pseudocode algorithm to input a password, check that it has exactly 8 characters and that all its letters are upper case, and output "Password meets the rules" if both conditions are true, otherwise "Password rejected".
Mark scheme
- Prompt and input of the password (1).
- Use of
LENGTH(Password)(1)… - …compared with
= 8(1). - Use of
UCASE(Password) = Passwordto check all letters are upper case (1). - Both conditions combined with
ANDin one IF / nested IFs (1). - Both correct outputs in the correct places (1).
🗝️ Recap & Key Terms 3 min
Initialise, then add: totalling adds the value, counting adds one. For strings, learn LENGTH, SUBSTRING(s, start, length), UCASE and LCASE — and remember pseudocode starts at position 1, Python at index 0.
- Totalling
- Keeping a total that values are added to.
- Counting
- Keeping track of the number of times an action is performed.
- Length
- The number of characters in a string, including spaces.
- Substring
- Part of a string, extracted from a start position for a given number of characters.
- Upper / lower
- Converting all the letters in a string to upper case / lower case.
- Concatenation
- Joining two strings end to end (
&in pseudocode,+in Python).
Homework 1 min
Task (≤ 15 min): Write pseudocode that inputs 10 words using a FOR loop and outputs how many of them are longer than 6 characters, and the total number of characters in all 10 words. [5]
Model answer
DECLARE Word : STRING
DECLARE Index, LongCount, TotalChars : INTEGER
LongCount ← 0
TotalChars ← 0
FOR Index ← 1 TO 10
OUTPUT "Enter a word: "
INPUT Word
TotalChars ← TotalChars + LENGTH(Word)
IF LENGTH(Word) > 6
THEN
LongCount ← LongCount + 1
ENDIF
NEXT Index
OUTPUT "Words longer than 6 letters: ", LongCount
OUTPUT "Total characters: ", TotalCharsMarks: both variables initialised (1); FOR loop 1 to 10 with input (1); totalling with LENGTH (1); conditional count (1); both outputs after the loop (1).