Standard methods
Four methods the exam keeps asking for
- Many Paper 2 answers are built from the same few standard methods. You met totalling, counting and a simple search in the last lessons.
- This lesson adds four more: a rogue value that ends the input, an average that never divides by zero, the highest and lowest in one loop, and a search that stops at the first match.
- Learn the shape of each one. In the exam you fit the shape to the question's names.
A rogue value ends the input
- Sometimes you do not know how many values will be typed. A rogue value (here
-1) is typed to say "no more". It is not data, so it is never added. - Read the first value before the loop. Inside the loop, use the value, then read the next one at the end of the body.
WHILEtests before each pass, so a rogue value typed first means the loop runs zero times.

DECLARE Typed : ARRAY[1:5] OF INTEGER
DECLARE Position, Mark, Total, Count : INTEGER
DECLARE Average : REAL
Typed[1] ← 55
Typed[2] ← 70
Typed[3] ← 48
Typed[4] ← -1
Typed[5] ← 99
Total ← 0
Count ← 0
Position ← 1
Mark ← Typed[Position]
WHILE Mark <> -1 DO
Total ← Total + Mark
Count ← Count + 1
Position ← Position + 1
Mark ← Typed[Position]
ENDWHILE
IF Count > 0
THEN
Average ← ROUND(Total / Count, 1)
OUTPUT "Average: ", Average
ELSE
OUTPUT "No marks entered"
ENDIF
- The array
Typedstands in for the keyboard. In an exam answer, writeINPUT Markwhere the program readsTyped[Position]. IF Count > 0guards the division: if the first value were-1,Total / Countwould divide by zero.ROUND(Total / Count, 1)gives one decimal place:57.7.
Highest and lowest in one loop
- Start both
HighestandLowestat the first value, then compare every other value with each of them. - Never start
Highestat0orLowestat999: real data could be negative, or bigger than your guess. - Use two separate
IFstatements. One value can change only one of them, but both tests must run.

DECLARE Temps : ARRAY[1:6] OF INTEGER
DECLARE Day, Highest, Lowest : INTEGER
Temps[1] ← 18
Temps[2] ← 23
Temps[3] ← 15
Temps[4] ← 27
Temps[5] ← 21
Temps[6] ← 12
Highest ← Temps[1]
Lowest ← Temps[1]
FOR Day ← 2 TO 6
IF Temps[Day] > Highest
THEN
Highest ← Temps[Day]
ENDIF
IF Temps[Day] < Lowest
THEN
Lowest ← Temps[Day]
ENDIF
NEXT Day
OUTPUT "Highest: ", Highest, " Lowest: ", Lowest
A search that stops early
- A
FORloop looks at every item. AWHILEloop with a flag can stop at the first match. - The test has two parts:
Index <= 5(still inside the array) andFound = FALSE(not found yet). - Store the position when you find the item, so the answer can say where it is.

DECLARE Names : ARRAY[1:5] OF STRING
DECLARE Index, FoundAt : INTEGER
DECLARE Found : BOOLEAN
DECLARE Target : STRING
Names[1] ← "Ola"
Names[2] ← "Ben"
Names[3] ← "Mei"
Names[4] ← "Raj"
Names[5] ← "Ava"
Target ← "Mei"
Found ← FALSE
Index ← 1
WHILE Index <= 5 AND Found = FALSE DO
IF Names[Index] = Target
THEN
Found ← TRUE
FoundAt ← Index
ELSE
Index ← Index + 1
ENDIF
ENDWHILE
IF Found
THEN
OUTPUT Target, " found at position ", FoundAt
ELSE
OUTPUT Target, " not found"
ENDIF
Indexchanges only when the item does not match, so it is not moved past the match.
Common mistakes
| Wrong | Right | Why |
|---|---|---|
adding the rogue value to Total |
read the next value at the end of the loop body | the loop test then stops before -1 is used |
Average ← Total / Count with no check |
IF Count > 0 first |
a list with no values would divide by zero |
Highest ← 0 |
Highest ← Temps[1] |
all the values could be negative |
ELSE between the highest and lowest tests |
two separate IF statements |
the first value checked must be tested for both |
WHILE Names[Index] <> Target DO |
test Index <= 5 as well |
a missing name would read past the end of the array |
Now you try
- Name the method first (rogue value, average, highest/lowest, search), then write its shape.
Independent transfer
The final task reads numbers until a rogue value. Plan the counts and the guard, then solve it before opening support. Check what happens when the rogue value comes first.
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Typed stands in for the keyboard: 9, 6, 10, then the rogue value -1. Add and count the values before -1, then output Average: and the average rounded to 1 decimal place.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Scores holds 40, 75, 62, 18. In một loop, find the highest and the lowest. Output the highest, then the lowest, on separate lines.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Codes holds A1, B4, C2, C7, D3, C7. Search for "C7" and stop at the đầu tiên match. Output its position, or 0 if it is not there.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 4/5 · 280 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Read whole numbers until the rogue value -1. If no numbers came before -1, output No numbers. Otherwise output Count: and how many there were, then Highest: and the largest. The numbers can be negative.
Nhấn Chạy để xem kết quả ở đây.