Docs

Pseudocode reference

Worked programs

Four complete exam-style programs with explanations and sample runs: counting marks, validating a menu, searching an array, and using subroutines.

updated November 2, 2026

Program A: count passes and find the average

Read 5 marks, count how many are passes (50 or more), and output the average.

  • There are exactly 5 marks, so a count-controlled FOR loop fits.
  • Total adds up every mark; PassCount only goes up when the IF condition is true.
  • The average is worked out once, after the loop ends.
Pseudocode
DECLARE Index : INTEGER
DECLARE Mark : INTEGER
DECLARE Total : INTEGER
DECLARE PassCount : INTEGER
DECLARE Average : REAL

Total <- 0
PassCount <- 0

FOR Index <- 1 TO 5
    OUTPUT "Enter mark ", Index
    INPUT Mark
    Total <- Total + Mark

    IF Mark >= 50
      THEN
        PassCount <- PassCount + 1
    ENDIF
NEXT Index

Average <- Total / 5
OUTPUT "Passes = ", PassCount
OUTPUT "Average = ", Average
Sample run
MarkTotalPassCount
42420
50921
741662
211872
902773
Step 1 / 5
Output: Passes = 3, Average = 55.4

Program B: accept only a valid menu choice

Keep asking until the user enters a choice from 1 to 4.

  • REPEAT ... UNTIL fits because the user must see the menu at least once.
  • The condition says what a valid choice is, not what an invalid one is.
  • AND checks the lower and upper bounds together.
Pseudocode
DECLARE Choice : INTEGER

REPEAT
    OUTPUT "1.View 2.Add 3.Delete 4.Exit"
    INPUT Choice
UNTIL Choice >= 1 AND Choice <= 4

OUTPUT "Accepted choice: ", Choice
Sample run
InputValid?What happens
8NoAsks again
0NoAsks again
3YesOutputs Accepted choice: 3

Program C: search an array with a flag

Find out whether a name the user enters is in the array StudentNames.

  • Found starts as FALSE and becomes TRUE when a name matches.
  • This version checks every item. Examiners usually accept it because it is clear.
  • The final IF uses the flag to output one message. Enter Ali and the output is Found.
Pseudocode
DECLARE StudentNames : ARRAY[1:5] OF STRING
DECLARE Index : INTEGER
DECLARE Target : STRING
DECLARE Found : BOOLEAN

StudentNames[1] <- "Mia"
StudentNames[2] <- "Tom"
StudentNames[3] <- "Ali"
StudentNames[4] <- "Zoe"
StudentNames[5] <- "Sam"

INPUT Target
Found <- FALSE
FOR Index <- 1 TO 5
    IF StudentNames[Index] = Target
      THEN
        Found <- TRUE
    ENDIF
NEXT Index

IF Found = TRUE
  THEN
    OUTPUT "Found"
  ELSE
    OUTPUT "Not found"
ENDIF

Program D: a procedure and a function together

Use a procedure to display a line and a function to do a calculation.

  • A procedure call is a statement on its own, so it starts with CALL.
  • A function returns a value, so its call goes inside an expression.
  • PrintLine(10) outputs 10 dashes, and SumSquare(3, 4) returns 25, so the last line is Answer = 25.
Pseudocode
PROCEDURE PrintLine(Count : INTEGER)
    DECLARE Index : INTEGER
    FOR Index <- 1 TO Count
        OUTPUT "-"
    NEXT Index
ENDPROCEDURE

FUNCTION SumSquare(A : INTEGER, B : INTEGER) RETURNS INTEGER
    RETURN A * A + B * B
ENDFUNCTION

DECLARE Answer : INTEGER
CALL PrintLine(10)
Answer <- SumSquare(3, 4)
OUTPUT "Answer = ", Answer