Tutorial 1: Programming Paradigms: Imperative and Declarative Programming
Unit 3 ยท Section 1
Objectives
Compare imperative and declarative descriptions.
Recognize state, commands, expressions, and constraints.
Choose a paradigm that clarifies a problem.
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
Rewrite a loop as a SQL-style description.
Identify state changes in a program.
Compare readability and control trade-offs.
Self-check
What does imperative code emphasize?
What does declarative code emphasize?
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
Compare two solutions to a data transformation.
Explain which paradigm each uses.
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.