Skip to content
Cambridge AS & A Level9618

Programming Basics

Computer Science 9618 Chapter Notes

Last updated November 2026

ShareWhatsAppPost

Programming Basics notes as a printable PDF.

Read online free. Download a watermarked copy with a free account.

Read the notes

The full Programming Basics notes as text: skim, search, and jump between subtopics.

~17 min read

1. From a design to pseudocode

A design is given as a program flowchart or as structured English. Pseudocode writes the same steps in the Cambridge style: keywords in capitals, `←` for assignment, and indented blocks.

A function earns marks for its function header, with the parameters and the `RETURNS` type, and for the declaration of required local variable(s). It then needs the calculation, a `RETURN` so that the result is returned, and the closing `ENDFUNCTION`. A procedure closes with `ENDPROCEDURE`.

A value that the design passes in is a parameter. It is not read with `INPUT` inside the subroutine. `INPUT` and `OUTPUT` belong to the main program.

Examiner insight

“A procedure header was usually included, but often the parameter or the corresponding ENDPROCEDURE statement was missing (MP1).” “Some omitted the ENDFUNCTION statement and so did not gain this mark.”

Worked example 14 marks

Electric light bulbs are advertised online at 1.75 dollars each or 20 bulbs for 30.00 dollars.

A pseudocode function `BulbCost()` takes the quantity of bulbs purchased as its single parameter and returns the total cost in dollars.

Write pseudocode for the function `BulbCost()`.

  1. 1

    Function header with a single parameter and the return type, ended by `ENDFUNCTION`: `FUNCTION BulbCost(Quantity : INTEGER) RETURNS REAL` [1]

  2. 2

    Declaration of the local variables needed: `DECLARE Cost : REAL`, `DECLARE Twenty : INTEGER`, `DECLARE Other : INTEGER` [1]

  3. 3

    Multiples of 20 and the other bulbs found with `DIV` and `MOD`: `Twenty ← Quantity DIV 20`, `Other ← Quantity MOD 20`, `Cost ← (Twenty * 30) + (Other * 1.75)` [1]

  4. 4

    Cost is returned: `RETURN Cost` [1]

  5. 5

    ``` FUNCTION BulbCost(Quantity : INTEGER) RETURNS REAL DECLARE Cost : REAL DECLARE Twenty : INTEGER DECLARE Other : INTEGER Twenty ← Quantity DIV 20 Other ← Quantity MOD 20 Cost ← (Twenty * 30) + (Other * 1.75) RETURN Cost ENDFUNCTION ```

2. Declaring constants and variables

A variable is declared before it is used, with its data type. Variables of one type may share a declaration. A counter or total is initialised before the loop that uses it.

A constant is declared once, with its value.

``` DECLARE Count, Total : INTEGER CONSTANT SPACECHAR = ' ' Count ← 0 ```

Use a constant for a value such as a tax rate or a postal cost that appears at several places. The value is entered once only, it avoids input error or changing it accidentally, and it is easier to maintain when the value changes.

Examiner insight

“The use of a literal value together with the statement that the calculation was performed at various points in the program proved insufficient signposting for the majority of candidates, who failed to recognise the requirement for the use of a constant.” “Very few three-mark answers were seen, suggesting that the use of constants is not widely appreciated.”

Worked example 14 marks

A program calculates the postal cost based on the weight of the item and its destination. Calculations occur at various points in the program and these result in the choice of several possible postal costs. The programmer has built these postal costs into the program.

For example, the postal cost of 3.75 dollars is used in the following lines of pseudocode:

``` IF Weight < 250 AND ValidAddress = TRUE THEN ItemPostalCost ← 3.75 // set postal cost for item to 3.75 dollars ItemStatus ← "Valid" // item can be sent ENDIF ```

(i) Identify a more appropriate way of representing the postal costs.

(ii) Describe the advantages of your answer to part(i) with reference to this program.

  1. 1

    (i) Use constants [1]

  2. 2

    (ii) Postal rates are entered once only [1]

  3. 3

    Avoids input error / changing the cost accidentally, or avoids different values for postal rates at different points in the program [1]

  4. 4

    When required, the constant representing the postal rate is changed once only, so the program is easier to maintain [1]

  5. 5

    A fourth point also earns credit: it makes the program easier to understand. The maximum for part(ii) is 3.

3. Data types and identifiers

Every variable has a data type. Match the type to the value it holds: `3` is `INTEGER`, `15.75` is `REAL`, `"Mr Khan"` is `STRING`, `TRUE` is `BOOLEAN` and `19/02/1983` is `DATE`. `CHAR` is used for a single character. A division such as `Total1 / Total2` gives a `REAL`.

