A
JASOA
Reference language for
Computing Science question
papers
This document introduces the reference language used to present code in SQA
Computing Science question papers for National 5, Higher and Advanced Higher
qualifications. Elements of the language required for Higher and Advanced
Higher only are indicated using margin highlights.
This edition: September 2016, version 1.0
Published by the Scottish Qualifications Authority
‘The Optima Building, 58 Robertson Street, Glasgow G2 8DQ
Lowden, 24 Wester Shawfair, Dalkeith, Midlothian EH22 1FD.
[Link]
© Scottish Qualifications Authority 2016Contents
1 Purpose ofthe reference language
2. Inroducng the language by example
3. Specification
3.4 Types
3.2 System entities
3.3 Identifiers
3.4 Commands
Variable introduction and assignment
Meaning of assignment
Command sequences
Condition
Repetition
Subprograms
3.5 Operations
3.6 Comments and elisions
3.7 Input/output (including file operations)
3.8 Object-oriented (00) programming
4 Further resources1 Purpose of the reference language
The ability to reason about code is increasingly being seen as a crucial part of
leaming to program, For example, if you can't explain in precise detail what a
fragment of code does, you can't debug. If you can't explain the code you've just
written to someone else, how can you justify any of the decisions you made in
creating it and then demonstrate any level of understanding?
To assess candidates’ ability to reason about programs, programs must be
presented in assessment questions. This document contains a specification for a
reference language designed for setting such questions, developed in
collaboration with Prof. Greg Michaelson of Heriot Walt University, Prof. Quintin
Cutts of the University of Glasgow, and Prof, Richard Connor of Strathelyde
University. It enables assessors, teachers and candidates to work to one well-
defined notation and is suitable for use in schools and further education/higher
education institutions.
Note: in earlier versions of related documents, this reference language was
referred to as ‘pseudocode’. Given that the language presented here is
formally-defined (and pseudocode is not), the term ‘reference language’ is
preferred. A formally-defined language is required because it is a candidate's
ability to understand and analyse code in such languages (including
programming languages) that should be assessed,
The use of a reference language supports SQA's decision to allow centres to use
the programming language of their choice for teaching and leaming, as long as
assessors ensure that candidates have mapped their understanding from the
language of instruction across to the reference language. This focus on concepts
that is shared among programming languages is potentially a major lever in
deepening understanding of computation in general
Although the idea of a clearly-defined reference language may seem daunting, it
is not, in fact, so different from the ‘pseudocode’ that has been used for years in
‘SQA question papers. It has simply been regularised, so that current and new
teachers, assessors, question paper setters and candidates will all be working to
the same definition
In reviewing this specification, bear in mind its primary purpose:
‘¢ Although candidates may be taught using one of a range of languages, this
clearly-defined reference language enables code to be presented in a way
that candidates can reason about it under closed assessment conditions.
‘¢ Candidates are not expected to write code in the reference language.
Assessors should be able to mark solutions written in a range of languages
‘commonly used for teaching, so candidates can use the language of their
choice
Note that assessors and candidates may choose to use this reference language
as a tool to support program design, but this is not its primary purpose. The
September 2018, version 1.0 1slision feature <...> presented in section 3, enables the inclusi
have not been fully worked out yet.
n of steps that
The aim of the rest of this document is to present the reference language
principally via a small number of examples. In reading through the examples and
specification, attachment to particular constructs, or to ‘my favourite construct in
language X’, should be avoided — itis the concepts that are the major focus.
September 2018, version 1.02 — Introducing the language by example
The following typical programming examples show that solutions in the reference
language do not differ markedly from those in any programming language.
The first example is for the problem:
Read in a number representing a temperature in degrees Celsius and
display it as a value in degrees Fahrenheit. f the Celsius value is C, then
the Fahrenheit value, F, is calculated as follows: F=(9/5)*C + 32.
Using the reference language, the solution would be written as follows:
DECLARE ¢ AS INTEGER INITIALLY FROM KEYBOARD
DECLARE £ INITIALLY ( 9.0/5.0) * c + 32
SEND £ 70 DISPLAY
‘An immediate observation is that the keywords are written in CAPITALS. In any
representation of programming language code, itis useful for the reader to
distinguish easily between the language's keywords and other names created by
the user. Readability has been a primary goal in designing the language.
Here is a slightly more complex problem:
Read in 10 numbers and display the average of those numbers as a
floating-point number.
Using the reference language, the solution would be written as follows:
DECLARE t) 0
DECLARE 0
DECLARE next Input INITIALLY 0
WH: < 10 D0
VE next Input FROM KEYBOARD
total T0 total ~ nextInput
count TO count + 1
END
SEND total / 10.0 To D:
Here is a problem that uses an array:
Store and process the race times of the finalists in a 100 m sprint, so that
the winner's time is output.
Using the reference language, the solution would be written as follows:
DECLARE allTimes INITIALLY [ 10.23, 9.9, 10.34
DECLARE fastest Time INITIALLY allTim
FOR BACH time FROM allTimes DO
IP fastestTime > time THEN
September 2018, version 1.0 3SET fastestTime TO time
END
END FOR EACH
SEND "Ihe winner's time was:" & fastestTime T0 DISPLAY
Possibly the only slightly new aspect to this code is the FOR EACH iterator,
which iterates over anything that is a collection of values, lke an array. Itis
therefore a generalisation of the kind of FOR loop found in most languages,
which can iterate over a sequence of integers only. Increasingly, modern
programming languages have the FOR EACH style of iterator.
The final example shows how code can be presented in relation to a graphical
environment, with a library of graphical procedures/functions/subroutines.
We are working in a graphical context and have an array of sprites
(graphical objects) we have already created, declared as follows:
DECLARE n
The following subroutines are defined to work on sprites:
getColour: returns the colour of the sprite parameter as a string
‘move: moves the sprite in the direction and distance specified
Write code to move those objects in the sprites array that are red up by a
distance 0.5.
Using the reference language, the solution would be written as follows:
OR EACH sprite FROM sprites DO
IF getColour( sprite ) = “red” THEN
move( sprite, "up", 0.5 )
END IF
END FOR EACH
Note that in the above solution, some of the detail is left out. For example, itis not
clear exactly how the frog, cow, and kangaroo are created, but this shouldn't
matter. It is expected that candidates will have had experience of this kind of
concept using the concrete languages with which they are learning to program.
So the concept of graphical objects, and of subprograms that operate over them,
shouldn't be new.
In summary, the purpose here is to show that solutions to problems presented
Using the clearly-defined reference language do not look radically different from
other pseudocodes used for assessment. The aim here is simply to ensure that
all parties, particularly exam setters and candidates, are using the same
reference language, remembering that there is a formal definition that should be
adhered to.
September 2018, version 1.0 4The full specification defined in the following sections may look lengthy, but that
is what is required if any language is to be specified accurately. Itis a testament
to how much anyone learning a programming language has implicitly picked up,
even if they can't articulate all the pieces!
Remember, candidates are never going to be expected to write this reference
language, only to be able to read and understand it.
September 2018, version 1.03. Specification
3.4 Types
Types is a major modelling tool forthe development of programs, enabling the
structure of the data manipulated to be cleariy specified. The type system of a
language typically contains both base types, such as integers and Booleans, and
structured types, such as arrays and records.
The reference language is typed — that is, all values in the language have a type
associated with them — but types need not be exposed if obvious from context.
The base types and their values are:
INTEGER —: -big.... + big, where bigs arbitrary
REAL : -[Link]... + [Link], where big and small are arbitrary
BOOLEAN _ : true, false
CHARACTER : ‘character’
The structured types are:
ARRAY _ finite length sequence of same type
STRING : ARRAY OF CHARACTER
RECORD : collection of labelled, typed values
CLASS __: used in OO programming (see seSction 3.8)
Note that STRING is really just a specialisation of ARRAY.
(UAB) A 2-0" array is an ARRAY OF ARRAYS (see section 3.4)
Structured type values may be denoted explicitly as:
# [ valuet, value2, ... 1 for ARRAY
@ “"characteri character? ..." for STRING
For example:
# (true, false, true, true ]
is an ARRAY holding four BOOLEANS
0142/34 1, (5/67/81, ( 9/10/11,12 1 1
is an ARRAY of ARRAY OF INTEGER, which
might be described as a "2-D array” of 3 “rows”
and 4 “columns”
¢ "Hello, this is a message"
is a STRING
¢ (name = "Fred", age = 42)
is a RECORD with two fields name and age of
types STRING and INTEGER respectively, and
with values "Fred" and 42
September 2018, version 1.0 6Record types can be named as shown below, for a record type that holds the
same information as in the example above:
@ RECORD Person
RING name, INTEGER age )
and values can then be constructed using the new record name. For example:
@ Person( "Fred", 42)
creates a value that is equivalent to the record example given above.
The empty string is "
the empty array is []; the empty record is
Types may be specified explicitly in variable declarations if necessary
(see section 3.1)
‘Types must appear in the definitions of subprogram formal parameters and
RECORD and CLASS fields. The type names are:
¢ INTEGER, REAL, BOOLEAN, CHARACTER, STRING
@ ARRAY OF type where type can be any type name
oid where iclis the name of a CLASS or RECORD
3.2 System entities
‘System entities include:
¢ DISPLAY the default window or console out
@ KEYBOARD the default textbox or console in
3.3 Identifiers
Identifiers are the usual sequences of letters and digits and “_" starting with a
letter. They cannot include . or -, and should not be all uppercase, to avoid
confusion with reserved words. Examples are:
@ myValue
* My va
# counter2
3.4. Commands
Commands include:
variable introduction and assignment
‘command sequences
conditions
repetitions and iterations
subprogram calls
September 2016, version 1.0Variable introduction and assignment
Variables are introduced, or declared, explicitly and must be initialised with
value, using the syntax
DECLARE id AS type INITIALLY value
or
DECLARE id INITIALLY value
The type of the variable need not be provided if it can be inferred from the
initialising value; if this is not possible, then the type must be provided explicitly
Note that, in questions, fragments of code may be used that omit variable
declarations, as long as the nature of those variables is thoroughly described in
the question preamble, Examples are given below:
Base types
# DECLARE counter INITIALLY 0
creates a counter variable, initialised to 0
@ DECLARE a INITIALLY b
creates variable a, initialised to the value associated with variable b
¢ DECLARE x AS INTEGER INITIALLY 0
type not essential but given for clarity
Arrays
oD)
WARE, someVala INITIALLY [ 1, 2, 3
creates someVals initialised to an array
¢ DECLARE myVals AS ARRAY OF INTEGER INITTALLY
type must be given: it cannot be inferred from the initialising empty array
@ DECLARE maze AS ARRAY OF ARRAY OF INTEGER I
introduces an emply "2-D array” of integers
NITIALLY [1
To Initialise the "2-D” array above with (say) 9 "rows" and 4 “columns” of zeroes.
‘would require the following code:
SET maze TO [ [| ) * 9 #arraywith 9 elements,
# each an empty array
FOR count FROM 0 70 8 DO
# update element to be a 4-element array of zeros
T maze[ count ] TO [0] * 4
END FOR
Note the use of the shorthand for creating large array values (see section 3.5), eg
[0] * 4 creates an array with four elements, all set to zero.
September 2016, version 1.0 8However, in a question, it would be acceptable to use any of the following or
similar alternatives:
¢ DECLARE maze AS ARRAY OF ARRAY OF INTEGER INITIALLY
© DECLARE maze AS ARRAY OF ARRAY OF INTEGER INITIALLY <9-
element array, each containing a 4-element array, all set to zero>
or, describe the array in the question preamble, for example ‘Assume a 2D array
named maze which has 9 columns and 4 rows, with all elements set to zero.’ and
then have:
¢ DECLARE maze AS ARRAY OF ARRAY 0:
described above>
INTEGER INITIALLY
¢ greater than or equal: 2
The logical operators are:
‘¢ conjunction: AND
¢ disjunction: OR
¢ negation: NOT
Expressions may be bracketed by (...).
The precedence rules are as follows:
Unary minus
*,,MOD
comparison operators
NOT
AND
OR
‘Where operators are of the same precedence, they are evaluated left-to-right.
STRINGS and ARRAYS may be concatenated using the 5 operator, and their
length found using the standard subprogram length. For example:
September 2018, version 1.0 147 myLength TO Length( "Quintin" ¢ "Cutts" )
If one of the arguments used with the « operator is of type string, then the other
argument will be coerced to a string. For example:
SEND "Number " @ 3 70 DISPLAY
will result in Wumbe= 3 being output
For convenience, the * operator can be used in place of repeated concatenation.
For example:
DECLARE myArray AS ARRAY OF INTEGER INITIALLY [ 0 ] * 20
creates a new array variable, myArray, and assigns to it a new array value
containing 20 elements all set to zero. The expression after INITIALLY is,
shorthand for the expression [0)&(0)(0]s(0)& .. «(0J&(0], with twenty
[018 and nineteen «s
Items are selected from structured types as follows:
¢ Both ARRAY and STRING types may be accessed by:
= idl index 1
¢ Indexing for both ARRAY and STRING starts from zero, unless otherwise
stated,
¢ Fields in record types may be accessed using dot notation as follows:
— id. fieldname
where fioldname is one of the valid field names in the record type.
Indexing outside the bounds of an array or string value is an error.
3.6 Comments and elisions
Both comments and olisions enable natural language to be mixed with the formal
reference language, in a well-defined manner.
Comments
These operate in the same way as in most other languages, with an initial
comment character (in this case #), followed by text up to the end of the line.
Comments are used to clarify, query or explain to a human reader the purpose of
nearby constructs. The comment character and text do not form part of the
computation being described. For example:
September 2016, version 1.0 8# Declarations for the program come next
DECLARE myAge INITIALLY 21 # Can 21 really be true?
Typographically, the comment text is presented in non-fixed-width font, to
the appearance of natural language, compared to formal program text which is
always presented in Courier fixed-width font, Where a programming language
entity is referred to from within a comment, itis presented in fixed-width font. For
example, see the use of 21 in the second comment above.
Elisions
Since this reference language is primarily to be used for the presentation of code
in exam contexts, there is a need to be able to avoid unnecessary detail in the
code fragments shown to candidates. That is, the specification of some parts of a
program may be left for further refinement, by using the following notation.
For example:
SET myArray TO
FOR EACH number FROM myArray DO
END FOR EAC!
could be used as an example in a question that asked candidates to explain
precisely how the FOR EACH construct worked. The code rigorously specifies
the creation of a variable myArray and the framework of a FOR EACH loop
iterating over myArray, but does not specify the precise detail of the array itself
and the action to be performed on the elements of the array inside the loop body.
Typographically, the elision text is presented in the same way as comments.
Note that this construct brings the flexibility of a pseudocode into the domain of a
rigorously defined language. All code written outside instances of the <...>
construct must adhere absolutely to the language definition, Only inside the <...>
can English-lke writing be used.
The <...> construct can be used instead of any command or expression.
3.7 _Input/output (including file operations)
Values can be read in from input devices and files and stored in variables, array
elements, and record | class fields. For example, reading an integer from the
keyboard into an integer variable my nt that has already been declared:
RECEIVE myInt FROM
BOARD
Similarly for array elements and record fields:
September 2018, version 1.0 16VE myArrayOfNumbers[ 4 ] FROM EYBOARD
RECEIVE [Link] FROM KEYBOARD
Currently, K2YBOAaD is the only defined input device. If writing example code
using another kind of device, eg a sensor, then elision should be used, for
example:
The type of the value read from the input deviceffile is inferred from the variable /
array element / record field type.
A file can be specified using a file name, path, or URL, represented as a STRING.
Examples are’
+ “myNumbers. txt"
ly/dataFile. txt"
y/personal/[Link]"
+ "eile: //july.
‘tp: //[Link]-org/[Link]”
[Link]
For convenience, declaring a variable and initialising it to a value from an input
device or file can be combined ina single DECLARE. statement. For example:
DECLARE userInput a
RING INITIALLY FROM KEYBOARD
Output values can be appended to an output device or file. For example, sending
an integer, variable or the result of an expression to the screen:
SEND 4 70 DISPLAY
myVariable 70 DISPLAY
SEND "Tne new v:
eis " & newValue TO DISPLAY
Currently, Dr SLAY is the only defined output device. As for input, if another kind
of device is to be used in example code, eg a controller, then elision should be
used, for example:
+ Afile can be specified in the same way as for input, as above.
When working with files that have already been created, they must be opened
and closed, for example:
OPEN “myNumbe: em
CLOSE “myNumbers.. txt!
When a file does not exist for output, it can be created using:
CREATE "myUpdatedtur
September 2016, version 1.0 7For example, the following is a complete sequence opening and reading two lines
from one file and creating and writing the lines to a second file, and finally closing
both files:
DECLARE sqaData INITIALLY "[Link]"
OPEN sqaData
DECLARE data AS STRING INITIALLY FROM sqaData
CREATE “[Link]"
SEND data 70 “outputFile. txt"
RECEIVE data PROM sqaData
SEND data 70 “outputFile.t
E “[Link]"
E sgabata
cLosi
cLos!
3.8 Object-oriented (00) programming
Records
Records provide the ability to aggregate name:value pairs (fields) into a single
value accessible for read and update using a dot notation
RECORD
{ STRING name, INTEGER age }
(wovintin", 47)
Classes, objects, instance variables and methods
As the scale of programs increases, mechanisms are required to partition a
program so as to limit the extent by which one section of the program can
manipulate data in another section of the program. Using abstract data types, or
object orientation, the program is partitioned according to aggregations of related
data (like records) and the associated operations over those aggregations, The
partitioned data can only be accessed directly by the associated operations. This
is encapsulation,
When viewing an object as an extension to a record instance, access to the data
items in the record instance is restricted to a defined set of operations, or
methods — these are just subprograms (procedures and functions) as outlined
above. Code elsewhere in the program can only access the data items in an
object via the set of operations defined for that object, and not directly, as in the
case of a local variable or a record field
Just as the type/structure of a record requires a definition in the program, so the
type structure of an object, in terms of both the data items and the valid
operations, needs a definition — a class definition.
September 2016, version 1.0 8In a record definition, the data items are referred to as fields. Many names are
used for the data items in a class definition, such as attribute, property and
instance variable. Instance variable is used, as itis the most general — values for
each of the data items exist in an instance of the class, and since the data items
have a name and are updatable, like a variable, the name instance variable
makes sense.
In 00 languages generally, instance variables can optionally be hidden from
external view, only being accessible to the defined operations. This allows
aspects of the underlying implementation of the class to be hidden, a key aspect
of large system engineering
In this reference language, all instance variables are inaccessible/invisible
outside the methods that are defined within the class. This simplifies the
language, removing the need for access modifiers such as Java's private,
protected and public keywords.
A class can be defined as follows:
CLASS Person I$ { STRING name, INTEGER age }
METHODS
PROCEDURE introduce (}
# Note the use of THIS to access the object on
# which this procedure has been invoked.
SEND "Hello, my name is " &
[Link] TO D
etAge () RETURNS INTEGER
RETURN THIS. age
END FUNCTION
END CLASS
Note the consistency with records in the form of procedures or functions.
[Link] model for a learner is to consider an object to be a record with
associated functionality.
Also, as is typical in OO languages, the predefined name THIS is used to access
the object on which a method has been invoked.
From a minimalist point of view (although see below for extensions), we do not
require a constructor function explicitly. Instead, the class name can be used as
with records:
DE
RE qu.
on ( "Ou:
and a method is selected in just the same way as a record field, and then invoked
just as any subprogram would be
gquintin. introduce (}
September 2018, version 1.0 19You cannot write the following lines because of the encapsulation restricting
access to the data elements:
gage INITIAL
LLY [Link]
[Link] TO 48
The same instance variable and method names may be used across different
Classes, as their scope is restricted to the class in which they are defined only.
Method overioading, where many methods within a single class definition have
the same name but with different parameter lists, is not permitted.
Inheritance
A class can be extended with additional data elements and behaviour. This is
reflected in the syntax as shown below:
CLASS Employee INHERITS Person WITH ( IN
GER empID }
METHODS
FUNC
N getID() RETURNS INTEGER
RETURN THIS
END FUNCTION
END CLASS
All instance variables and methods in the superclass are accessible to the code
in any new subclass methods, as well as newly-added instance variables and
methods.
Avalue of a subclass can be created by using the subclass name and all the
data elements of the superclass, followed by the data elements of the subclass.
For example:
SET aWorker TO Employee ( “Fr:
SEND "This employee's 1D is " & aWorker.
TSPLAY
» 18, 1401234 )
et TD() TO
A superclass variable may be assigned to a value of a subclass, For example:
DECLARE aPerson TI
SET aPerson TO aliorker
This is an example of polymorphism, since the superclass variable may be
associated with objects of many different subclasses, although only the methods
associated with the superclass may be applied to those values. This is
Particularly useful when working over a collection of values of different
subclasses, but with a common superclass, as in the following:
DECLARE myPeople AS ARRAY OF Person INITIAL
guintin, aWorker
FOR EACH guy FROM myPeople DO
guy. introduce ()
BND FOR EACH
September 2016, version 1.0 20Without the polymorphism supported by inheritance, we would not be able to
create an array with values of two different types (given that in the reference
language, all values in an array must be of the same type).
Overriding a method in a subclass
Amethod may be declared ina subclass with the same name as a method
already existing in a superclass. The new method is said to override the
superclass method. Such overriding is noted explicitly as follows:
CLASS Student INHERITS Person WITE
{ ARRAY OF STRING courses }
METHODS
OVERRIDE PROCEDURE
SEND "K my name Le" 6
END CLASS
Constructor functions
Constructor functions enable the initial values of instance variables to be set,
‘without exposing the particular implementation of those instance variables and/or
having to explcily provide an initial value for every variable. For example:
tudent INHERITS Person WITE
(ARRAY
METHODS
consrRucTOR ( §
DECLARE,
DECLARE,
DECLARE,
END CONSTRUCTOR
END CLASS
In this example, a student can now only be created via the constructor function,
which hides the original implicit constructor:
DECLARE aStudent INITIALLY Student( "Hazel", 18 )
Only one constructor may appear in a class definition. If a constructor is included
then it must contain dectarations for all the class's instance variables, including
those of all superclasses.
September 2016, version 1.0 24 Further resources
‘A checker run-time system and full formal specification of the
language are available at
hitp://[Link],com/haggisparser,htmI?variant=hiaher
September 2018, version 1.0
2