AS Level · Topic 11.1
A-Level Computer Science 9618 / Topic 11 / AS

Name it, store it, use it.

Every program starts with data: variables that change, constants that do not, and expressions that combine them. Learn to write each statement exactly as the 9618 pseudocode guide does, turn a flowchart or structured English into pseudocode, and use the built-in functions the exam gives you.

The key idea

A variable is a named memory location whose value can change while the program runs; declare it with DECLARE Name : TYPE and give it a value with the assignment operator ←. A constant is declared once with CONSTANT Name = value, using a literal only, and can never be changed. Expressions combine values with arithmetic operators (+ - * / DIV MOD), relational operators and the logical operators AND, OR, NOT. Built-in functions and library routines are pre-written, pre-tested subroutines such as LENGTH, MID and INT.

each flowchart symbol becomes one pseudocode statement STARTINPUT Celsius F ← Celsius * 9 / 5 + 32OUTPUT FSTOP DECLARE Celsius : REAL DECLARE F : REAL INPUT Celsius F ← Celsius * 9 / 5 + 32 OUTPUT F START and STOP need no statement; declarations come first Celsius = 20 gives F = 68
Fig. 1: converting a program flowchart into pseudocode, one symbol at a time
Section 01

Declare, initialise, assign.

Exam questions give you a design as a program flowchart or as structured English and ask for pseudocode. Work through it one step at a time: declarations and constants first, then one statement for each input, process and output.

CONSTANT TicketPrice = 8.50
CONSTANT BookingFee = 1.20
DECLARE Tickets : INTEGER
DECLARE Cost : REAL

OUTPUT "How many tickets?"
INPUT Tickets
Cost ← Tickets * TicketPrice + BookingFee
OUTPUT "Total to pay: ", Cost
Worked example Structured English to pseudocode
Convert this design into pseudocode. 1. SET the hourly rate to 12.50 (it never changes). 2. INPUT the hours worked. 3. CALCULATE the pay as hours multiplied by the rate. 4. OUTPUT the pay. Then state the output when 6 hours are entered.
  1. Step 1 is a value that never changes, so it is a constant: CONSTANT HourlyRate = 12.50.
  2. Declare the variables with suitable types: DECLARE Hours : INTEGER and DECLARE Pay : REAL.
  3. Steps 2 and 3: INPUT Hours then Pay ← Hours * HourlyRate.
  4. Step 4: OUTPUT "Pay: ", Pay. With 6 hours, 6 × 12.50 = 75.00.
Answer:
CONSTANT HourlyRate = 12.50
DECLARE Hours : INTEGER
DECLARE Pay : REAL
INPUT Hours
Pay ← Hours * HourlyRate
OUTPUT "Pay: ", Pay
With 6 hours the output is Pay: 75.00 (many languages show it as 75.0).
Your turn
With the ticket program above, what is output when 4 is input?
Examiner trap

CONSTANT VAT = Price * 0.2 is not allowed: a constant must be a literal. And a constant can never appear on the left of ← later in the program.

Section 02

Operators and expressions.

An expression is anything that evaluates to a single value. Know exactly what each operator returns, because examiners love asking for the value of an expression or its data type.

OperatorMeaningExample (A = 17, B = 5)Result
+ − *add, subtract, multiplyA + B * 227 (* before +)
/division: always a REAL resultA / B3.4
DIVinteger division: the whole-number quotientA DIV B3
MODthe remainder after integer divisionA MOD B2
> < >= <= = <>relational: the result is BOOLEANA <> BTRUE
AND, OR, NOTlogical: BOOLEAN in, BOOLEAN out(A > 15) AND NOT (B = 5)FALSE
Worked example Evaluate step by step
Price = 40, Qty = 3, Member = TRUE. Evaluate (Price * Qty > 100) AND NOT Member, and (Price * Qty) MOD 7.
  1. Price * Qty = 120, and 120 > 100 is TRUE.
  2. NOT Member = NOT TRUE = FALSE.
  3. TRUE AND FALSE = FALSE.
  4. 120 MOD 7: 7 × 17 = 119, so the remainder is 1.
Answer: FALSE, and 1
Your turn
Seconds holds 437. Write two assignment statements that set Minutes and Secs so that 437 seconds is shown as 7 minutes 17 seconds.
Examiner trap

17 / 5 is 3.4, not 3. If a question says the result must be an INTEGER, the mark is for DIV (or INT), not /.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

What is the value of 23 MOD 4?

Quick check+10 XP

Which is a valid constant declaration in 9618 pseudocode?

Quick check+10 XP

What does MID("COMPUTER", 4, 3) return?

Quick check+10 XP

In the lab, the program that reverses a word is given "stressed". What does it output?

Section 03

Built-in functions.

Built-in functions and library routines are subroutines that are already written, tested and ready to use: they save time and reduce errors. String functions are always given in the exam paper, but you should recognise these from the 9618 guide. Try the lab's reverse-a-word program (choose MID and a STEP -1 loop) to watch MID pick out one character at a time.

FunctionReturnsExample
LENGTH(ThisString)INTEGER: number of charactersLENGTH("Lucid STEM") returns 10
MID(ThisString, x, y)STRING of length y starting at position xMID("PROGRAM", 4, 3) returns "GRA"
RIGHT(ThisString, x)STRING: the rightmost x charactersRIGHT("PROGRAM", 3) returns "RAM"
UCASE(ThisChar), LCASE(ThisChar)CHAR in upper or lower caseUCASE('q') returns 'Q'
INT(x)INTEGER: the integer part of xINT(7.9) returns 7
RAND(x)REAL from 0 up to, but not including, xRAND(6) may return 4.27
// a random whole number from 1 to 6
Dice ← INT(RAND(6)) + 1
// join strings with &
Message ← "You rolled " & "a dice"
Worked example Build a username
A username is the first three letters of a surname followed by the last two digits of the year of birth, both stored as strings. Write the statement, and give the username for Surname = "Okafor" and BirthYear = "2008".
  1. The first three letters start at position 1 and are 3 long: MID(Surname, 1, 3) gives "Oka".
  2. The last two characters of the year: RIGHT(BirthYear, 2) gives "08".
  3. Join them with the concatenation operator &.
  4. UserName ← MID(Surname, 1, 3) & RIGHT(BirthYear, 2).
Answer: UserName ← MID(Surname, 1, 3) & RIGHT(BirthYear, 2), giving "Oka08"
Your turn
Explain why INT(RAND(6)) + 1 always gives a whole number from 1 to 6.
Examiner trap

In the 9618 guide UCASE and LCASE take a single CHAR, and string positions start at 1. To change a whole string, loop through it with MID one character at a time.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

Which expression always gives a random whole number from 1 to 10 inclusive?

Exam style+20 XP

What is the data type of the result of 9 / 3 in 9618 pseudocode?

Exam style+20 XP

Age holds 19. What is the value of (Age >= 13) AND NOT (Age > 19)?

Exam style+20 XP

Give the best reason for declaring the VAT rate as a constant rather than writing 0.2 throughout the program.

Skill unlocked

Variables, constants and built-in functions, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Declaring and initialising variables and constants, evaluating expressions with DIV, MOD and logical operators, converting designs to pseudocode and using built-in string and numeric functions.