The identifier is the variable's name. It must be meaningful: it says what the variable stores, as in `CustomerName` or `NumItems`. Names such as `A`, `B`, `flag` or `name` make the program more difficult to understand, debug and maintain.

`Date` is the name of a data type and so a keyword: it cannot be the name.

Examiner insight

“One of the most frequent errors was the use of a less than meaningful variable name.” “Occasional errors included suggesting real for Index and string for Holiday.”

Worked example 14 marks

A shop sells car accessories. A customer order is created if an item cannot be supplied from current stock. A program is being developed to create and manage the customer orders.

Complete the identifier table by adding meaningful variable names and appropriate data types.

Example valueExplanationVariable nameData type
"Mr Khan"The name of the customer
3The number of items in the order
TRUETo indicate whether this is a new customer
15.75The deposit paid by the customer
  1. 1

    `CustomerName`, `STRING` [1]

  2. 2

    `NumItems`, `INTEGER` [1]

  3. 3

    `NewCustomer`, `BOOLEAN` [1]

  4. 4

    `Deposit`, `REAL` [1]

4. Assignment

An assignment gives a variable a value. It is written `variable ← expression`. The right-hand side may be a value, another variable, an expression or a function call.

The arrow assigns. The `=` sign compares: `UNTIL NextInput = "END"` is correct and `UNTIL NextInput ← "END"` is an error.

Write the statement only. Do not add a data type or any part of a function header after it.

Examiner insight

“Fewer correct answers than might be expected for this pseudocode assignment statement, as the insert included an example of what was required.” “A small number of answers suggested the use of string functions rather than date functions to extract the month from MyDob.”

Worked example 13 marks

A program stores a user's date of birth using a variable `MyDOB` of type `DATE`.

(a) Write a pseudocode statement, using a function from the insert, to assign the value corresponding to 17/11/2007 to `MyDOB`.

(b) `MyDOB` has been assigned a valid value representing the user's date of birth. Write a pseudocode statement to calculate the number of months from the month of the user's birth until the end of the year and to assign this to the variable `NumMonths`. For example, if `MyDOB` contains a value representing 02/07/2008, the value 5 would be assigned to `NumMonths`.

  1. 1

    (a) `MyDOB ← SETDATE(17, 11, 2007)`: the date function with the day, month and year, assigned with `←` [1]

  2. 2

    (b) The month is taken from the date with the date function `MONTH(MyDOB)` [1]

  3. 3

    `NumMonths ← 12 - MONTH(MyDOB)`. For 02/07/2008: 12 - 7 = 5 [1]

5. Operators and expressions

The arithmetic operators are `+`, `-`, `*`, `/`, `DIV` and `MOD`. `/` can give a `REAL`. `DIV` counts whole groups and `MOD` gives what is left: `Quantity DIV 20` counts the groups of 20 and `Quantity MOD 20` is the remainder. `ThisInt MOD 10 = 7` tests for a last digit of 7.

The logical operators `AND`, `OR` and `NOT` join conditions that are Boolean values. Each side needs its own comparison: `x = 3 OR x = 4`, not `x = 3 OR 4`.

`&` joins strings. `+` cannot add a string to a number. A string value is written with its quotation marks.

Examiner insight

“The most common error was the omission of quotation marks to indicate the string value "Cat".” “Data type errors were seen in many cases. For example, attempting to concatenate an integer with a string or using the + operator in place of & (MP3).”

Worked example 14 marks

The variables have these initial data values.

Evaluate each expression in the following list by using the initial data values.

``` Modified OR Index > 100 LENGTH("Student: " & Name) INT(Index + 2.9) MID(Name, 1, 3) ```

VariableInitial data value
Name"Catherine"
Index100
ModifiedFALSE
  1. 1

    `Modified OR Index > 100`: `FALSE` OR (100 > 100, which is `FALSE`), so FALSE [1]

  2. 2

    `LENGTH("Student: " & Name)`: 9 characters joined to 9 characters, so 18 [1]

  3. 3

    `INT(Index + 2.9)`: `INT(102.9)` is 102 [1]

  4. 4

    `MID(Name, 1, 3)` is "Cat", with the quotation marks and a capital C [1]

6. Input and output

`INPUT` reads a value from the keyboard into a variable. `OUTPUT` writes to the console, and each `OUTPUT` starts on a new line. A prompt is output first: `OUTPUT "How many bulbs? "` then `INPUT NumberPurchased`.

Separate the items of one output with commas: `OUTPUT "The total was ", Total`. A counter is initialised before the loop and its value is output after the loop.

