Ryans Tutorials
More great tutorials at RyansTutorials

Pseudocode

Cheat Sheet

Introduction

Below is a summary of the main concepts and syntax used in presenting algorithms as pseudocode.

Sequence

Sequence

  1. BEGIN
  2.   Get number1
  3.   Get number2
  4.   answer = number1 + number2
  5.   ...
  6.   Display answer
  7. END

Selection

Binary Selection

Binary Selection

  1. BEGIN
  2.   Get number1
  3.   IF number1 > 5 THEN
  4.     Display "great work"
  5.   ENDIF
  6.   Display "some more data to display"
  7. END

Binary Selection with ELSE

  1. BEGIN
  2.   Get number1
  3.   IF number1 > 5 THEN
  4.     Display "great work"
  5.   ELSE
  6.     Display "almost there"
  7.   ENDIF
  8.   Display "more data to display"
  9. END

Binary Selection - nested

  1. BEGIN
  2.   Get number1
  3.   IF number1 > 10 THEN
  4.     Display "awesome work"
  5.   ELSEIF number1 > 5 THEN
  6.     Display "great work"
  7.   ELSE
  8.     Display "almost there"
  9.   ENDIF
  10.   Display "something else"
  11. END

The final ELSE is optional here but it is good practice to have it in.

Multi-way selection - CASEWHERE

Binary Selection - nested

  1. BEGIN
  2.   Get number1
  3.   CASEWHERE number1 =
  4.     10 : Display "awesome work"
  5.     7 : Display "great work"
  6.     5 : Display "going well"
  7.     3 : Display "almost there"
  8.     OTHERWISE : Display "keep practicing"
  9.   END CASE
  10.   Display "something else"
  11. END

Repetition

Pre-test - WHILE

WHILE

  1. BEGIN
  2.   var1 = 4
  3.   WHILE var1 < 10
  4.     Display "great work"
  5.     Increment var1
  6.   ENDWHILE
  7.   Display "something else"
  8. END

Post-test - REPEAT

REPEAT

  1. BEGIN
  2.   var1 = 4
  3.   REPEAT
  4.     Display "great work"
  5.     Increment var1
  6.   UNTIL var1 > 10
  7.   Display "something else"
  8. END

Counted - FOR / NEXT

FOR / NEXT

  1. BEGIN
  2.   FOR var1 = 1 TO 10 STEP 1
  3.     Display "great work"
  4.   NEXT var1
  5.   Display "something else"
  6. END

Arrays and Records

Arrays

Array

  1. BEGIN
  2.   Set scores to [45, 73, 87, 56, 44]
  3.   scores[3] = 38
  4.   Display scores[2]
  5.   IF scores[1] > 70 THEN
  6.     Display "Well done!"
  7.   ENDIF
  8. 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

  1. BEGIN
  2.   Set player.name to "Fred"
  3.   Set player.health to 100
  4.   Set player.score to 5
  5.   ...
  6.   player.health = 80
  7.   Display player.name
  8. END

Functions

Sequence

  1. BEGIN
  2.   Get number1
  3.   answer = validate (number1)
  4.   ...
  5.   Display answer
  6. END
  7.  
  8. BEGIN validate (var1)
  9.   ...
  10.   result = var1 + 20
  11.   ...
  12.   return result
  13. 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.