Unit 1 · Basics of Computer Programming

1.3.2Worked examples

On this page
  1. Example 1: Largest of two numbers
  2. Example 2: Sum of 1 to N
  3. Example 3: Grade classification (nested selection)
  4. Example 4: Reading until a sentinel value
  5. Example 5: Using a procedure

Example 1: Largest of two numbers

Pseudocode
BEGIN
  READ a, b
  IF a > b THEN
    largest = a
  ELSE
    largest = b
  END IF
  PRINT largest
END

How it works: IF ... ELSE ... END IF gives exactly two paths. The indentation shows which line belongs to which path, and END IF shows where the selection ends, so PRINT largest runs whichever path was taken. Compare this with the flowchart in 1.2.2: the diamond became IF, and the point where the two paths join became END IF.

Example 2: Sum of 1 to N

Pseudocode
BEGIN
  READ n
  total = 0
  FOR i = 1 TO n DO
    total = total + i
  END FOR
  PRINT total
END

How it works: a FOR loop is used because the number of passes (n) is known before the loop starts. The FOR line does the work of three separate steps in the flowchart version: it sets i to 1, tests i <= n before each pass, and adds 1 to i after each pass. total is an accumulator: it must be set to 0 before the loop. If it were set inside the loop it would be reset on every pass.

Example 3: Grade classification (nested selection)

Pseudocode
BEGIN
  READ marks
  IF marks >= 75 THEN
    PRINT "A"
  ELSE IF marks >= 65 THEN
    PRINT "B"
  ELSE IF marks >= 50 THEN
    PRINT "C"
  ELSE
    PRINT "F"
  END IF
END

How it works: ELSE IF chains several conditions together. They are tested from top to bottom and the first true condition wins; the rest are skipped. That is why the bands are tested from the highest down: when the test marks >= 65 is reached, we already know that marks is below 75. The final ELSE catches every value that failed all the tests above it.

Order matters: if you tested marks >= 50 first, a mark of 90 would print "C", because 90 is also 50 or more. Always put the most restrictive condition first.

Example 4: Reading until a sentinel value

Pseudocode
BEGIN
  total = 0
  READ x
  WHILE x != 0 DO
    total = total + x
    READ x
  END WHILE
  PRINT total
END

How it works: a WHILE loop is used because we do not know in advance how many numbers the user will enter. The value 0 is the sentinel: a special value that means "no more data". Note the pattern, called a priming read: read once before the loop, then read again as the last step of the body. This way the sentinel is tested before it can be added to the total.

Example 5: Using a procedure

Pseudocode
PROCEDURE isPrime(n)
  IF n < 2 THEN RETURN false
  FOR d = 2 TO n - 1 DO
    IF n MOD d = 0 THEN RETURN false
  END FOR
  RETURN true
END PROCEDURE

Tip: isPrime only needs to test divisors up to the square root of n: if n has a divisor larger than √n it also has one smaller than √n. Writing the loop as WHILE d * d <= n gives the same answers with far fewer steps for large numbers.

How it works: a procedure is a named block of steps. It receives a value through its parameter (n) and sends a result back with RETURN. As soon as a RETURN runs, the procedure ends, so the loop stops at the first divisor it finds. If no divisor is found, the loop finishes and RETURN true runs. Numbers below 2 are not prime, so they are rejected at the start.

Another part of the algorithm can now use it like this:

Pseudocode
FOR k = 1 TO 20 DO
  IF isPrime(k) THEN PRINT k
END FOR

This prints 2, 3, 5, 7, 11, 13, 17 and 19. Procedures make algorithms shorter, easier to read and easier to test, because each procedure can be checked on its own. In Java, procedures become methods (see 3.1.2).