A function returns its result with `RETURN`. It does not use `OUTPUT` for that.

Examiner insight

“A significant number of solutions attempted to INPUT the three parts of the expression.” “Most solutions ended with the output of a count variable, but in many cases, this had not been initialised before the loop.”

Worked example 15 marks

An algorithm will:

  • output each integer value between 100 and 200 that ends with the digit 7, for example, 107
  • output a final count of the number of values that are output.

Write pseudocode for this algorithm. Any variables used must be declared.

  1. 1

    Declare the loop variable and the counter as integers, and initialise the counter: `DECLARE ThisInt, Count : INTEGER` and `Count ← 0` [1]

  2. 2

    Loop from 100 to 200 with no step: `FOR ThisInt ← 100 TO 200` [1]

  3. 3

    Test the value in the loop: `IF ThisInt MOD 10 = 7 THEN` [1]

  4. 4

    Output the selected value and increment the counter in the loop: `OUTPUT ThisInt` and `Count ← Count + 1` [1]

  5. 5

    Output the counter after the loop: `OUTPUT Count` [1]

  6. 6

    ``` DECLARE ThisInt, Count : INTEGER Count ← 0 FOR ThisInt ← 100 TO 200 IF ThisInt MOD 10 = 7 THEN OUTPUT ThisInt Count ← Count + 1 ENDIF NEXT ThisInt OUTPUT Count ```

7. Built-in functions and library routines

A built-in function returns a value and is used in an expression. Use the functions in the insert as they are given. Functions not in the guide are provided in the question, and string manipulation functions are always given.

The mark schemes use `LENGTH`, `LEFT`, `RIGHT`, `MID`, `INT`, `STR_TO_NUM`, `NUM_TO_STR`, `IS_NUM`, `CHR` and the date functions `DAY`, `MONTH`, `YEAR`, `SETDATE` and `DAYINDEX`. Pass the right type: `IS_NUM` takes a string, so `IS_NUM(-23.4)` is wrong. `STR_TO_NUM("37" + "5")` uses the wrong operator: join the strings with `&`. `RIGHT(Description, 7)` of `"AB12345:Cleaning Brush (small)"` is `"(small)"`.

A library routine is a ready-made routine from a program library. It is tried and tested, so free from errors. It may perform a function the programmer cannot program alone, such as encryption, and it is readily available, which speeds up development.

Examiner insight

“The most common correct points given were that they are tried and tested so are free from errors and that they perform a function that you may not be able to program yourself.” “Vague answers such as ‘less mistakes can occur’ were commonplace and these did not gain credit.”

Worked example 13 marks

An algorithm includes a number of complex calculations. A programmer is writing a program to implement the algorithm and decides to use library routines to provide part of the solution.

State three possible benefits of using library routines in the development of the program.

  1. 1

    They are tried and tested, so free from errors [1]

  2. 2

    They perform a function that you may not be able to program yourself, for example encryption [1]

  3. 3

    They are readily available, so they speed up development time [1]

Recap

  • A design is written as pseudocode with a function header, parameters, local variables and `ENDFUNCTION` or `ENDPROCEDURE`.
  • `DECLARE Name : TYPE` declares a variable. `CONSTANT NAME = value` declares a constant. Initialise counters before the loop.
  • Use a constant for a value used at several places: it is entered once only and easier to maintain.
  • Types: `INTEGER`, `REAL`, `STRING`, `CHAR`, `BOOLEAN`, `DATE`. Names must be meaningful and must not be keywords.
  • `←` assigns and `=` compares. `&` joins strings. `DIV` and `MOD` give the whole groups and the remainder.
  • A string value keeps its quotation marks. `INPUT` reads from the keyboard, `OUTPUT` writes to the console, `RETURN` gives back a result.
  • Library routines are tried and tested, available at once, and can do what you cannot program yourself.

End-of-chapter exercise

