Middle
What is declarative programming, and what are examples like HTML and SQL?
sobes.tech AI
Answer from AI
Declarative programming is a paradigm where the desired result is described, rather than the sequence of steps to achieve it. The programmer tells the system what needs to be done, not how.
Examples:
-
HTML (HyperText Markup Language): A markup language that describes the structure and content of a web page. You define elements (headings, paragraphs, links), and the browser interprets this markup for display.
<!-- Level one heading --> <h1>Hello World!</h1> <!-- Regular paragraph --> <p>This is an example of a declarative approach in HTML.</p> -
SQL (Structured Query Language): A language for managing relational databases. You describe what data you want to retrieve or modify, and the database management system (DBMS) determines the optimal way to execute the query.
-- Select all records from the users table -- where age is greater than 18 SELECT * FROM users WHERE age > 18;
Key differences from imperative programming (which describes step-by-step algorithms):
| Criterion | Declarative Programming | Imperative Programming |
|---|---|---|
| Focus | What to do | How to do |
| Description | Desired result | Sequence of operations |
| Flexibility | DBMS/interpreter determines the method | Programmer determines the method |
| Examples | SQL, HTML, CSS, Prolog | Java, C++, Python (to a large extent), JavaScript |