Unit I covers the basics of C programming, including programming paradigms, the structure of a C program, and various components such as data types, constants, and operators. It discusses imperative and declarative programming paradigms, detailing their characteristics, examples, and types, including procedural, object-oriented, functional, and logical programming. The document emphasizes the importance of understanding these paradigms for effective programming and problem-solving.
Unit I covers the basics of C programming, including programming paradigms, the structure of a C program, and various components such as data types, constants, and operators. It discusses imperative and declarative programming paradigms, detailing their characteristics, examples, and types, including procedural, object-oriented, functional, and logical programming. The document emphasizes the importance of understanding these paradigms for effective programming and problem-solving.
9/23/2023 1 Technology UNIT I Topics to be Covered: Introduction to programming paradigms Structure of C program C programming: Data Types Constants Keywords Operators Operator Precedence and Associativity Expressions Input/Output statements Assignment statements Decision making statements Switch statement Computer Centre - Madras Institute of 9/23/2023 2 Technology Topic of Discussion Introduction to programming paradigms Structure of C program C programming: Data Types Constants Keywords Operators Operator Precedence and Associativity Expressions Input/Output statements Assignment statements Decision making statements Switch statement
Computer Centre - Madras Institute of
9/23/2023 3 Technology What is a Programming Paradigm? • Programming paradigm is defined as a set of principles, ideas, design concepts and norms that defines the manner in which the program code is written and organized. • It is all about the writing style and organizing the program code in a specific way. • It does not specify the programming language syntax, it simply defines the program structure and set of principles that the programming language compiler should enforce. • Each paradigm advocates a specific way to organize the program code. • For example, the OOP paradigm represents everything in the form of objects, whereas the function programming represents the program as set of functions and in logic programming , a logic is created by series of symbols . Computer Centre - Madras Institute of 9/23/2023 4 Technology Programming Paradigm – Cont’d • Each programming language has unique programming style that implements a specific programming paradigm • If a programming language support or falls under a certain paradigm then this means it has some unique feature or it follows a certain approach that makes it best to solve a certain kind of problem. • For example: – Object-oriented programming is best suited for implementing real- world entities by hiding important data and finding methods with the entities. Example C++, Java, Python, etc. – Logical programming is best for creating logic by a complex structure of symbols. Example Prolog, F-logic, etc. • A good programming language should support multiple paradigms because different programming problems require a different way to approach. • Most modern languages like C++, C#, Python, Go, etc. support multi-paradigm languages Computer Centre - Madras Institute of 9/23/2023 5 Technology Types of Programming Paradigm
• The programming paradigms are categorized in multiple
categories yet most significant are only two: – Imperative programming paradigm – Declarative programming paradigm Computer Centre - Madras Institute of 9/23/2023 6 Technology Imperative programming paradigm • The word “imperative” comes from the Latin “impero” meaning “I command”. bake a cake • The imperative paradigm is said to be command driven. • In Imperative programming, the programmer has to elaborate each statement in detail. • Each statement directs what is to be done and how it is to be done. • In an imperative programming paradigm, the order of the steps is crucial • Fortran, Java , c++, c programming languages are examples of 9/23/2023 imperative Computer programming. Centre - Madras Institute of 7 Technology Imperative programming paradigm –Cont’d Find the sum of first ten natural numbers in the imperative paradigm approach. Example in C: #include <stdio.h> int main() { • In this example, we are int sum = 0; commanding the sum += 1; sum += 2; computer what to do line sum += 3; sum += 4; by line. sum += 5; sum += 6; • Finally, we are storing the sum += 7; sum += 8; value and printing it. sum += 9; sum += 10; printf("The sum is: %d\n", sum); return 0; } Output: Computer Centre - Madras Institute of The sum is: 55 9/23/2023 Technology 8 Types of imperative programming paradigms • Procedural Programming • Object-oriented Programming • Parallel Processing Approach
Computer Centre - Madras Institute of
9/23/2023 9 Technology Imperative programming paradigm Procedural Programming bake a cake • It is a programming paradigm based upon the concept of procedure calls. • Here, statements are structured into procedures (also known as subroutines or functions). • Procedures contain a series of computational commands that should be carried out to achieve a certain outcome. • Procedure call that lets to reuse the code. • Procedural programming languages are known as top-down languages. Computer Centre - Madras Institute of 9/23/2023 10 Technology Imperative programming paradigm Procedural Programming –Cont’d #include <stdio.h> • simplification and int add(int n1,int n2){ int s; abstraction is one of s=n1+n2; return s; the benefits of } procedural int main() { programming. But int a=10; within the functions, int b=20; int sum1=add(a,b); we still got same old printf("The sum is of %d + %d= %d\n", a,b,sum1); imperative code. int p=56; int q=78; • Example of procedural int sum2=add(p,q); printf("The sum is of %d + %d= %d\n", p,q,sum2); programming is BASIC return 0; C, and Pascal. } Computer Centre - Madras Institute of 9/23/2023 11 Technology Imperative programming paradigm Object-oriented Programming • It is the most popular programming paradigm because of its unique advantages like the modularity of the code and the ability to directly associate real-world business problems in terms of code. • The key characteristics of object-oriented programming include Class, Abstraction, Encapsulation, Inheritance and Polymorphism. • A class is a template or blueprint from which objects are created. • An object contains data in the form of fields that are known as attributes and the procedures are known as methods. • Examples of Object Oriented programming paradigm: Simula, Java, C++, ComputerPython, Ruby, Centre - Madras Smalltalk , etc Institute of 9/23/2023 12 Technology Object-oriented Programming- Cont’d
OOP facilitates the
understanding of a program, by the clear separation of concerns and responsibilities. Computer Centre - Madras Institute of 9/23/2023 13 Technology Imperative programming paradigm Parallel Processing Approach • The parallel programming paradigm breaks the problem or task into chunks that are distributed among multiple processors. • These chunks work on the same problem, simultaneously. • It reduces the total time to solve a problem. • It connects multiple processors to the memory. • It is either pooled across all processors or distributed over a network. • There are several programming languages that support parallel processing. • Example of parallel programming paradigm is SISAL, Parallel Haskell, SequenceL, System C (for FPGAs), Mitrion-C, VHDL, and Verilog, MPI. Computer Centre - Madras Institute of 9/23/2023 14 Technology Declarative programming paradigm • A declarative programming paradigm are those paradigms in which the programmer describes the property of the result without focusing on how to achieve it (imperative programming focuses on how to achieve the task by changing the state of the program). • The main focus in this kind of programming is what is to be done rather than how it should be done. • Declarative programming is used in programming languages used in a database query, regular expression, functional programming, logical programming, etc. • Some of the programming languages that support the declarative paradigm are:Prolog, javascript, Scala, Lisp, SQL, XQuery, Clojure Computer Centre - Madras Institute of 9/23/2023 15 Technology Types of Declarative Programming Paradigms • Functional Programming • Logical Programming • Database Processing Approach
Computer Centre - Madras Institute of
9/23/2023 16 Technology Declarative Programming Paradigms Functional Programming • It is the programming in which a program is constructed by creating and using functions. • The functions we refer to here are pure functions only. • Pure functions are representations of mathematical functions. • This means functions just do one thing. • They don’t depend on anything else but their arguments, and always produce the same result.
Computer Centre - Madras Institute of
9/23/2023 17 Technology Declarative Programming Paradigms Functional Programming – Cont’d • Rather than a series of statements functional programming use function to map and change one value to another value. • The other important note is, there are no loops in functional programming, all we have is implementing pure functions which have zero side effects. Example,
Computer Centre - Madras Institute of
9/23/2023 18 Technology Declarative Programming Paradigms Logical Programming • It's based on formal logic. • The logic programming paradigm isn't made up of instructions - rather it's made up of facts and clauses. • Logic programming uses sentences in logical form and creates an expression by using symbols. • In machine learning and artificial intelligence, there are many models that use these programs.. • The programs are executed very much like some mathematical statement. It is mainly based on forming logic. • For instance, Socrates is a man, all men are mortal, and therefore Socrates is mortal. • The following is a simple Prolog program which explains the above instance: man(Socrates). mortal(X) :- man(X) Computer Centre - Madras Institute of 9/23/2023 19 Technology Declarative Programming Paradigms Logical Programming – Cont’d man(Socrates). mortal(X) :- man(X) • The first line can be read, "Socrates is a man.'' It is a base clause, which represents a simple fact. • The second line can be read, "X is mortal if X is a man;'' in other words, "All men are mortal.'' This is a clause, or rule, for determining when its input X is "mortal. • The symbol ":-'', sometimes called a turnstile, is pronounced "if'‘. • We can test the program by asking the question: ?- mortal(Socrates). • that is, "Is Socrates mortal?'' (The "?-'' is the computer's prompt for a question). Prolog will respond "yes''. Computer Centre - Madras Institute of 9/23/2023 20 Technology Declarative Programming Paradigms Database Processing Approach • This programming methodology is based on data and its movement. • Program statements are defined by data rather than hard-coding a series of steps. • A database is an organized collection of structured information, or data, typically stored electronically in a computer system. • A database is usually controlled by a database management system (DBMS) • To process the data and querying them, databases use tables. • Data can then be easily accessed, managed, modified, updated, controlled and organized. Computer Centre - Madras Institute of 9/23/2023 21 Technology Declarative Programming Paradigms Database Processing Approach – Cont’d • A good database processing approach is crucial to any company or organization. This is because the database stores all the pertinent details about the company such as employee records, transaction records and salary details. • Most databases use Structured Query Language (SQL) for writing and querying data. • Here’s an example in database processing approach (SQL):