Test yourself on the whole chapter. Work through these before moving on.

  1. State the keyword that declares a constant. Give one reason for using a constant for a tax rate that is used at several places in a program.2 marks
    Answer
    1. The keyword is `CONSTANT` [1].
    2. The rate is entered once only, so it is changed in one place when the rate changes [1].

    Trap: Typing the rate as a number at every place, which a change in the rate then misses.

  2. Give the most appropriate data type for each variable: (a) the number of items in a basket, example value 4; (b) the price of one item, example value 12.50; (c) whether a customer has a loyalty card.3 marks
    Answer
    1. (a)
    2. `INTEGER` [1].
    3. (b)
    4. `REAL` [1].
    5. (c)
    6. `BOOLEAN` [1].

    Trap: A price with a decimal part is not an `INTEGER`, and a yes or no value is not a `STRING`.

  3. Write pseudocode statements to declare an integer variable `Count`, to declare a constant `MAXITEMS` with the value 12, and to set `Count` to 0.3 marks
    Answer
    1. `DECLARE Count : INTEGER` [1]
    2. `CONSTANT MAXITEMS = 12` [1]
    3. `Count ← 0` [1]

    Trap: Writing `Count = 0`: the `=` sign compares and does not assign.

  4. The variables have these values: `Score` is 17, `Name` is "Priya" and `Passed` is TRUE. Evaluate `Score MOD 5`, `Score DIV 5`, `"Hi " & Name` and `Passed AND Score > 20`.4 marks
    Answer
    1. `Score MOD 5` is the remainder when 17 is divided by 5, which is 2 [1].
    2. `Score DIV 5` is the whole number of fives in 17, which is 3 [1].
    3. `"Hi " & Name` is `"Hi Priya"`, with the quotation marks [1].
    4. `Passed AND Score > 20`: TRUE AND FALSE, so `FALSE` [1].

    Trap: Swapping `DIV` and `MOD`, or leaving the quotation marks off the string.

  5. The variable `Code` holds "GZ4587". Evaluate `LENGTH(Code)`, `RIGHT(Code, 3)`, `MID(Code, 3, 2)` and `STR_TO_NUM(MID(Code, 3, 2)) * 2`.4 marks
    Answer
    1. `LENGTH(Code)` is 6 [1].
    2. `RIGHT(Code, 3)` is `"587"` [1].
    3. `MID(Code, 3, 2)` starts at the third character and takes two, so `"45"` [1].
    4. `STR_TO_NUM("45") * 2` is 90 [1].

    Trap: A string is not a number: `"45"` needs `STR_TO_NUM` before it is multiplied, and its quotation marks stay on a string answer.

  6. Each statement may contain an error. State the error, or write NO ERROR. `Count` is an `INTEGER`, `Score` is an `INTEGER`, `Total` and `Half` are `REAL` and `Message` is a `STRING`. ``` Message ← "Total: " + Count IF Score = 50 OR 60 THEN Total = 4.5 * Score Half ← INT(Total) / 2 ```4 marks
    Answer
    1. Statement 1: `+` cannot add a string to a number. Use `&` with `NUM_TO_STR(Count)` [1].
    2. Statement 2: `60` is not a Boolean value. Each side of the `OR` needs its own comparison, `Score = 60` [1].
    3. Statement 3: the assignment must use `←`, not `=` [1].
    4. Statement 4: NO ERROR [1].

    Trap: Writing ‘+ is used in calculations’, which does not refer to the operands, or marking statement 4 as an error.

  7. A function `Perimeter()` takes the lengths of two sides of a rectangle, both of type `REAL`, as its parameters. It returns the perimeter of the rectangle. Write pseudocode for the function `Perimeter()`.4 marks
    Answer
    1. `FUNCTION Perimeter(Side1, Side2 : REAL) RETURNS REAL` and `ENDFUNCTION` [1]
    2. `DECLARE Result : REAL` [1]
    3. `Result ← 2 * (Side1 + Side2)` [1]
    4. `RETURN Result` [1]

    Trap: Using `INPUT` for the sides or `OUTPUT` for the answer inside the function, instead of parameters and `RETURN`.

  8. Tickets cost 8 dollars each, or 28 dollars for a group of 4. A program reads the number of tickets wanted and outputs the total cost, using as many groups of 4 as possible. Write pseudocode for the program. Use constants for the prices and declare every variable.6 marks
    Answer
    1. ``` CONSTANT SINGLE = 8 CONSTANT GROUP = 28 DECLARE Tickets, Groups, Others, Cost : INTEGER INPUT Tickets Groups ← Tickets DIV 4 Others ← Tickets MOD 4 Cost ← (Groups * GROUP) + (Others * SINGLE) OUTPUT "The total cost is ", Cost ```
    2. Marks: constants [1], declarations [1], `INPUT` [1], `DIV` and `MOD` [1], cost [1], `OUTPUT` [1]. Check: 10 tickets is 2 groups and 2 singles, 56 + 16 = 72.

    Trap: `Tickets / 4` gives a `REAL`, not the whole number of groups, and the remainder is lost.

  9. A product code is two letters followed by a three-digit number from 100 to 989, such as "KT472". A program reads a code from the keyboard and outputs a new code. The letters are unchanged and 10 is added to the number. For the input "KT472" the output is "KT482". Write pseudocode for the program. Declare every variable.6 marks
    Answer
    1. ``` DECLARE Code, Letters, NewCode : STRING DECLARE NumPart : INTEGER INPUT Code Letters ← LEFT(Code, 2) NumPart ← STR_TO_NUM(RIGHT(Code, 3)) + 10 NewCode ← Letters & NUM_TO_STR(NumPart) OUTPUT NewCode ```
    2. Marks: declarations [1], `INPUT` [1], `LEFT` [1], `RIGHT` and `STR_TO_NUM` [1], `&` and `NUM_TO_STR` [1], `OUTPUT` [1]. Check: 472 + 10 = 482, output "KT482".

    Trap: Adding 10 to the string, or joining a number to a string with `+` instead of `&`.

  10. A car park charges 2.50 dollars for each complete hour and 0.05 dollars for each remaining minute. The design in structured English is: 1. Input the length of stay in minutes, a whole number. 2. Call the function `ParkingCost()` with the stay as its single parameter. It returns the cost. 3. Output one message of the form "2 h 15 min costs 5.75". Write pseudocode for the function `ParkingCost()` and for the main program. Use constants for the two rates and declare every variable.8 marks
    Answer
    1. ``` CONSTANT HOURRATE = 2.50 CONSTANT MINRATE = 0.05 FUNCTION ParkingCost(Stay : INTEGER) RETURNS REAL DECLARE Cost : REAL Cost ← ((Stay DIV 60) * HOURRATE) + ((Stay MOD 60) * MINRATE) RETURN Cost ENDFUNCTION DECLARE Mins : INTEGER DECLARE Msg : STRING INPUT Mins Msg ← NUM_TO_STR(Mins DIV 60) & " h " & NUM_TO_STR(Mins MOD 60) OUTPUT Msg, " min costs ", ParkingCost(Mins) ```
    2. Marks: constants [1], header, `RETURNS REAL`, `ENDFUNCTION` [1], local `Cost` [1], `DIV` and `MOD` [1], cost returned [1], declarations and `INPUT` [1], function called with `Mins` [1], `Msg` built with `&` and `NUM_TO_STR` and output [1]. Check: 135 is 2 h 15 min, 2 × 2.50 + 15 × 0.05 = 5.75.

    Trap: `INPUT` inside the function instead of the parameter, or `Stay / 60`, which gives 2.25, not 2 complete hours.

