Pseudocode
Cheat Sheet
Introduction
Below is a summary of the main concepts and syntax used in presenting algorithms as pseudocode.
Sequence
Sequence
- BEGIN
- Get number1
- Get number2
- answer = number1 + number2
- ...
- Display answer
- END
Selection
Binary Selection
Binary Selection
- BEGIN
- Get number1
- IF number1 > 5 THEN
- Display "great work"
- ENDIF
- Display "some more data to display"
- END
Binary Selection with ELSE
- BEGIN
- Get number1
- IF number1 > 5 THEN
- Display "great work"
- ELSE
- Display "almost there"
- ENDIF
- Display "more data to display"
- END
Binary Selection - nested
- BEGIN
- Get number1
- IF number1 > 10 THEN
- Display "awesome work"
- ELSEIF number1 > 5 THEN
- Display "great work"
- ELSE
- Display "almost there"
- ENDIF
- Display "something else"
- END
The final ELSE is optional here but it is good practice to have it in.
Multi-way selection - CASEWHERE
Binary Selection - nested
- BEGIN
- Get number1
- CASEWHERE number1 =
- 10 : Display "awesome work"
- 7 : Display "great work"
- 5 : Display "going well"
- 3 : Display "almost there"
- OTHERWISE : Display "keep practicing"
- END CASE
- Display "something else"
- END
Repetition
Pre-test - WHILE
WHILE
- BEGIN
- var1 = 4
- WHILE var1 < 10
- Display "great work"
- Increment var1
- ENDWHILE
- Display "something else"
- END
Post-test - REPEAT
REPEAT
- BEGIN
- var1 = 4
- REPEAT
- Display "great work"
- Increment var1
- UNTIL var1 > 10
- Display "something else"
- END
Counted - FOR / NEXT
FOR / NEXT
- BEGIN
- FOR var1 = 1 TO 10 STEP 1
- Display "great work"
- NEXT var1
- Display "something else"
- END
Arrays and Records
Arrays
Array
- BEGIN
- Set scores to [45, 73, 87, 56, 44]
- scores[3] = 38
- Display scores[2]
- IF scores[1] > 70 THEN
- Display "Well done!"
- ENDIF
- END
Most people will start counting at 0 but be aware that sometimes counting will start at 1.
Most people use square brackets [] however you will encounter material where they use normal brackets () instead.
Records
Record
- BEGIN
- Set player.name to "Fred"
- Set player.health to 100
- Set player.score to 5
- ...
- player.health = 80
- Display player.name
- END
Functions
Sequence
- BEGIN
- Get number1
- answer = validate (number1)
- ...
- Display answer
- END
- BEGIN validate (var1)
- ...
- result = var1 + 20
- ...
- return result
- END validate
Concepts
- Remember to indent your code to identify control structures.
- Be consistent with the terms you use for things such as input and output.
- Just the right amount of detail for the intended audience.
- Keywords are always written in CAPITALS.
- Don't forget the corresponding closing keyword for structural elements.