Unit-1 - Introduction to Programming Paradigm PDF

Summary

This document provides an overview of programming paradigms, including imperative, functional, logical, and object-oriented paradigms. It also discusses the hierarchy of programming paradigms and gives examples of concepts in different languages.

Full Transcript

APP java public class Test public class Test{ public static void main(String { public static void main(String []args) []args) { System.out.println(3); { System.out.println("My First Java Program."); System.out.println(358);...

APP java public class Test public class Test{ public static void main(String { public static void main(String []args) []args) { System.out.println(3); { System.out.println("My First Java Program."); System.out.println(358); System.out.println(50000); }}; System.out.println("yes or no "); }}; System.out.println(3 + 3); System.out.println(2 * 5); //variabes String name ="John"; System.out.println(name); int myNum; myNum = 15; System.out.println(myNum); Unit-1 - Introduction to Programming Paradigm Programming Languages – Elements of Programming languages - Programming Language Theory - Bohm- Jacopini structured program theorem - Multiple Programming Paradigm – Programming Paradigm hierarchy – Imperative Paradigm: Procedural, Object-Oriented and Parallel processing – Declarative programming paradigm: Logic, Functional and Database processing - Machine Codes – Procedural and Object-Oriented Programming – Suitability of Multiple paradigms in the programming language - Subroutine, method call overhead and Dynamic memory allocation for message and object storage - Dynamically dispatched message calls and direct procedure call overheads – Object Serialization – parallel Computing Programming Languages Programming languages are formal languages comprised of sets of instructions that produce various kinds of output. They are used in computer programming to implement algorithms. Elements of Programming Languages The core elements of programming languages typically include: Syntax: The set of rules that defines the combinations of symbols that are considered to be correctly structured programs in that language. Semantics: The meaning of the syntactic elements and their combinations. Pragmatics: The ways in which the language constructs are used to achieve desired outcomes. Programming Language Theory Programming Language Theory (PLT) is a branch of computer science that deals with the design, implementation, analysis, characterization, and classification of programming languages and their individual features. Böhm-Jacopini Structured Program Theorem The Böhm-Jacopini theorem, established by Corrado Böhm and Giuseppe Jacopini in 1966, states that any computer program can be written using only three control structures: Sequence: Executing one subprogram after another. Selection: Executing one of several subprograms based on a condition (if-then-else). Iteration: Repeating a subprogram while a condition holds (loops). This theorem is fundamental to structured programming, promoting code that is easier to understand, test, and maintain. Multiple Programming Paradigms A programming paradigm is a style or way of programming. Different programming languages support different paradigms, some support multiple paradigms. The main programming paradigms include: Imperative: Describes computation in terms of statements that change a program state. Functional: Treats computation as the evaluation of mathematical functions and avoids changing state and mutable data. Logical: Uses formal logic to express computations. Object-Oriented: Organizes software design around data, or objects, rather than functions and logic. Procedural: A type of imperative programming where the program is built from procedures or functions. Programming Paradigm Hierarchy Programming paradigms can be organized in a hierarchy, from low-level to high-level: Machine Code: The lowest level, consisting of binary code directly executed by a computer's CPU. Assembly Language: A slight abstraction over machine code, using symbolic names for operations and memory locations. Imperative Programming: Higher-level languages like C, which are still close to hardware but use more human-readable syntax. Procedural Programming: A subset of imperative programming that emphasizes procedure calls. Object-Oriented Programming: Builds on procedural by encapsulating data and operations within objects. Functional and Logical Programming: Higher-level paradigms focusing on mathematical function evaluation and formal logic. Imperative Paradigm The imperative paradigm focuses on describing how a program operates, using statements that change a program's state. Key features include: State: Represents the memory of the program at any point in time. Statements: Commands that change the state. Control Flow: Constructs like loops, conditionals, and function calls that determine the sequence of execution. Imperative programming is exemplified by languages such as C, Java, and Python, where developers write explicit instructions to control the flow of the program. These foundational concepts are essential for understanding how different programming languages and paradigms are structured and how they operate. Programming Language Programming Language is a notational system for describing computations in both machine readable and human readable form. It allows the programmer to specify a task and the computer to execute the task. It is a notation for writing computer programs and are text based formal languages but could also be graphical. Elements of programming languages I. Syntax II. Semantics III. Pragmatics are the elements of a programming language. Syntax It converts the High level input program into a sequence of Tokens. A lexical token is a sequence of characters that can be treated as a unit in the grammar of the programming languages. First, a lexer turns the linear sequence of characters into a linear sequence of tokens; this is known as "lexical analysis" or "lexing". Second, the parser turns the linear sequence of tokens into a hierarchical syntax tree; this is known as "parsing" narrowly speaking. Thirdly, the contextual analysis resolves names and checks types. The parsing stage itself can be divided into two parts: the parse tree, or "concrete syntax tree", which is determined by the grammar, but is generally far too detailed for practical use, and the abstract syntax tree (AST), which simplifies this into a usable form. Words are in regular language, phrases are in CFL (context free language) and context in context-sensitive language. Example for Lexical analysis and Parsing Keywords; Examples-for, while, if etc Identifier; Examples-Variable name, function name, etc Operators; Examples '+', '++', '-' etc Separators; Examples ',' ';' etc Non-tokens : Preprocessor directives, macros, blanks, tabs, newline The syntax of a computer language is the set of rules that define the combinations of symbols that are considered to be correctly structured statements or expressions in that language. Computer language syntax is generally distinguished into three levels ▪ Words -the lexical level, determining how characters form tokens; ▪ Phrases -the grammar level, narrowly speaking, determining how tokens form phrases; ▪ Context -determining what objects or variables names refer to, if types are valid, etc. AST: abstract syntax tree Semantics Semantics assigns computational meaning to valid strings in a programming language syntax. It describes the processes a computer follows when executing a program in that specific language. Syntax therefore refers to the valid form of the code, and is contrasted with semantics -the meaning. Both syntax tree of previous phase and symbol table are used to check the consistency of the given code. Type checking is an important part of semantic analysis where compiler makes sure that each operator has matching operands. Semantic errors: ▪ Type mismatch ▪ Undeclared variables ▪ Reserved identifier misuse ▪ No break outside loop Programming Language Theory Programming language theory (PLT) is a branch of computer science that deals with the design, implementation, analysis, characterization, and classification of formal languages known as programming languages. Bohm- Jacopini structured program theorem The structured program theorem, also called the Bohm-Jacopini theorem, is a result in programming language theory. It states that a class of control-flow graphs (historically called flowcharts in this context) can compute any computable function if it combines subprograms in only three control structures. These are 1. Executing one subprogram, and then another subprogram (sequence) 2. Executing one of two subprograms according to the value of a Boolean expression (selection) 3. Repeatedly executing a subprogram as long as a Boolean expression is true (iteration) Corrado Bohm and Giuseppe Jacopini showed in 1966 that any non-structured program can be rewritten by combining three techniques: sequence, selection, and repetition (or loop). Sequence: perform S1, then perform S2. Selection: if Q is true, then perform S, else perform S2. Loop: while Q is true, do S. Programming Paradigms (PP) A programming paradigm is a fundamental style of computer programming, ▪ Serve as a way of building the structure and elements of computer programs. ▪ It is a way to classify programming languages based on their concepts and constructs. Languages can be classified into multiple paradigms. Some programming languages follow only one paradigm [Ex: Smalltalk - object-oriented and Haskell - functional], while others support multiple paradigms. [Ex: C++, Java, C#, Scala, Python, Ruby] ▪ OO PP models a program as a collection of interacting objects; ▪ Functional PP models a program as a sequence of stateless function evaluations; ▪ Event-driven PP models a program as a sequence of events such as user clicks or key presses. Main loops, waits for events and callbacks are called for specific events. ▪ Process PP allows a program to be modelled as a set of concurrent processes that act upon a shared data structure. ▪ Pure functional PP forbids side-effects ▪ Structured PP disallows GoTo. Hierarchy of Programming Paradigms Imperitive programming paradigm ▪ Programmer instructs the machine how to change its state ▪ Instructions are statements that can be used to assign values to variables, control the flow of execution, and perform operations on data. ▪ A variable name is bound to an address and a value to bit pattern. The location is called as l-value and the value is r-value. x=x+2, where the x on the left is the location and x on the right is the value. ▪ State of a program: Collection of names, associated values, the location of the control in the program together is the state of a program. Imperative programs are a sequence of state changes ▪ Constructs of IPP: Assignment, Sequence, Control ✔ Assignment construct creates a statement that has a variable and expression. x=t; x location is filled with the value of the expression t. ✔ Expression involves variables and constants with arithmetic/logical operators. Literals are constants. ✔ Variables have to be declared before usage. Declaration ties the variable and its location. Variables need not be declared with initialized values. ✔ Constant variable: The value of an variable can’t be changed after initializing. ✔ Mutable variable: A variable that is not a constant is mutable variable. To declare a const, use final keyword in Java. ▪ Sequence is a set of statements that are executed in a sequential manner. Block is a sequence {s1; s2} is a sequence, where s1 gets executed to change the state of the program and then s2 is executed. ▪ Control constructs determine the sequence to be executed. Example: If-Else test constructs and for, while loop constructs. Example: C, C++, Java, Ruby, Javascript and Python. Advantage: Easy to write, Efficient and Widely supported. Disadvantage: Difficult to debug and maintain. Procedural programming paradigm Groups instructions into procedures, A program is built from one or more procedures (also termed subroutines or functions) and is modular. They use procedures and control flow to describe the flow. Example: Fortan, C, Cobol Parallel Processing Programming Paradigm: Many calculations or processes are carried out simultaneously. Large problems are divided into smaller ones, which can then be solved at the same time. For example, a program that needs to calculate the sum of a large number of numbers can be parallelized by dividing the numbers into smaller groups and assigning each group to a different processor. Different parallel programming approaches: Thread-based parallelism (uses threads to execute different parts of a program simultaneously. Threads are lightweight processes that share the same memory space, which makes them easy to synchronize.): Process-based parallelism (uses processes to execute different parts of a program simultaneously. Processes are independent of each other, which makes them more difficult to synchronize, but they also offer more flexibility): Data parallelism (divides a program's data into smaller pieces and assigns each piece to a different processor. This approach is well-suited for problems that can be divided into independent tasks.). Advantage: Scalability, Improved performance Disadvantage: Complexity, synchronization. Object-oriented programming paradigm Groups instructions with the part of the state they operate on. Concept of "objects", which contain data and code. A common feature of objects is that procedures (or methods) are attached to them and can access and modify the object's data fields. C++, Java, C#, Python are examples Advantages: Reusable, modular Disadvantage: Complex inheritance Declarative programming paradigm: Programmer merely declares properties of the desired result, but not how to compute it. It expresses the logic of a computation without describing its control flow. It is quite easy to write and understand by humans. Common declarative languages include those of database query languages (e.g., SQL, XQuery, Prolog). Advantages: Easy to read/write, Flexible, Efficient for searching, querying and reasoning. Disadvantages: Inefficient as compiler can’t optimize code. Logic programming paradigm Desired result is declared as the answer to a question about a system of facts and rules. Focuses on expressing knowledge and rules in a declarative way. Logic programming language families include Prolog, answer set programming (ASP) and Datalog. Concepts in logic programming Facts: Facts are statements that are known to be true Rules: Rules are statements that describe how facts can be deduced from other facts Inference: The process of deducing new facts from existing facts. Example: In python fact: father(john, Michael). fact: father(john, peter). rule: grandfather(X, Y) :- father(X, Z), father(Z, Y). query: grandfather(john, Michael). Programming Paradigm When diving into the world of programming, one often encounters the term "programming paradigm." But what exactly is a programming paradigm, and why is it so crucial to understand? A programming paradigm is essentially a style or "way" of programming. It's a set of concepts and practices that define how you structure and write your code. Understanding these paradigms can significantly impact how you approach problem-solving and software development. Types of Programming Paradigms Programming paradigms can be broadly categorized into different types, each with its unique approach to coding. Let's explore the most common paradigms: Imperative Programming Imperative programming is a paradigm that uses statements to change a program's state. It focuses on describing how a program operates. Characteristics Sequential execution of instructions Use of variables to track state Emphasis on how to perform tasks Example: Using C Language Consider a simple example in C to calculate the sum of the first 10 natural numbers: #include int main() { int sum = 0; for (int i = 1; i

Use Quizgecko on...
Browser
Browser