Reference languageReference language examples

It is important to read and understand a formal language. Declaring variables and data types, using selection statements, iteration and using operators are comprehensions required before coding.

Part ofComputing ScienceSoftware design and development

Reference language examples

A range of examples are used to exemplify the structure of reference language.

Declaring and initialising a variable

DECLARE score INITIALLY 0

This means that a variable called score has been declared (created) and its initial value has been set to 0.

It is also possible to declare and initialise the values of an array.

DECLARE allLengths INITIALLY [ 10.1, 21.2, 11.3, 9.4, 19.8, 26.5, 9.3, 14.2, 28.2, 2.6, 12.6 ]

Initialising the value of a variable is an important element of programming. When declaring a variable in reference language, it is always necessary to initialise the value.

It may also be necessary to provide the data type when initialising, particularly if the type cannot be clearly spotted from the context. The examples shown above may also be stated as:

DECLARE score AS INTEGER INITIALLY 0
DECLARE allLengths AS ARRAY OF REAL INITIALLY [ 10.1, 21.2, 11.3, 9.4, 19.8, 26.5, 9.3, 14.2, 28.2, 2.6, 12.6 ]

Using the form shown above there is no ambiguity about the data type being used.

It is also possible to initialise an array using the following:

DECLARE sampleArray AS ARRAY OF INTEGER INITIALLY [ 0 ] * 10

This would create an array variable called sampleArray and would set the value as 0 for all ten elements in the array, without the need for individual initialisation of each element.

Assigning a value to a variable

SET score TO score + 1

In this case the value of the variable score is set to the current value of score plus one.

SET city TO “Glasgow”

When assigning a string value it is necessary to include inverted commas/speech marks as shown.

Assigning values to an Array

SET cities TO [“Glasgow”, “Edinburgh”, “Dundee”, “Stirling”, “Perth”, “Inverness”, “Aberdeen”]

The values shown within the square brackets would populate the array called cities.

Receiving User Input from a device

RECEIVE score FROM (INTEGER) KEYBOARD

This would receive a score from the user and store the score that they enter via the keyboard as an integer.

RECEIVE firstName FROM (STRING) KEYBOARD

This would receive a first name from the user and store the first name that they enter via the keyboard as string data.

RECEIVE initial FROM (CHARACTER) KEYBOARD

This would receive an initial (single letter) from the user and store the initial that they enter via the keyboard as a single character.

Sending Output to the display

SEND score TO DISPLAY

This would send the value held within the score variable to the screen, so if score held the number 21 the screen would show the number 21.

SEND “Please try again!” TO DISPLAY

This would display on screen:

Please try again!