1

mrahmedcomputing

KS3, GCSE, A-Level Computing Resources

Lesson 3. Pseudocode


Lesson Objective

  1. Understand the purpose of pseudocode.
  2. Be able to write some simple algorithms using it.

Lesson Notes

What is a Pseudocode?

Pseudocode is a kind of structured English for describing algorithms.

It allows the designer to focus on the logic of the algorithm without being distracted by the syntax of the programming language.

Programming Constructs

There are three basic ways of controlling the order in which program statements are carried out.

Sequence

The statements are executed in the order they are written.

mealCost = 4.00
drinkCost = 2.00
total = mealCost + drinkcost

Selection

An IF statement is a selection statement. The next statement to be executed depends on whether the condition being tested is true or false.

hoursPerNight = USERINPUT
IF (hoursPerNight < 8) THEN
  OUTPUT "That's not enough!"
ELSE
  OUTPUT "That's plenty!"
ENDIF

Iteration

Iteration means repetition. There are three types of iteration statement in most, but not all, imperative programming languages such as Pascal, Visual Basic or Python.

FOR Loop

Use this when you want to execute the loop a specific number of times.

total = 0
FOR counter = 1 TO 7
  maxTemperature = USERINPUT
  add maxTemperature to total
ENDFOR
averageWeeksTemp = Total / 7
OUTPUT "This week's average is ", averageWeeksTemp

WHILE Loop

Use this when you want to execute the loop WHILE a certain condition is true.

emailAddress = USERINPUT
WHILE emailAddress does not contain "@"
  OUTPUT "Invalid address – please re-enter"
  emailAddress = USERINPUT
ENDWHILE
OUTPUT "Thank you"

REPEAT UNTIL Loop

The condition is not tested until the end of the loop (it is always executed at least once), so you need an IF statement as well as the Repeat loop.

OUTPUT "Please enter email address"
REPEAT
  emailAddress = USERINPUT
  IF emailAddress does not contain "@" THEN
    OUTPUT "Invalid address – please re-enter
  ENDIF
UNTIL emailAddress contains "@"


Note: Examples linked to previous Flowchart Lesson.

Example Pseudocode 1

OUTPUT "Enter User ID"
ID = INPUT
WHILE ID NOT IN Database THEN
    OUTPUT "Enter VALID User ID"
    ID = INPUT
END WHILE
ADD ID to Database

Example Pseudocode 2

total = 0
count = 0
count = count + 1
total = total + count
WHILE count != 1000 THEN
    count = count + 1
    total = total + count
END WHILE
OUTPUT total

3