Declarative vs Imperative Programming — Explained with Examples
Declarative programming expresses the logic of a computation without describing its control flow — you state what you want, not how to get it. Imperative programming explicitly describes the steps the computer must take to achieve a result — you write how to do it step-by-step.
SQL is the classic declarative language: SELECT * FROM users WHERE age > 18 describes the desired result, not the algorithm to find it. C and Python are typically imperative: loops, conditionals, assignments spell out each step. Most real-world programs mix both styles. HTML and CSS are declarative (describe what the page should look like). Functional programming leans declarative; OOP and procedural programming lean imperative.
Real-world analogy. Declarative is ordering a “cheese pizza” from a restaurant (specifying what you want). Imperative is writing the recipe yourself: “Take 500g flour, add water, knead for 10 minutes, spread sauce, grate cheese, bake at 220°C for 15 minutes” (specifying how).
Example (Declarative vs Imperative — finding even numbers):
# Imperative (how)
result = []
for i in range(10):
if i % 2 == 0:
result.append(i)
# Declarative (what) — Python list comprehension
result = [i for i in range(10) if i % 2 == 0]Example (SQL — entirely declarative):
SELECT department, AVG(salary)
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;Related terms: Functional Programming, OOP, Procedural Programming, Data-Driven Programming, Infrastructure as Code
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro