Declarative vs Imperative Programming
Programming paradigms describe different ways of expressing a solution. Imperative programming tells the computer how to perform a task through explicit steps and state changes. Declarative programming focuses on what result or relationship is required.
Learning Objective
- 12.5.1.1 Compare declarative and imperative programming languages
Success Criteria
I can identify imperative and declarative characteristics · explain HOW vs WHAT · identify sequence, selection and iteration · classify code examples · compare two solutions to the same problem · justify which characteristics are suitable for a given scenario.
Conceptual Anchor
The Restaurant Analogy
Imperative: give the chef explicit instructions:
“Boil the water, add pasta, cook it, drain it, add sauce.”
Declarative: describe the desired result:
“I want pasta bolognese.”
Imperative — HOW?
The programmer specifies the sequence of actions that should be executed.
EXPLICIT STEPS + CONTROL FLOW1. Start with an empty result list. 2. Read the first student. 3. Check the student's age. 4. If age > 16, add the student. 5. Move to the next student. 6. Repeat until all students are checked.
Another Analogy: Navigation
Imperative: “Turn right, drive 300 m, turn left, stop at the second
building.”
Declarative: “Take me to the school.”
Imperative Programming
Imperative programming describes a computation as a sequence of commands that change the program's state. The programmer explicitly controls what happens and in what order.
- Explicit sequence of instructions.
- Variables commonly change value during execution.
- Uses control structures such as selection and iteration.
- The programmer describes the procedure used to obtain the result.
Three Core Control Structures
Sequence
Instructions are executed in order.
x = 5
y = x + 2
print(y)
Selection
A condition chooses which path is executed.
if x > 0:
print("positive")
else:
print("not positive")
Iteration
A block of instructions is repeated.
for i in range(5):
print(i)
1 Imperative Example — Python
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
evens = []
for n in numbers:
if n % 2 == 0:
evens.append(n)
print(evens)
The programmer explicitly creates state (evens), loops through
the data, tests each value and updates the list.
Declarative Programming
Declarative programming expresses the desired result, relationship or rule without explicitly describing the complete sequence of control-flow steps used to obtain it.
- Focuses on the required result or logical relationship.
- The execution mechanism decides many procedural details.
- Commonly uses queries, expressions, facts or rules.
- The programmer usually specifies less explicit control flow.
2 Declarative Example — SQL
SELECT value
FROM numbers
WHERE value % 2 = 0;The query states what rows are required. It does not specify the search algorithm, loop structure or exact execution plan. The database management system decides how to execute the query.
3 Declarative Logic — Prolog
grandparent(X, Z) :-
parent(X, Y),
parent(Y, Z).
The rule describes the logical relationship that makes
X a grandparent of Z. It does not provide
a step-by-step loop for searching the knowledge base.
Paradigm ≠ Language
Important idea
A programming language does not always belong to exactly one paradigm. Many modern languages support several programming styles. It is more accurate to discuss how a language or piece of code is commonly used.
multi-paradigm
Frequently used imperatively, but also supports object-oriented and functional-style constructs.
mainly imperative
Explicit statements, state changes, loops and conditions are central.
declarative query language
Describes the result set required from a database.
declarative logic programming
Describes facts, rules and queries rather than a conventional control-flow algorithm.
What about HTML and CSS?
HTML and CSS are often described as declarative technologies because they describe structure and presentation rather than a step-by-step algorithm. However, HTML itself is a markup language rather than a general-purpose programming language.
Comparison Matrix
| Feature | Imperative | Declarative |
|---|---|---|
| Main focus | HOW to perform the task | WHAT result or relationship is required |
| Control flow | Usually specified explicitly by the programmer | Often handled by the execution system |
| State | Explicit state changes are common | Usually less emphasis on explicit step-by-step state updates |
| Typical constructs | Assignments, loops, conditions, sequences | Queries, expressions, facts, rules |
| Examples | Imperative Python/C++ code | SQL queries, Prolog rules |
| Responsibility | Programmer specifies much of the execution procedure | System determines many details of how the result is produced |
Same Problem — Two Styles
Both examples below solve the same problem: find all students whose age is greater than 16. The difference is how responsibility is divided between programmer and execution system.
Imperative — Python
The programmer describes the procedure.
result = []
for student in students:
if student["age"] > 16:
result.append(student)
print(result)
- Explicit iteration.
- Explicit condition.
- Explicit state update with
append().
Declarative — SQL
The programmer describes the required result.
SELECT * FROM students WHERE age > 16;
- No explicit loop is written.
- The required condition is declared.
- The DBMS chooses an execution plan.
Exam-quality comparison
Do not write only “Python is imperative and SQL is declarative”. Compare the characteristics: explicit control flow, state changes, procedure, desired result and responsibility of the execution system.
Imperative or Declarative?
Classify each example. Do not rely only on the language name — look for explicit steps/state changes versus a description of the required result.
When Are These Characteristics Useful?
Detailed Algorithmic Control
When a programmer needs explicit control over sequence, conditions, loops and state changes.
Often associated with imperative solutions.Database Query
When the programmer wants to specify which data is required while the DBMS chooses how to retrieve it.
Declarative SQL is well suited to this pattern.Rule-Based Knowledge
When a problem can be expressed using logical relationships, facts and rules.
Declarative logic programming is a natural fit.Common Misconceptions
1. “Declarative means no algorithm exists”
An algorithm still runs underneath. The difference is that the programmer usually does not explicitly describe every control-flow step.
2. “Every language belongs to only one paradigm”
Many languages are multi-paradigm. Classify the programming style or construct being used rather than relying only on the language name.
3. “SQL is imperative because the DBMS performs steps”
SQL is declarative because the query specifies the desired data, while the database engine decides the detailed execution strategy.
4. “Declarative means there are no variables”
Declarative systems can use variables. Prolog variables, for example, represent values that can be matched during logical inference.
Where This Topic Leads Next
Practice Tasks
Define imperative programming and declarative programming.
Explain the phrase HOW vs WHAT in the context of programming paradigms.
Explain the difference between sequence, selection and iteration.
Classify six short code fragments as imperative or declarative and justify each answer using programming characteristics.
Identify sequence, selection and iteration in a short Python program.
Compare the Python and SQL examples that find students older than 16. Give at least three differences.
Explain why SQL is considered declarative even though the database engine still executes algorithms internally.
For each scenario — detailed algorithmic control, database query, rule-based knowledge system — explain which paradigm characteristics are useful and why.
Exam Practice
Q1: State one difference between imperative and declarative programming. [2]
Reveal marking points
Q2: Explain why a SQL SELECT query is an example of declarative programming. [2]
Reveal marking points
Q3: Describe two features of imperative programming. [2]
Reveal marking points
Q4: Compare an imperative and a declarative solution to the same data-filtering problem. [4]
Reveal marking points
Self-Check Quiz
Q1: What is the main focus of imperative programming?
Q2: What is the main focus of declarative programming?
Q3: What are the three core imperative control structures?
Q4: Why is SQL considered declarative?
Q5: Why is Prolog considered declarative?
Q6: Can one programming language support several paradigms?
Exit Ticket — 3–2–1
Write three characteristics of imperative programming.
Write two characteristics of declarative programming.
Give one clear HOW vs WHAT example of your own.