Pseudocode cheatsheet
Pseudocode cheatsheet
Variables are assigned using the = operator e.g. x = 3. Finding the length of a string VAR name as STRING
name = INPUT("Enter your name")
Local variables Variables declared inside a function
PRINT("Your name has" + name.length + "characters")
or procedure are local to that
subroutine. Getting a substring stringname.subString(startingPosition, numberOfCharacters)
NB The string will start with the 0th character.
Global variables Variables in the main program can
Example:
be made global with the keyword
global. E.g. GLOBAL userid = 123. someText = "Computer Science"
PRINT(someText.length)
Constants The values of constants do not
PRINT(someText.substring(3,3))
change throughout the program.
Will display:
E.g. CONST Vat = 20.
16
put
Data Types
Extracting a specific name[i]
Integer
VAR age Whole numbers only 0, 6, chatacter from a string Example:
as 10293, - name = "Paloma"
INTEGER 999 name[3] returns "o"
Real VAR price Numbers that have a 0.15, - Converting to uppercase name.UPPER()
or as REAL decimal point 5.87,
Converting to lowercase name.LOWER()
Float 100.0
Char VAR letter A single letter, "A", "k", Taking inputs from user
as CHAR number, symbol "5", "-",
"$" Inputs taken from a user need to be stored in a variable.
String VAR name Used to represent "FsTmQ Example: VAR name as STRING
of characters "$money
$"
Boolean
VAR Could take one of True/Fal
numFound two values, usually se, 1/0,
as TRUE or FALSE Yes/No
BOOLEAN
Casting Variables
Outputting a string PRINT("Hello") Performing calculations on E.g. Increase element 2 of ARRAY age by
one Array element 10: age[2] = age[2] + 10
Outputting a variable word = ("Hello")
set by you PRINT(word) Performing calculations on E.g. Increase ALL the values in ARRAY
Array elements ages by 2:
Outputting a variable VAR name as STRING
entered by the user name = INPUT("What is your name?") FOR i = 0 to 4
PRINT("Hello" + name) age[i] = age[i] + 2
NEXT i
1-Dimensional Arrays
2-Dimensional Arrays
Declaring an array ARRAY names[5]
Note: Refer to CGP Page 50
Initialising an array - filling it up names[0] = "Ahmad"
names[1] = "Ben" Declaring
A 2D array is built as ARRAY(row, column)
with values
names[2] = "Catherine" a 2D array ARRAY score[4,5]
names[3] = "Dana" builds an array of 4 rows, 5 columns.
names[4] = "Elijah" This can be interpreted as 4 Tests, 5 Students
Reading and outputting a single line from the text file(see further details in CGP Pg 51) Functions take at least one parameter and they
must always return a value.
myFile = openRead("sample.txt")
x = myFile.readLine() Example: Write a function to join two strings
myFile.close() together with a space between them and show it
Reading and outputting the whole contents of a text file working on the strings "computer" and "science".
ENDWHILE ENDFUNCTION
/ Division
e.g. x=12/2 gives 6
MOD Modulus
e.g. 12MOD5 gives 2
DIV Quotient
e.g. 17DIV5 gives 3
^ Exponentiation
e.g. 3^4 gives 81
PRINT("Unrecognised selection") while the condition is TRUE (i.e. until it is false). Never runs the code
ENDIF inside if condition is initially false. You get an infinite loop if the condition
is always true.
Selection - switch/case Example: Write an algorithm that a supermarket self-scan machine could
Selection involves making decisions based on a comparison. use to check if enough money has been fed into it and output the right
Comparison operators are used, sometimes with boolean operators. amount of change.
FOR loops will repeat the code inside them a fixed number of times. The Iteration - Do While Loop
number of times that the code repeats will depend on an initial value, end
This loop is controlled by a condition at the end of the loop. Keep going
value, and the step count.
while the condition is TRUE (i.e. until it is false). Always runs the code
Example:
inside it at least once. You get an infinite loop if the condition is always
FOR i = 0 to 7
true.
PRINT("Hello")
Example: Write an algorithm that a supermarket self-scan machine could
NEXT i
Will print hello 8 times (0-7 inclusive). use to check if enough money has been fed into it and output the right
amount of change.
Example: Write an algorithm that a supermarket self-scan machine could total = total + coin
WHILE total < cost
use to check if enough money has been fed into it and output the right
amount of change. OUTPUT change