Teachers' Portal  /  A-Level Computer Science 9618  /  Further Programming  /  Low-level, imperative and declarative paradigms
Lesson pack · A-Level Computer Science 9618 · 20.1 · A Level (A2)

Low-level, imperative and declarative paradigms

Many ways to program. The same problem can be solved in very different styles.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Further Programming
Syllabus reference
Cambridge International AS and A Level Computer Science 9618, 20.1
Level
A Level (A2)
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 1 of 4 in Further Programming; leads into Object-oriented programming
Central model
Fig. 1: the four paradigms in the 9618 syllabus, each with a typical line of code
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • What is a paradigm?: A paradigm is not a language: it is a way of thinking about a program.
  • Low-level and imperative code: Low-level programs must handle every detail themselves, including where each item of data is.
  • Declarative: facts, rules and goals: A declarative program is a knowledge base.
Key points to leave on the board
  • Low-level: instruction set, registers, addresses; LDM immediate, LDD direct, LDI indirect, LDX indexed, relative = offset from current instruction.
  • Imperative: sequence of statements changing variables, structured with procedures and functions.
  • Declarative: facts and rules; a goal is satisfied by matching and backtracking. Constants lower case, variables upper case.
  • Trace goals in clause order and list every answer that backtracking finds.
The core visual model

One picture every idea returns to

A programming paradigm is a style or approach to programming, with its own concepts and way of structuring a solution. Low-level programs use the processor's instruction set directly, working with registers and memory addresses through addressing modes. Imperative (procedural) programs are sequences of statements that change the program's state (its variables), organised with sequence, selection, iteration and procedures and functions. Object-oriented programs are built from classes and objects. Declarative programs state facts and rules; the programmer gives a goal (query) and the language's inference engine searches the facts and rules to satisfy it. Imperative code says how; declarative code says what.

Four programming paradigms: four ways of telling a computer what to do Low-level Imperative (procedural) Object-oriented Declarative instructions for one processor: registers, addresses, addressing modes LDX 300 ADD TOTAL a sequence of statements that change variables, grouped into procedures Total ← Total + N classes bundle data with methods; objects are instances of classes S ← NEW Circle("C1", 2) facts and rules describe WHAT is true; the system finds HOW to meet a goal parent(X, Y) IF ... Imperative and low-level code say HOW, step by step; declarative code says WHAT
Fig. 1: the four paradigms in the 9618 syllabus, each with a typical line of code
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, Many ways to program.. The same problem can be solved in very different styles. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: What is a paradigm?A paradigm is not a language: it is a way of thinking about a program.Whole class, then pairs
13 to 21 minTeach and check: Low-level and imperative codeLow-level programs must handle every detail themselves, including where each item of data is. Project tab 6 of the lab (Addressing modes) and let a student drive it while the class predicts. Model the worked example Trace the low-level loop on the board, one line at a time. Your turn: Address 150 holds 210, address 151 holds 75, address 210 holds 42 and IX holds 1. What does each instruction load into ACC: LDM #150, LDD 150, LDI 150, LDX 150?Pairs
21 to 30 minTeach and check: Declarative: facts, rules and goalsA declarative program is a knowledge base. Model the worked example Write facts and rules from information on the board, one line at a time.Frayer Model
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 21 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in Declarative: facts, rules and goals, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Frayer Model

Pairs fill a four-box grid for the key term: definition, characteristics, examples and non-examples. The non-examples box is where the misconceptions surface, so give it the most time.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
Do not describe a paradigm by naming a language. "Prolog" earns nothing; "the program is a set of facts and rules, and a goal is satisfied by searching them" earns the mark.Return to What is a paradigm?: ask a pair to state the correct version and show it on the lab before moving on.
Variables must start with a capital letter and constants with a lower-case letter.Writing studies(Ana, physics) makes Ana a variable, so the clause would be true for anyone.
Worksheet

Low-level, imperative and declarative paradigms: worksheet

6 questions, 21 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · explain[3 marks]

Explain what is meant by a programming paradigm and describe the main characteristics of the imperative (procedural) paradigm.

Q2 · trace[4 marks]

Address 200 holds 204, address 201 holds 3, address 202 holds 8, address 203 holds 1 and address 204 holds 6. The index register holds 2. State the value in the accumulator after each instruction is executed separately:

LDM #201   LDD 201   LDI 200   LDX 201

Q3 · write[4 marks]

The following facts describe a family:

30 parent(rosa, tom).
31 parent(rosa, ivy).
32 parent(tom, max).
33 parent(ivy, zoe).

(a) Write a rule grandparent(X, Y) that is true if X is a grandparent of Y. (b) Write a rule sibling(X, Y) that is true if X and Y have the same parent and are different people. (c) State the result of the goal grandparent(rosa, Y).

Q4 · trace[3 marks]

Using the clauses in question 3, state the result of each goal: (a) parent(X, zoe) (b) sibling(tom, Y) (c) grandparent(tom, Y).

Q5 · explain[4 marks]

Compare the low-level and declarative paradigms.

Q6 · analysis[3 marks]

A company wants (a) a driver for a new sensor that must run in very little memory and (b) a system that advises which medicine to give from a set of symptoms. Suggest a suitable paradigm for each and justify your choices.

