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
FORloop fits. Totaladds up every mark;PassCountonly goes up when theIFcondition 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 = ", AverageSample run
| Mark | Total | PassCount |
|---|---|---|
| 42 | 42 | 0 |
| 50 | 92 | 1 |
| 74 | 166 | 2 |
| 21 | 187 | 2 |
| 90 | 277 | 3 |
Step 1 / 5
Program B: accept only a valid menu choice
Keep asking until the user enters a choice from 1 to 4.
REPEAT ... UNTILfits because the user must see the menu at least once.- The condition says what a valid choice is, not what an invalid one is.
ANDchecks 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: ", ChoiceSample run
| Input | Valid? | What happens |
|---|---|---|
| 8 | No | Asks again |
| 0 | No | Asks again |
| 3 | Yes | Outputs Accepted choice: 3 |
Program C: search an array with a flag
Find out whether a name the user enters is in the array StudentNames.
Foundstarts asFALSEand becomesTRUEwhen a name matches.- This version checks every item. Examiners usually accept it because it is clear.
- The final
IFuses the flag to output one message. EnterAliand the output isFound.
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"
ENDIFProgram 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, andSumSquare(3, 4)returns 25, so the last line isAnswer = 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