A Level OCR Computer Science Pseudocode Guide
Arithmetic operators are also used: "+", "-", "*", "/", "MOD", "DIV", and "^".
+ : add, - : subtract, * : multiply, / : divide MOD : modulus, DIV : quotient, ^ : exponentiation
Comparison operators are also used: "==", "!=", "<", "<=", ">", and ">=".
== : equal to, != : not equal to < : less than, <= : less than or equal to > : greater than, >= : greater than or equal to
Object-oriented pseudocode: Classes. Access level is denoted by the keywords "public" and "private". "super.{procedure/function}({value})" is used to inherit methods from the superclass
classes: class {className} ... endclass inheritance: class {subclass} inherits {superclass} ... super.{procedure/function}({value}) endclass
Output a variable using "print( )".
print({variable}) or print("{string}") e.g. print("Hello, " + name + "!")
Subroutines: Procedures perform a task using a value.
procedure: procedure {procedureName}({value}) ... endprocedure calling procedure: {procedureName}({value})
Arrays start from 0 and are declared with the keyword array. 2D arrays are row by column.
array {1D}[n] {1D}[0] ... {1D}[n-1] array {2D}[n, n] {1D}[0, 0] {1D}[0, 1] {1D}[0, 2] ... {1D}[1, 0] {1D}[1, 1] {1D}[1, 2] ... ... etc.
Iteration: Until loops check if a condition is true at the end of the loop and stop when it is false.
do ... until {condition, such as variable == value}
Iteration: For loops count inclusively, so "for i=0 to n" would loop n+1 times.
for i=0 to {any number} ... next i
Subroutines: Functions return a value.
function: function {functionName}({value}) ... return {some value to the main program} endfunction calling function: {variable} = {functionName}({value})
Selection: If/else uses the keywords "if", "elseif" and "else". "if" specifies a specific condition, "elseif" specifies a different condition, and "else" is carried out if neither is met. "elseif" can be used more than once.
if {condition} then ... elseif {condition} then ... else ... endif
Object-oriented pseudocode: Methods. Access level is denoted by the keywords "public" and "private".
methods: {private/public} {procedure/function} {name}({values}) ... end{procedure/function} calling methods: {object}.{procedure/function}({values})
Casting converts a variable from one datatype into another. Variables can be typecast using the "int", "str", and "float" functions.
str(3) returns "3" int("3") returns 3 float("3.14") returns 3.14
Selection: Switch/case is based on the value of a single variable, with the "cases" being the values that the variable is equal to. "default" is the case that is carried out if none of the specified cases have been met.
switch {variable}: case {value}: ... case {another value}: ... default: ... endswitch
Iteration: While loops check if a condition is true at the beginning of the loop and stop when it is false.
while {condition, such as variable == value} ... endwhile
Logical operators such as "AND", "OR" and "NOT" can be used.
{condition} {operator} {condition} e.g. while x <= 5 AND flag == false
Reading from files.
{file} = openRead("{nameOfFile}.txt") : opens a file to read. {file}.readline( ) : reads a line of text from the file. {file}.endOfFile( ) : determines the end of the file. {file}.close ( ) : closes the file.
Writing to files.
{file} = openWrite("{nameOfFile}.txt") : opens a file to write. {file}.writeLine("{stuff to write}") : adds a line of text to the file.
Finding the length of a string.
{stringName}.length e.g. name = "John", length = name.length print(length) //outputs "4"
Getting a substring from a string. The first character of a string starts from 0.
{stringName}.substring({startingPosition}, {numberOfCharacters}) e.g. name = "John" print(name.substring(1,2)) //outputs "oh"
Variables are assigned using the "=" operator. Strings are put in quote marks.
{variable name} = {value} e.g. x = 3, name = "bob"
Ask a user for an input using "input( )".
{variable} = input("{prompt to user}") e.g. name = input("Please enter your name")