Haggis Pseudo Code
Haggis Pseudo Code
&
Version 1
Reading &Writing Haggis Pseudocode
Contents
Page 1 How to use this booklet.
Page 2 What is Haggis?
Page 3 Formatting Rules of Haggis.
Page 4 Assigning Values to Variable
Expressions to Output Data
Page 5 Expressions Using Arithmetic Operators
Expressions to Concatenate Strings
Page 6 Selection Constructs Using Simple Conditions and Logical Operators
Selection Constructs Using Complex Conditions and Logical Operators
Page 7 Iteration and Repetition Using Fixed Loops
Page 8 Iteration and Repetition Using Conditional Loops
Pre-Defined Functions with Parameters
Page 9 A Few Worked Examples
What is Haggis?
Definition - Haggis is a standardised design methodology used by the
Scottish Qualification Authority (SQA) in place of a programming
language for the purpose of asking coding questions in assessments
or exams.
Haggis is very similar to a programming language in that is has strictly defined syntax and rules. The
inflexibility of Haggis syntax is not a usual feature of pseudocode as users would usually write
pseudocode algorithms in natural language. This inflexibility is a necessary evil as the purpose of Haggis
to set a standard across Scotland and therefore ensure that both staff and pupils are well prepared for exam
questions.
This guide will help staff prepare their pupils for the new exams by explaining the ins and outs of Haggis
syntax in reference to the following sections:
Ÿ Assigning values to variables
Ÿ Expressions to output data
Ÿ Expressions using arithmetic operators
Ÿ Expressions to concatenate strings
Ÿ Selection constructs including simple/complex conditions and logical operators
Ÿ Iteration and Repetition using fixed and conditional loops
Ÿ Pre-defined functions with parameters
Note that Haggis syntax only applies to the final refinement of a problem. In your pseudocode’s main
algorithm you should outline a problem that requires further refinement by using < >.
For example, the algorithm below shows two completed “Haggis” lines and four lines that require further
refinement.
Line 1 RECEIVE numberOfItems FROM (INTEGER) KEYBOARD
Line 2 <Calculate the total cost of purchases>
Line 3 <Get valid type of customer>
Line 4 SET vatTotal TO 0.175*totalCost
Line 5 <Calculate final cost>
Line 6 <Display purchase details>
This guide may also be given to pupils as a reference document to help them interpret pseudocode.
It is important to note that pupils will never be expected to write Haggis code in an exam. They will
always be given the option “using pseudocode or a programming language of your choice” when
answering coding questions.
SDD 2
Reading &Writing Haggis Pseudocode
2. Line Numbers
Haggis uses a numbering system for lines of code and refinements. Lines should be numbered as
shown below using a capital L and a single space before each number.
Line 1
Line 2
Line 3
3. Indentation
The beginning and end of some constructs (REPEAT..UNTIL, IF..END IF) should be highlighted
by indenting the code between. For example,
Line 1 REPEAT
Line 2 SET total = total + 5
Line 3 UNTIL total =100
4. Variable Names
Simple variable names (one word) should be written in lower case. For example,
total
surname
Where the user wishes to use a longer variable name (two or more words) the second word should
be emphasised with a capital letter. For example,
firstName
secondNumber
5. Data
Where a numeric value is used in Haggis the number on its own is enough.
SET number TO 973
The command words SET & TO are used to assign values to variables.
Variables may also be assigned values as part of an input statement - RECEIVE & FROM
Variable name
The syntax notes the variable type The final part of the receive line states the device
being received as input: that the input is being received from:
CHARACTER KEYBOARD
STRING MOUSE
INTEGER SENSOR
REAL TOUCHSCREEN
BOOLEAN WEB CAM
MICROPHONE
Note, due to the level of programming at N4/N5,
the majority of questions will probably receive
simple input from a keyboard.
The command words SEND & TO are used to output data to a variety of devices. A few examples are
listed below.
Numeric SEND 23 TO DISPLAY
Variable SEND total TO DISPLAY
String SEND “The total is:” TO DISPLAY
Alternative SEND on TO MOTOR
SEND “print this text” TO PRINTER
SDD 4
Reading &Writing Haggis Pseudocode
Text, number & variables may be concatenated with an ampersand (&) and enclosed by [ ] brackets:
SET errorMessage TO [“Device failed due to fault number” & faultNumber]
SEND [“Device failed due to fault number” & faultNumber] TO DISPLAY
SEND [“Player” & playerNumber & “’s score is” & playerScore] TO TOUCHSCREEN
Extended Statement
IF temperatureNow >= 100 THEN
SEND “Water is in a gaseous form at this temp” TO DISPLAY
SEND “This is called steam” TO DISPLAY
END IF
An ELSE statement may be used to show what should take place should a condition be false.
IF temperatureNow >= 100 THEN
SEND “Water is in a gaseous form at this temp” TO DISPLAY
SEND “This is called steam” TO DISPLAY
ELSE
SEND “Water is a liquid or a solid at this temp” TO DISPLAY
END IF
Logical operators (AND, OR, NOT) may be used to create complex conditions
IF temperatureNow > 0 AND temperatureNow < 100 THEN
SEND “Water is in a liquid at this temp” TO DISPLAY
SEND “Lower the temperature to form a solid” TO DISPLAY
END IF
IF temperatureNow <= 0 OR temperatureNow >= 100 THEN SEND “Water is not a liquid at this
temp” TO DISPLAY
SDD 6
Reading &Writing Haggis Pseudocode
Code may be repeated a fixed number of times using FOR, FROM, TO, DO, END FOR.
FOR counter FROM 1 to 10 DO
GET nextInput FROM (REAL) KEYBOARD
SET totalCost TO totalCost + nextInput
END FOR
The variable used in the loop (“counter” in the above example) may also be used in the pseudocode. A
STEP may also be introduced (for example - STEP 2) to control how the loop counts.
The example below stores 10 names in an array in reverse order.
FOR counter FROM 1 to 10 STEP -1 DO
SEND [“Please enter the name of user number” & counter] TO DISPLAY
RECEIVE nextUserName FROM (STRING) KEYBOARD
SET nameList[counter] TO nextUserName
END FOR
Alternatively, a fixed loop may also be written using the REPEAT, TIMES, END REPEAT commands:
REPEAT 10 TIMES
RECEIVE nextValue FROM (REAL) KEYBOARD
END REPEAT
Additionally a FOR EACH loop may be used for structures with a set length,
FOR EACH character FROM “This is a test”
IF <character does not equal a letter of the alphabet> THEN
SEND [character & “ is not a letter”] TO DISPLAY
END IF
END FOR EACH
or
FOR EACH time FROM runnersTimesArray
SET totalTimes TO totalTimes + time
END FOR EACH
SET averageTime TO totalTimes / numberOfRunners
Loops may also be ended with a pre or post condition using WHILE, END WHILE or REPEAT,
UNTIL. These are formatted as shown below:
Pre-condition
RECEIVE pressureLevel FROM (REAL) SENSOR
WHILE pressureLevel < 0 OR pressureLevel > 200 DO
IF pressureLevel < 0 OR pressureLevel > 200 THEN
SEND “Error in Reading. Reset Sensor!” TO DISPLAY
END IF
RECEIVE pressureLevel FROM (REAL) SENSOR
END WHILE
Post-condition
REPEAT
RECEIVE pressureLevel FROM (REAL) SENSOR
IF pressureLevel < 0 OR pressureLevel > 200 THEN
SEND “Error in Reading. Reset Sensor!” TO DISPLAY
END IF
UNTIL pressureLevel >= 0 AND pressureLevel <= 200
Predefined functions may differ from language to language. As yet the only mentions of pre-defined
functions in N5 are length() in the SQA Haggis document and move(), rotate() from the graphical question
in the specimen paper.
A function or sub-program is noted by using brackets ( ).
If an item of data or a variable is passed into the function it is placed inside the brackets.
MOVE(5)
ROTATE(90)
SET lengthOfWord TO length(“computing”)
SET temperature TO currentTemp(“Dining Room”)
SET AsciiCode TO ord(character)
SET maxValue TO findMax(shoesizeList[ ])
In some cases the brackets may be left blank. This may be relevant to calling a sub-program.
GetValidName()
SDD 8
Reading &Writing Haggis Pseudocode
The output from the program should display the total cost of purchases, the type of customer, the amount
of VAT to be paid and the final cost e.g.
Total cost of purchases: 2.38
Type of customer: S
VAT: 0.42
Final Cost: 2.80
Line 5.1 IF validGuess < chosenNumber THEN SEND “My number is bigger than your guess.” TO
DISPLAY
Line 5.2 IF validGuess > chosenNumber THEN SEND “My number is smaller than your guess.” TO
DISPLAY
SDD 10
Reading &Writing Haggis Pseudocode