← AS/A Level

CS · Chapter 25

Cambridge AS & A Level · Part 4 · Chapter 25

Programming Paradigms

A paradigm is a style of programming: a way of thinking about a problem before you write a single line. This chapter compares four of them, low-level, imperative, object-oriented and declarative, and points you to the chapters where you program in each one. Every section, the reflection point and both tasks from the chapter are here, with a lab that solves one problem four ways.

Guru Jazzy
Chapter 25 opener: circuit traces glowing inside a ring of light

🎯 Learning objectives

By the end of this chapter you should be able to:

  • show understanding of what is meant by a programming paradigm
  • show understanding of the characteristics of a number of programming paradigms: low-level, imperative, object-oriented and declarative.

🧭 25.01 Programming paradigms

A programming paradigm is a fundamental style of programming. Each paradigm supports a different way of thinking about a problem and solving it.

Key term · Programming paradigm: a fundamental style of programming.
  • Paradigms are supported by programming language features.
  • Some languages support more than one paradigm. Python, for example, can be written imperatively or in an object-oriented way.
  • There are many paradigms, and some of them overlap. This chapter looks at just four.
1Low-level
2Imperative
3Object-oriented
4Declarative
Big idea: imperative code tells the computer how to get a result, step by step. Declarative code describes what the result should be, and leaves the “how” to the language.

💭 Reflection point

Why are there different paradigms?
Think about it before you open my notes.

  • Different problems suit different ways of thinking. Driving a sensor at the hardware level, processing records step by step, modelling a world of interacting things and querying a database are very different jobs.
  • Different levels of control. Low-level code gives exact control of memory and registers, but is slow to write and tied to one processor family. Higher-level paradigms trade some control for programs that are faster to write and easier to read.
  • Managing size and change. Object-oriented features (classes, encapsulation, inheritance) grew up to help teams build and maintain very large programs.
  • Some answers are easier to describe than to compute by hand. In SQL or Prolog you state what you want and the system searches for it.
  • History. Languages and hardware developed over time, and each new paradigm built on, rather than replaced, the earlier ones.

🧩 The four paradigms in this chapter

⚙️ Low-level

talks straight to the processor

Low-level languages let you manipulate the contents of memory addresses and registers directly and exploit the architecture of a given processor. You solve problems in a very different way from a high-level language.

Each type of processor has its own language. Processors in the same family share similar architectures and similar languages: the Intel family in many PCs uses the x86 instruction set.

assemblyx86machine code

See Chapter 6 and Chapter 28.

👣 Imperative

how: do this, then that

A program is a sequence of explicit steps executed by the processor. It tells the computer how to get the result, in contrast to declarative programming, which describes what the result should be.

The procedural paradigm belongs to the imperative paradigm. Most programs in this book are imperative.

PascalCBasic

See Chapters 12 to 15 and Chapters 23, 24 and 26.

📦 Object-oriented

objects send each other messages

Based on objects interacting with one another. Objects are data structures with associated methods.

Many imperative languages were developed further to support it: Pascal (as Delphi or Object Pascal) and Visual Basic (the .NET version was the first fully object-oriented one). Newer languages such as Python and Java were object-oriented from the beginning.

PythonJavaVB.NETDelphi

See Chapter 27.

🔎 Declarative

what: here is the problem, find the answer

The programmer writes down, in the language of logic, a specification of the situation: what the problem is. To get information they pose a query (set a goal), and the logic programming system works out how to answer it.

Programs are expressed as formal logic, and computations are deductions from the logic statements.

SQLProlog

See Chapter 11 (SQL) and Chapter 29 (Prolog).

Watch the wording: Pascal, VB and Python are called imperative programming languages because you write sequences of statements that say how to solve the problem. Being able to add classes on top does not stop them being imperative.

🆚 Side by side

ParadigmYou write…The basic unitTied to one processor?
Low-levelinstructions from the processor's instruction setan instruction acting on a register or addressYes (one processor family)
Imperativea sequence of statements that change dataa statement / procedureNo
Object-orientedclasses, then objects that call each other's methodsan object (data + methods)No
Declarativefacts and rules, then a query (goal)a relation / logic statementNo

🎮 Name that paradigm

Read the clue, then pick the paradigm it shows best.

🧪 One problem, four paradigms

The task is the same every time: add up three test marks (12, 15 and 9) and get the total, 36. Switch paradigm and compare what the programmer has to write.

The same total, four ways

Notice: as you move from low-level to declarative, the programmer writes less about how and more about what. The low-level version names exact memory locations; the SQL version never says how the adding is done.

📝 Tasks from the chapter

The book says to do these after you have studied Chapters 26 to 29.

Task 25.01

Draw a line between a statement on the left and its matching programming paradigm on the right. Here, choose the paradigm for each statement, then check.

StatementParadigm

Task 25.02

Task 25.02
For each of the four programming paradigms in this chapter, give one programming statement example that is characteristic for this paradigm. State the programming language you used for each example.

ParadigmLanguageExample statementWhy it is characteristic
Low-levelCambridge assembly languageLDD 200Loads the contents of address 200 straight into the accumulator: direct use of memory and a register.
ImperativePythontotal = total + markAn explicit step that changes the value of a variable.
Object-orientedPythonplayer.move(3)Calls a method of an object; the object's data is changed only through its own methods.
DeclarativePrologcapitalCity(paris).A fact. You then pose a goal such as ?- capitalCity(X). and the system finds the answers.

Other good answers: MOV EAX, 5 (x86), FOR i ← 1 TO 10 (pseudocode), class Car(Vehicle): (Python inheritance), SELECT Name FROM Student WHERE Age > 16; (SQL).

📌 Recap

  • A programming paradigm is a fundamental style of programming; each supports a different way of thinking and problem solving.
  • Low-level: direct use of memory addresses and registers; each processor (family) has its own language, e.g. x86.
  • Imperative: a sequence of explicit steps saying how; procedural programming belongs here.
  • Object-oriented: objects (data structures with associated methods) interacting with one another.
  • Declarative: describe what the problem is in logic, then pose a query; e.g. SQL and Prolog.

🃏 Key-term flashcards

Tap a card to flip it.

✅ Quick quiz

My own questions on the chapter.

Guru Jazzy · Cambridge AS & A Level Computer Science · Chapter 25 Programming Paradigms · Figure and tasks from the Cambridge coursebook (Langfield & Duddell) · Notes, examples, lab and quiz are my own