Tutorial 1: Programming Paradigms: Imperative and Declarative Programming

Unit 3 ยท Section 1

Objectives

Imperative programming describes how state changes through commands. Declarative programming describes what result or relationship is wanted, leaving execution strategy to a system. SQL is largely declarative; a loop that updates a counter is imperative. Most practical systems combine paradigms.

imperative: total = 0; for each item, total += item

declarative: SELECT SUM(amount) FROM payments;

Exercises

  1. Rewrite a loop as a SQL-style description.
  2. Identify state changes in a program.
  3. Compare readability and control trade-offs.

Self-check

  1. What does imperative code emphasize?
  2. What does declarative code emphasize?
  3. Can a system combine both?

Self-Check Quiz

1. Which paradigm emphasizes commands and state changes?

AnswerImperative programming.

2. Is SQL purely imperative?

AnswerNo. SQL primarily declares the desired result or data relationship.

Homework

  1. Compare two solutions to a data transformation.
  2. Explain which paradigm each uses.
  3. Discuss one trade-off.
Sample answerA loop gives explicit control and can handle complex state; a query concisely declares filtering and aggregation for a database engine. The query may enable optimization while the loop offers broader procedural flexibility.