Answer key and marking notes
Q1 · 3 marks
  • A paradigm is a style or approach to programming / a way of structuring and thinking about a solution.
  • Imperative: a program is a sequence of statements / commands executed in order that change the program state (values of variables).
  • Code is structured using sequence, selection and iteration and divided into procedures and functions with parameters and local variables.
Q2 · 4 marks
  • LDM #201: 201 (immediate: the operand is the value).
  • LDD 201: 3 (direct: contents of 201).
  • LDI 200: 6 (indirect: 200 holds 204, contents of 204 is 6).
  • LDX 201: 1 (indexed: 201 + 2 = 203, contents of 203 is 1).
Q3 · 4 marks
  • (a) grandparent(X, Y) IF parent(X, Z) AND parent(Z, Y).
  • (b) sibling(X, Y) IF parent(P, X) AND parent(P, Y) AND NOT(X = Y).
  • (c) Y = max, zoe.
  • Correct use of IF / AND and capital letters for variables throughout.
Q4 · 3 marks
  • (a) X = ivy.
  • (b) Y = ivy (rosa is the parent of both tom and ivy; tom himself is excluded).
  • (c) false / no result: max has no children recorded, so tom is not a grandparent of anyone.
Q5 · 4 marks
  • Low-level code uses the processor's instruction set, so it is machine dependent; declarative code is independent of the processor.
  • Low-level code gives every step explicitly (how), working with registers, memory addresses and addressing modes.
  • Declarative code states facts and rules about the problem (what) and does not give the steps.
  • In declarative programming a goal is satisfied by the system searching the knowledge base and backtracking; in low-level code the programmer must write any search themselves.
Q6 · 3 marks
  • (a) Low-level: direct control of hardware registers and memory addresses; small, fast code.
  • (b) Declarative: the medical knowledge can be written as facts and rules.
  • A goal such as treatment(Patient, Medicine) is satisfied by the system searching the rules, so new knowledge can be added without rewriting the search.
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

Which statement describes the declarative programming paradigm?

  • AThe programmer states facts and rules, and the system searches them to satisfy a goal
  • BThe programmer writes a sequence of statements that change the values of variables
  • CThe programmer bundles attributes and methods together into classes
  • DThe programmer uses the instruction set and addressing modes of the processor
Q21 mark

Address 120 holds 135, address 121 holds 40, address 135 holds 9 and the index register IX holds 1. What does LDI 120 load into the accumulator?

  • A9
  • B135
  • C40
  • D120
Q31 mark

40 likes(amy, tea).
41 likes(bo, coffee).
42 likes(cy, tea).
43 likes(amy, juice).
What is the result of the goal likes(X, tea)?

  • AX = amy, cy
  • BX = amy
  • CX = tea
  • DX = amy, bo, cy
Q41 mark

A company needs a driver for a new sensor. It must control hardware registers directly and fit in a very small amount of memory. Which paradigm is most suitable?

  • ALow-level
  • BDeclarative
  • CObject-oriented
  • DImperative with a large library of procedures
Q51 mark

50 parent(ann, bob).
51 parent(ann, cat).
52 parent(bob, dan).
53 parent(cat, eve).
54 parent(cat, fay).
55 grandparent(X, Y) IF parent(X, Z) AND parent(Z, Y).
What is the result of the goal grandparent(ann, Y)?

  • AY = dan, eve, fay
  • BY = bob, cat
  • CY = dan
  • DY = dan, eve
Q61 mark

Addresses 300 to 303 hold 6, 3, 8 and 4.

        LDR #0
        LDM #0
        STO 310
        STO 311
LOOP:   LDX 300
        ADD 310
        STO 310
        INC IX
        LDD 311
        INC ACC
        STO 311
        CMP #3
        JPN LOOP
        END
What are the contents of address 310 when the program ends?

  • A17
  • B21
  • C9
  • D8
Quiz key
Q1: A

Declarative programs are a knowledge base of facts and rules; a goal is satisfied by the system matching and backtracking. A sequence of statements changing variables is imperative (procedural), classes with attributes and methods are object-oriented, and working with the instruction set and addressing modes is low-level programming.

Q2: A

LDI is indirect: address 120 holds the address 135, and the contents of 135 (9) are loaded. 135 is what LDD 120 (direct) would load, 40 is what LDX 120 would load (120 + IX = 121), and 120 is what the immediate instruction LDM #120 would load.

Q3: A

Clauses 40 and 42 match the goal, so X is instantiated to amy and then, after backtracking, to cy. Stopping at amy forgets backtracking, tea is the constant in the goal rather than the variable's value, and bo likes coffee, so clause 41 does not match.

Q4: A

Low-level code uses the processor's instruction set, so it can access hardware registers directly and produce very small, fast code. Declarative code states facts and rules and suits expert systems, object-oriented code suits large team projects, and relying on a large library would increase the memory used.

Q5: A

parent(ann, Z) gives Z = bob, then parent(bob, Y) gives Y = dan. Backtracking gives Z = cat, and parent(cat, Y) gives eve and then fay. bob and cat are ann's children, not grandchildren; stopping at dan or at dan, eve misses the later answers that backtracking finds.

Q6: A

The loop adds the value at 300 + IX on each pass and stops when the counter in 311 reaches 3, so it adds 6 + 3 + 8 = 17. 21 adds all four values, as if the loop ran four times; 9 stops after two passes; and 8 is just the last value loaded by LDX rather than the running total.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.