0% found this document useful (0 votes)
109 views6 pages

Lecture Notes 2 (COS 201) Edited

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views6 pages

Lecture Notes 2 (COS 201) Edited

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lecture notes II (CPS 201)

What is a Programming Paradigm?

Programming Paradigm is a style, technique, or way of writing a program. We can also say

programming paradigms are the various approaches to solve a problem. And it is possible to solve

the same problem using different paradigms, just like there can be multiple routes to reach from

one village to another. However, certain types of problems fit more naturally to specific

paradigms.

Why should we study it?

As we know, Programming Paradigms are different styles to write a program, hence knowing

those styles and techniques will help us to choose the right paradigm based on the problem. I

think every programmer should know the different paradigms, as learning a language is like

learning the grammar or syntax of a particular language, but studying paradigms, on the other

hand, are like learning the very principles upon which the languages are built. For example, when

we learn the merge sorting, we don’t just learn the one more sorting technique. Instead, we learn

the divide-and-conquer paradigm and the places where we can use this paradigm.

Almost all high-level computer programming languages support to write a program in one or

multiple paradigms, and the decision to choose the right paradigm is left upon the programmer.

For example, now, assembly language has features of high-level languages. We can write a

program in assembly language in an object-oriented style.

There are two primary programming paradigms, an imperative and a declarative, and there are

several paradigms that are influenced by these two types. I am going to cover all the major
paradigms one by one. In this note, we are going to focus on both Imperative and Declarative

paradigms. Let’s begin:

A computer program is a list of instructions that can be executed by a central processing unit.

These instructions are in the machine language, in the binary form that is 0 and 1. But these

instructions are not in the human-readable format; hence it isn’t straightforward to understand,

which limits our ability to fix the bugs or modify the existing programs.

To overcome this problem, we created an assembly language that contained words from the

English language. We call them mnemonic. Assembly language uses these mnemonic to represent

each low-level machine instruction. Remember, the primary purpose of all programming

languages (including assembly) is to provide an abstraction to the underlying machine

architecture. The assembly language is easy to modify and understand in comparison to machine

language. For example, we can write the machine code 10110000 01100001 as MOV AL, 61h in

assembly, which means Load 97 decimal (61 hex) in AL register. MOV is an abbreviation of the

move.

MOV AL, 1h ; Load AL register with immediate value 1


CMP AL, DL ; Compare AL with DL
ADD AL, DL ; ADD AL and DL

In the above program, we are informing the computer what to do next and also instructing to
change its state (register values). This type of programming style is called the Imperative style of
Programming.

The roots of imperative programming are rooted deep in computer architecture. The hardware
implementation of almost all computers is imperative.
Imperative Paradigm

The imperative style of programming emphasis on explicit control flow, it means the order in

which statements, instructions, or function calls are executed. So the term explicit control

flow means the programmer itself explicitly defines the order of execution of the instruction. The

main characteristics of the Imperative style of programming are assignment statements and

global variables.

Assignment Statements:

Imperative programming style uses statements that change a program’s state. The program state
is nothing but the contents of the memory at a particular time. In computer programming, an

assignment statement sets or re-sets the value stored in the storage location(s) denoted by a

variable name. In most imperative programming languages like C, C++, Java, Kotlin, PHP, the

assignment statement is a fundamental construct. For example, in the below code snippet, we are

initializing the variables a, b, and total using the assignment operator (=).

val a :Int = 20
val b :Int = 30
var total:Int = a + b
System.out.println("Sum of $a and $b is $total") // op -> Sum of 20 and 30 is 50

Global Variable

A global variable is a variable with global scope. Scope refers to the visibility of variables. By

global scope, it means the variable is visible and accessible throughout the program.

In short, the steps and sequences are explicitly given in the imperative type of paradigm.

Several paradigms are influenced by the imperative paradigm, such as Structural Paradigm,
Procedural Paradigm, and Object-Oriented Paradigm. C, C++, Java, Kotlin, PHP are a few
examples of languages that support the imperative paradigm. I will cover these paradigms in
separate posts.

Declarative Paradigm
Declarative paradigm is a non-imperative style of programming. In the declarative programming
paradigm, we only tell the computer what the problem is and let the system decide what steps to
take and also the sequence of those steps. This behavior is a contrast to an imperative style where
we mention all the steps to solve the problem.

Imperative says how to do it, and declarative says what to do. In other words, declarative
programming expresses the logic of a computation without describing its control flow. Following
is the example of the declarative way of programming.

mysql> desc vegetables;

op ->
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(50) | YES | | NULL | |
| color | varchar(20) | YES | | NULL | |
| type | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+

mysql> select name from vegetables;


+----------+
| name |
+----------+
| Lettuce |
| Cabbages |
| Spinach |

In the above example, we use query select name from vegetables; as an instruction to get
information about the name of all vegetables, we didn’t tell the computer how to get the
information. Instead, we just described what we want and not how to get it. This type of
programming style is called as the declarative programming paradigm.
There are several paradigms and languages which are influenced by the declarative paradigm,
such as a Functional Paradigm, Logical Paradigm, and Database. Prolog, SQL are few examples
of languages that support the declarative paradigm.
Imperative vs. Declarative paradigms:

In the Imperative paradigm, the programmer instructs the machine on how to change its state. In

the declarative paradigm, the programmer merely declares properties of the desired result, but not

how to compute it.

We shall look into the other types of paradigms, which are the form of these two main paradigms.

One note on the languages, some languages support programming in multiple paradigms, for

example, high-level languages like Java, Kotlin supports writing program in both an Object-

Oriented and Functional way.

I have created the following diagram to help you to understand the classification of the paradigms.
Following are the few paradigms which are influenced by the imperative paradigms:

1. Structured Paradigm

2. Procedural Paradigm

3. Object-Oriented Paradigm

Structured Programming:

Structured programming is a programming paradigm that uses structured control flow(without

goto) to improve code clarity and quality.

Structured programming has three ways of combining programs — sequencing, selection, and

iteration.

This paradigm makes extensive use of the block structures (sequencing), if/else (selection),

and while or for loop(repetition).

You might also like