Pseudocode Cheat Sheet
by lcheong via cheatography.com/59690/cs/15631/
Declaring Variables and Constants String Handling
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]
Intege VAR age Whole numbers only 0, 6, chatacter from a string Example:
r 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
as STRING text, it is a collection 2", name = INPUT("Enter your name")
of characters "$money
$"
Boole VAR Could take one of True/Fal
an numFound two values, usually se, 1/0,
as TRUE or FALSE Yes/No
BOOLEAN
Casting Variables
You can change the data type of a variable by using
casting.
Converting integer 3 to str(3) returns "3"
string.
Converting string "3" to int("3") returns 3
integer.
Converting string "3.14" to float("3.14") returns
float. 3.14
By lcheong Not published yet. Sponsored by Readability-Score.com
cheatography.com/lcheong/ Last updated 30th April, 2018. Measure your website readability!
Page 1 of 4. https://readability-score.com
Pseudocode Cheat Sheet
by lcheong via cheatography.com/59690/cs/15631/
Outputting to screen 1-Dimensional Arrays (cont)
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" Declarin A 2D array is built as ARRAY(row, column)
with values
names[2] = "Catherine" g a 2D ARRAY score[4,5]
names[3] = "Dana" array builds an array of 4 rows, 5 columns.
names[4] = "Elijah" This can be interpreted as 4 Tests, 5 Students
Displaying a specific item from PRINT(names[3]) Initialisin score[0,0] = "15"
an array will display "Dana" g a 2D Sets score 15 to Test 0, Student 0
array -
Displaying ALL items in an array FOR i = 0 to 5
filling it
- method 1 PRINT(names[i])
up with
NEXT i
values
Displaying ALL items in an array ARRAY names[5]
- method 2 names[0] = "Ahmad" Displayin PRINT(score[1,3])
names[1] = "Ben" ga will display 14
names[2] = "Catherine" specific
names[3] = "Dana" item
names[4] = "Elijah" from a
PRINT(names) 2D array
Dynamically inserting values in E.g. Ask the user to enter Dynamic E.g. Ask the user to enter all the scores
an array 5 names ally FOR i = 0 to 3
FOR i = 0 to 5 inserting FOR j = 0 to 4
names[i] = INPUT("Enter values in score[i,j] = INPUT("Enter score for Test " + i + " Student " + j + ": ")
name:") NEXT j
an array
NEXT i NEXT i
By lcheong Not published yet. Sponsored by Readability-Score.com
cheatography.com/lcheong/ Last updated 30th April, 2018. Measure your website readability!
Page 2 of 4. https://readability-score.com
Pseudocode Cheat Sheet
by lcheong via cheatography.com/59690/cs/15631/
File Handling - Reading from a file Sub Programs - Functions
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".
myFile = openRead("sample.txt") FUNCTION join_strings(x as STRING, y as
while NOT myFile.endOfFile() STRING) as STRING
PRINT(myFile.readLine()) RETURN x + " " + y
ENDWHILE ENDFUNCTION
myFile.close() Calling the function from the main program:
subject = join_strings("computer", "science")
File Handling - Writing to a file PRINT(subject)
Adding a line of text to a file
Comparison operators
myFile = openWrite("sample.txt")
myFile.writeline("Hello World") == Equal to
myFile.close() != Not equal to
< Less than
Sub Programs - Procedures
<= Less than or equal to
Procedures don't have to take parameters... ...but sometimes they will.
> Greater than
PROCEDURE welcome() PROCEDURE betterwelcome(name as STRING)
PRINT("Hello and welcome.") PRINT("Hello" + name + "and welcome.") >= Greater than or equal to
PRINT("Let's learn about procedures.") PRINT("Let's learn about procedures.")
ENDPROCEDURE ENDPROCEDURE Arithmetic operators
Procedures are called by typing their name... ...and giving an argument if necessary
+ Addition
welcome() betterwelcome("Pablo") e.g. x=6+5 gives 11
Will display: Will display: - Subtraction
Hello and welcome. Hello Pablo and welcome. e.g. x=6-5 gives 1
Let's Learn about procedures. Let's Learn about procedures.
* Multiplication
Note that procedures DO NOT return a value e.g. x=12*2 gives 24
/ 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
By lcheong Not published yet. Sponsored by Readability-Score.com
cheatography.com/lcheong/ Last updated 30th April, 2018. Measure your website readability!
Page 3 of 4. https://readability-score.com
Pseudocode Cheat Sheet
by lcheong via cheatography.com/59690/cs/15631/
Boolean operators Iteration - Repeat Loop (cont)
AND If two or more statements are true. VAR total as INTEGER
total = 0
OR If either statement is true.
VAR cost, coin, change as INTEGER
NOT To reverse the logical results of a statement. cost = total cost in pence
REPEAT
Selection - if/else coin = INPUT("Value of coin")
total = total + coin
Selection involves making decisions based on a comparison.
UNTIL total >= cost
Comparison operators are used, sometimes with boolean operators.
change = total - cost
IF entry == "A" THEN OUTPUT change
PRINT("You selected A")
ELSEIF entry == "B" THEN Iteration - While Loop
PRINT("You selected B")
ELSE: This loop is controlled by a condition at the start of the loop. Keep going
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.
SWITCH entry: VAR total as INTEGER
CASE "A": total = 0
PRINT("You selected A") VAR cost, coin, change as INTEGER
CASE "B": cost = total cost in pence
PRINT("You selected B") WHILE total < cost
DEFAULT: coin = INPUT("Value of coin")
PRINT("Unrecognised selection") total = total + coin
ENDSWITCH ENDWHILE
change = total - cost
OUTPUT change
Iteration - For Loop
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.
Iteration - Repeat Loop VAR total as INTEGER
total = 0
This loop is controlled by a condition at the end of the loop. Keep going
VAR cost, coin, change as INTEGER
until the condition is TRUE (i.e. while it is false). Always runs the code cost = total cost in pence
inside it at least once. You get an infinite loop if the condition is never DO
true. coin = INPUT("Value of coin")
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
OUTPUT change
amount of change.
By lcheong Not published yet. Sponsored by Readability-Score.com
cheatography.com/lcheong/ Last updated 30th April, 2018. Measure your website readability!
Page 4 of 4. https://readability-score.com