Say this, not that

Wording that loses the mark, against the wording the mark scheme credits.

Loses the markGets the mark
`Date` or `flag` as a variable namea name that says what it stores, such as `StartDate` with type `DATE`
`Cat` as the value of `MID(Name, 1, 3)``"Cat"`, with the quotation marks
`MyDob ← SETDATE(17, 11, 2007) RETURNS DATE``MyDOB ← SETDATE(17, 11, 2007)`, the statement only
‘+ is used in calculations’Can't add a string to a number: `"Done"` is not a number

Frequently asked questions

How do you write a function in pseudocode?
Write the header with the parameters and the RETURNS type, for example FUNCTION BulbCost(Quantity : INTEGER) RETURNS REAL. Declare the local variables, do the calculation, and send the result back with RETURN before the closing ENDFUNCTION. A procedure closes with ENDPROCEDURE instead.
When should I use a constant in pseudocode?
Use CONSTANT NAME = value for a value that appears at several places, such as a postal cost. The value is entered once only, so it avoids input error and changing it by accident. When the value changes, it is changed once only, so the program is easier to maintain.
What is the difference between = and ← in pseudocode?
← assigns a value to a variable, so Count ← 0 sets Count to zero. The = sign compares, so UNTIL NextInput = "END" is correct. Writing UNTIL NextInput ← "END" is an error. When writing an assignment, write the statement only, with no data type after it.
How do DIV and MOD work?
DIV counts whole groups and MOD gives the remainder. Quantity DIV 20 counts the groups of 20, and Quantity MOD 20 is what is left. The test ThisInt MOD 10 = 7 is TRUE for a number whose last digit is 7. The arithmetic operators are +, -, *, /, DIV and MOD.
Why does adding a string and a number give an error?
The + operator cannot add a string to a number. Join strings with &, and write a string value with its quotation marks. Convert a number to a string first, for example "Total: " & NUM_TO_STR(Count). Data type errors were seen in many cases, including using + in place of &.
What is a library routine and why use one?
A library routine is a ready-made routine from a program library. It is tried and tested, so it is free from errors. It may perform a function that you may not be able to program yourself, such as encryption. It is readily available, which speeds up development.

Free notes are just the start.

Unlock every Workbook and Chapter at a Glance, and generate your own worksheets and predicted papers.

Explore plans

Related chapters