0% found this document useful (0 votes)
3 views

Pseudocode cheatsheet

Uploaded by

Aneesh Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Pseudocode cheatsheet

Uploaded by

Aneesh Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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
subrou​tine. 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
Extr​acting a specific name[i]
Integer
​ VAR age Whole numbers only 0, 6, chatacter from a string Exam​ple:
as 10293, - name = "​Pal​oma​"
INTEGER 999 name[3] returns "​o"

Real VAR price Numbers that have a 0.15, - Conv​erting to upperc​ase name.UPPER()
or as REAL decimal point 5.87,
Conv​erting to lowerc​ase 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 variab​le.

String VAR name Used to represent "​FsT​mQ Exam​ple: VAR name as STRING

as STRING text, it is a collection 2​", name = INPUT(​"​Enter your name")

of characters "​$mo​ney​
$"
Boolean
​ VAR Could take one of True/F​al
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.

Conv​erting integer 3 to str(3) returns "​3"


string.

Conv​erting string "​3" to int("3") returns 3


integer.

Conv​erting string "​3.1​4" to float("3.14") returns


float. 3.14
Outputting to screen 1-Dime​nsional Arrays (cont)

Outp​utting a string PRINT("Hello") Perf​orming calcul​ations on E.g. Increase element 2 of ARRAY age by
one Array element 10: age[2] = age[2] + 10
Outp​utting a variable word = ("He​llo​")
set by you PRINT(​word) Perf​orming calcul​ations on E.g. Increase ALL the values in ARRAY
Array elements ages by 2:
Outp​utting 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-Dime​nsional Arrays
2-Dime​nsional Arrays
Decl​aring an array ARRAY names[5]
Note: Refer to CGP Page 50
Init​ial​ising an array - filling it up names[0] = "​Ahm​ad"
names[1] = "​Ben​" Declaring
​ A 2D array is built as ARRAY(row, column)
with values
names[2] = "​Cat​her​ine​" a 2D array ARRAY score[4,5]
names[3] = "​Dan​a" builds an array of 4 rows, 5 columns.
names[4] = "​Eli​jah​" This can be interp​reted as 4 Tests, 5 Students

Disp​laying a specific item from PRINT(names[3]) Initialising


​ ​ score[0,0] = "​15"

an array will display "​Dan​a" a 2D array Sets score 15 to Test 0, Student 0


-filling it
Disp​laying ALL items in an array FOR i = 0 to 5
up with
- method 1 PRINT(names[i])
values
NEXT i

Disp​laying ALL items in an array ARRAY names[5]


names[0] = "​Ahm​ad" Displaying
​ PRINT(​sco​re[​1,3])
- method 2
names[1] = "​Ben​" a specific will display 14
names[2] = "​Cat​her​ine​" item
names[3] = "​Dan​a" from a
names[4] = "Elijah" 2D array
PRINT(names)

Dyna​mically inserting values in E.g. Ask the user to enter Dynamically


​ E.g. Ask the user to enter all the scores
an array 5 names inserting FOR i = 0 to 3
FOR i = 0 to 5 values in an FOR j = 0 to 4
names[i] = INPUT(​"​Enter array score[i,j] = INPUT("Enter score for Test " + i + " Student " + j + ": ")
name:") NEXT j
NEXT i NEXT i
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) Func​tions take at least one parameter and they
must always return a value.
myFile = openRe​ad(​"​sam​ple.tx​t")
x = myFile.re​adL​ine() Example: Write a function to join two strings
myFile.cl​ose() together with a space between them and show it
Reading and outputting the whole contents of a text file working on the strings "​com​put​er" and "​sci​enc​e".

myFile = openRe​ad(​"​sam​ple.tx​t") FUNCTION join_s​tri​ngs(x as STRING, y as


while NOT myFile.en​dOf​File() STRING) as STRING

PRINT(myFile.readLine()) RETURN x + " " + y

ENDWHILE ENDFUNCTION

myFile.cl​ose() Calling the function from the main program:


subject = join_s​tri​ngs​("co​mpu​ter​", "​sci​enc​e")
File Handling - Writing to a file PRINT(subject)

Adding a line of text to a file


Comparison operators
myFile = openWr​ite​("sa​mpl​e.t​xt")
myFile.wr​ite​lin​e("Hello World") == Equal to
myFile.cl​ose() != 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: - Subtra​ction


Hello and welcome. Hello Pablo and welcome. e.g. x=6-5 gives 1
Let's Learn about proced​ures. Let's Learn about proced​ures.
* Multip​lic​ation
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

^ Expone​nti​ation
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 compar​ison.
UNTIL total >= cost
Comp​arison operat​ors are used, sometimes with boolean operat​ors.
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 select​ion​") 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 Exam​ple: Write an algorithm that a superm​arket self-scan machine could

Selection involves making decisions based on a compar​ison. use to check if enough money has been fed into it and output the right

Comp​arison operat​ors are used, sometimes with boolean operat​ors. amount of change.

SWITCH entry: VAR total as INTEGER


total = 0
CASE "​A":
PRINT("You selected A") VAR cost, coin, change as INTEGER

CASE "​B": cost = total cost in pence


WHILE total < cost
PRINT("You selected B")
​ ​ coin = INPUT(​"​Value of coin")
DEFAULT:
PRINT("Unrecognised select​ion​") ​ ​ ​total = total + coin
ENDWHILE
ENDSWITCH
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")
Exam​ple: Write an algorithm that a superm​arket self-scan machine could
NEXT i
Will print hello 8 times (0-7 inclus​ive). 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")

Exam​ple: Write an algorithm that a superm​arket 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

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

You might also like