PROGRAMMING PARADIGM
A programming paradigm (model) is a way of organizing and designing a code to solve
problems. It contains some set of rules for writing programs.
1. Imperative programming paradigm:
It mainly focuses on how to achieve the goal. It performs step by step task by
changing state.
Ex: 100 people were sitting and need to find age above 18
a. Write down name,age
b. Count age>=18
c. Result=60
Example, "Do you fall into the following age groups: under 18, 18-30, 31-50, over
50?"
Create a form with a question asking if the person is over 18. For example, “Are you
over 18? (Yes/No)
Ex: C ,C++,PYTHON,JAVA
2. Declarative programming paradigm:
Declarative programming is a method where you define what you want to
achieve without specifying how to achieve it.
Result=60.
Ex: HTML
<img src=”c:/image.jpg”/>
It tells the browser to display an image. It does not bother about how it loads.
Declarative Query Languages: SQL, developed in the early 1970s for
relational databases, allows users to describe what data they want rather than
how to retrieve it.
3. Object oriented paradigm:
It is a way of designing and organizing code using objects.
Example: In an e-commerce platform, OOP models entities like products,
customers, and orders as objects, each with specific attributes and methods.
Product Class: Methods might include apply_discount()
Customer Class: Methods might include update_address()
Order Class: Methods might include calculate_total()
Basic concepts of oops:
1. Object:
Anything in the real world is called as object. Each object has its own
attributes
Ex: Ball Attributes: colour, size
Computer Attributes:
2. Class:
A collection of objects is called as class.It specifies what attributes (data)
and behaviors (methods) the objects will have.
For example, a Car class might define attributes like color and model, and
methods like drive() and brake()
3. Data abstraction:
It involves hiding the complex implementation details and exposing only
the essential features and operations to the user.
Ex: In bank you can see deposit money, withdraw money etc. But you
cannot see the underlying process such as how account is managed or
how transactions are recorded.
4. Data encapsulation:
An object contains both data and method together and only exposing
whats necessary.
Ex: Balance is kept private and it is not accessed from outside the class.
5. Inheritance:
Inheriting one class to another class is called as inheritance.
Ex: sports car is inherit from car.
6. Polymorphism:
It allows objects of different classes to be treated as objects of common
super class.
Super class: shape
Base class: circle, Rectangle.
7. Dynamic binding: (Late Binding)
The methods or functions to be executed at runtime rather than compile
time.
8. Message passing:
It tells how objects communicate and interact with each other.
4. Functional programming paradigm:
It performs the computation through the evaluation of mathematical function
and avoids changing the state.
Example:
Text Search Function
The original document remains unchanged. The search function scans the
document and produces a list of matches or locations where the word appears.
5. Structured programming:
A method to write clear, logical, and organized code.
Focuses on breaking down a program into modules.
Principles:
Sequence: Code executes one line after another.
Selection: Make decisions using conditionals.
Iteration: Repeat code using loops.
Control Structures:
Sequence:
int a = 5;
int b = 10;
int sum = a + b; // Executes in order
Selection:
if (a > b) {
printf("a is greater than b\n");
} else {
printf("a is not greater than b\n");
}
Iteration:
// Using a for loop
for (int i = 0; i < 5; i++) {
printf("Iteration %d\n", i);
}
GOTO statement is not used in structured programming because , when you
jump from one part of the code to another, it can be difficult to understand how the
code is executed and how different parts of the program are related.
6. Logical programming:
Logical Programming Basics
1. Concept:
o Logical Programming is about defining rules and facts, then asking questions
to find answers based on those rules and facts.
2. Components:
o Facts: Simple statements about objects (e.g., parent(john, mary). means John is
a parent of Mary).
o Rules: Conditional statements that describe relationships (e.g., sibling(X, Y) :-
parent(P, X), parent(P, Y). means X and Y are siblings if they share a parent).
3. Language: Prolog (used to write such programs). Prolog is powerful for
solving problems related to logic and relationships