16.1 What is Highest & Lowest?
Highest & Lowest is the algorithm of finding the maximum (highest) and minimum (lowest) values in a set of data. A comparison variable tracks the extreme value seen so far while looping through the data.
- Comparison variable — INTEGER (or REAL), initialised to the FIRST element.
- Highest — updated with
IF Value > Highest THEN Highest <- Value. - Lowest — updated with
IF Value < Lowest THEN Lowest <- Value. - Common uses — highest score, lowest temperature, maximum price, minimum time.
Key idea: totalling adds every value to an accumulator; counting matches a criterion. Highest & Lowest is different — it keeps only the most extreme value seen so far, replacing it whenever a new extreme appears.
Common mistake: initialising the comparison variable to 0. If all values are negative, 0 would be wrongly reported as the highest; if all are positive, 0 would wrongly be the lowest. Always seed with the FIRST element.
Concepts — What is Highest & Lowest?
16.2 Finding the Highest Value
The highest-value pattern has four steps: initialise Highest to the first element, loop through the remaining elements, compare each element to Highest using > and update if larger, and output the final Highest after the loop.
// Find the highest of 5 numbers entered by the user
DECLARE Numbers : ARRAY[1:5] OF INTEGER
DECLARE I : INTEGER
DECLARE Highest : INTEGER
FOR I <- 1 TO 5
OUTPUT "Enter number: "
INPUT Numbers[I]
NEXT I
Highest <- Numbers[1]
FOR I <- 2 TO 5
IF Numbers[I] > Highest THEN
Highest <- Numbers[I]
ENDIF
NEXT I
OUTPUT "Highest: ", Highest- Seed before loop —
Highest <- Numbers[1], never 0. - Loop from 2 — start at
FOR I <- 2 TO Nbecause index 1 is already in Highest. - Greater-than comparison — update only when
Numbers[I] > Highest. - OUTPUT after loop — print the final highest value, not the running maximum.
Exam tip: if a question says "find the largest", "find the maximum", or "find the highest", that is this algorithm. Look for the keyword "largest" / "maximum" / "highest".
Finding the Highest Value — Step by Step
16.3 Finding the Lowest Value
The lowest-value pattern mirrors the highest pattern — only the comparison operator changes. Initialise Lowest to the first element, loop through the rest, compare each using <, and update if smaller.
// Find the lowest of 5 numbers entered by the user
DECLARE Numbers : ARRAY[1:5] OF INTEGER
DECLARE I : INTEGER
DECLARE Lowest : INTEGER
FOR I <- 1 TO 5
OUTPUT "Enter number: "
INPUT Numbers[I]
NEXT I
Lowest <- Numbers[1]
FOR I <- 2 TO 5
IF Numbers[I] < Lowest THEN
Lowest <- Numbers[I]
ENDIF
NEXT I
OUTPUT "Lowest: ", Lowest- Seed before loop —
Lowest <- Numbers[1], never 0. - Less-than comparison — update only when
Numbers[I] < Lowest. - OUTPUT after loop — print the final lowest value after the loop ends.
- Same skeleton as highest — only the operator (< vs >) changes.
Watch out: initialising Lowest to 0 when all values are positive. Lowest stays 0 forever (no positive value is smaller than 0), giving the wrong answer. Always seed with the first real element.
Exam tip: "Find the cheapest", "find the coldest", "find the minimum" — these all signal the lowest-value algorithm. Swap > for < and you are done.
Finding the Lowest Value — Step by Step
16.4 Finding Both Highest and Lowest
You can find both extremes in a single pass through the data. Use two variables (Highest and Lowest), seed both to the first element, and inside one loop run two independent IF statements — one with > for Highest, one with< for Lowest.
// Find both highest and lowest in one loop
DECLARE Numbers : ARRAY[1:5] OF INTEGER
DECLARE I, Highest, Lowest : INTEGER
FOR I <- 1 TO 5
OUTPUT "Enter number: "
INPUT Numbers[I]
NEXT I
Highest <- Numbers[1]
Lowest <- Numbers[1]
FOR I <- 2 TO 5
IF Numbers[I] > Highest THEN
Highest <- Numbers[I]
ENDIF
IF Numbers[I] < Lowest THEN
Lowest <- Numbers[I]
ENDIF
NEXT I
OUTPUT "Highest: ", Highest
OUTPUT "Lowest: ", Lowest- Seed both —
Highest <- Numbers[1]andLowest <- Numbers[1]. - Two IFs per iteration — one for
>(Highest), one for<(Lowest). - Independent checks — both IFs run for every element; they are NOT else-branches.
- Single pass — visits each element once (more efficient than two loops).
Why one loop? A single loop that updates both variables visits each element once (N comparisons × 2 IFs). Two separate loops would visit each element twice (2N comparisons). The result is identical — the single pass is just more efficient.
Finding Both — Single Pass Through the Data
16.5 Practical Examples
Four classic highest & lowest problems: hottest/coldest week, cheapest item, and a sentinel-controlled version that reads values from the user.
Example 1 — Hottest and Coldest Day of the Week
DECLARE Temp : ARRAY[1:7] OF INTEGER
DECLARE I, Highest, Lowest : INTEGER
FOR I <- 1 TO 7
OUTPUT "Enter temperature: "
INPUT Temp[I]
NEXT I
Highest <- Temp[1]
Lowest <- Temp[1]
FOR I <- 2 TO 7
IF Temp[I] > Highest THEN Highest <- Temp[I] ENDIF
IF Temp[I] < Lowest THEN Lowest <- Temp[I] ENDIF
NEXT I
OUTPUT "Hottest: ", Highest
OUTPUT "Coldest: ", LowestReads 7 temperatures, then finds both extremes in one pass through the array. Both Highest and Lowest are seeded with Temp[1] before the loop.
Example 2 — Cheapest Item in a Shopping List
DECLARE Price : ARRAY[1:10] OF REAL
DECLARE I : INTEGER
DECLARE Cheapest : REAL
FOR I <- 1 TO 10
OUTPUT "Enter price: "
INPUT Price[I]
NEXT I
Cheapest <- Price[1]
FOR I <- 2 TO 10
IF Price[I] < Cheapest THEN
Cheapest <- Price[I]
ENDIF
NEXT I
OUTPUT "Cheapest item costs: ", CheapestUses REAL for prices (decimals). The lowest-value pattern with a < comparison finds the minimum price in the array.
Example 3 — Sentinel-Controlled Highest & Lowest
DECLARE V, Highest, Lowest : INTEGER
INPUT V
Highest <- V
Lowest <- V
WHILE V <> -1 DO
IF V > Highest THEN Highest <- V ENDIF
IF V < Lowest THEN Lowest <- V ENDIF
INPUT V
ENDWHILE
OUTPUT "Highest: ", Highest
OUTPUT "Lowest: ", LowestReads values until -1 is entered. The first value seeds both Highest and Lowest before the loop; subsequent values are compared inside the WHILE body. The sentinel -1 is never compared.
Watch out: forgetting the inner INPUT in the sentinel loop creates an infinite loop — the same value is compared forever. Always read the next value inside the loop body before ENDWHILE.
Practical Examples — Apply the Algorithm
16.6 Key Points Summary
A quick recap of everything covered on highest & lowest:
- Purpose — Find the maximum (highest) and/or minimum (lowest) value in data.
- Initialise to FIRST element — Highest/Lowest <- Numbers[1] BEFORE the loop, never 0.
- Use > for highest — IF Value > Highest THEN Highest <- Value.
- Use < for lowest — IF Value < Lowest THEN Lowest <- Value.
- Both in one loop — Seed both to first element; two independent IFs inside one loop.
- Loop from index 2 — FOR I <- 2 TO N — index 1 is already in Highest/Lowest.
- OUTPUT after loop — Print the final value AFTER the loop ends.
- Sentinel excluded — The stop value (-1 or 0) is never compared to Highest/Lowest.
- Works on any sequence — Arrays (FOR), user input (WHILE + sentinel) — pattern is the same.
- Common mistake — Initialising to 0 instead of first element — fails for all-negative or all-positive data.
Exam tip: look for the keywords "largest", "maximum", "highest", "smallest", "minimum", "lowest", "cheapest", "hottest", "coldest". They all signal the highest & lowest algorithm — seed with the first element and compare with > or <.