You are on page 1of 26
128 Programming - Module 3-COM SCI Unit 1 Computer St one ON PROGRAMMING SPECIFIC OBJECTIVES ‘At the end of this Section, you should be able to: 1. Identify the characteristics of different programming paradigms; ‘2, Explain the need for different programming languages; Explain how assemblers, compilers, virtual machines and interpreters are involved in the ‘execution of High-level programming language; ‘Assign values to declared variables; Use input and output statements; ‘Choose appropriate conditional and iterative constructs; Use conditional and iterative control constructs; Use arrays in programs; ‘Apply the techniques of structured decomposition to reorganize a program into smaller pieces; Implement algorithms to solve a given problem; Use records as a means of grouping related information; Use text files to store data and records. s stupy 7876) 433-3163 anytime (1 buy book) COM SCI Unit I-Modul PROGRAMMING [A programming language is a tool used by a programmer to give the computer specific itistructions in order to serve some purpose of a user. A program is like a recipe. It outlines the steps/instructions needed to create something or perform a certain task. Computers do exactly what they are told, no more, no less. Hence a program will perform the instructions given to it by a programmer in a particular program/software. . Programming languages are grouped according to programming paradigms. 163 anytime (19 buy book) 130 Programming paradigm -Module 3- COM SCI Unit Programming paradigms ‘A programming paradigm is a particular style of © programming or a solution to a programming \ problem using a programming language. Thus the programming languages that share similar characteristics are grouped together in the same paradigm. A programming paradigm is both a ‘method of problem solving and an approach to programming language design. A paradigm is just an approach or viewpoint to program development. : Programming paradigms are: © Procedural or imperative paradigm — examples of procedural programs are: COBOL, FORTRAN, BASIC, Ada, C, ‘Assembly, Machine Language Object-oriented paradigm- examples of object-oriented programs are: C++, Java, Visual Basic , «+ Functional paradigm- example of fanctonal program is: LISP J ov¥ Declarative or Logie paradigm-example of declarative program are: Prolog, GPSS (Simulation) Aspect and Scripting Ano stupy Call: 1 (76) 433-3163 anytime (to buy book) COM SCI Unit I-Module 3 — Procedural paradigm 131 Procedural or imperative paradigm Procedural paradigm describes a way of writing programs through the use of procedures/functions which contain a sequence of programming instructions. As a method of design, procedural programming attempts to solve a human problem through a sequence of procedures. Procedural programming has separate data and program logics unlike object-oriented programming that combines both data and methods into one unit known as an object. Examples of imperative programming languages are Fortran, Pascal and so on. 132 Imperative/procedural paradigm ~ Module 3 Tharacteristics ati L Programming Paradigm procedural paradigm takes a traditional approach to programming, that is, program is view.as a list of instructions to the computer. the programming process is viewed as developing a sequence of instructions to produce a desired result. the programming problem is taken as @ given (it is understood). algorithms can be created to represent the solution to a problem « simpLe A Call: 1 (@76) 433-3163 anytime ( COM SCT Unit 1-Module 3 — Procedural paradigm 133 Advantages of procedural paradigm ‘tis relatively simple. In comparison to other programming paradigm it is easy to execute by compilers and interpreters. Procedural programs utilise low memory Programs are written in a sequence of instructins and procedure, making them easy to read and understand. Disadvantages of procedural paradigm Itis difficult to use a procedural program to solve a complex problem which might not be ‘easy 10 visualise from a procedural viewpoint, I is difficult 0 find errors Procedural programming tends to be low= level language and as a result can be less productive. They usual take a long time to write. - Programs written using procedural Programming are difficult to debug as larger ‘programs have several programming instructions that are written in procedures. Data in pr sily get corrupted, as there are easily accessible to all procedures, even to those which do not have any right to access them. Call: 1 (876) 433-3163 anytime (to buy book) 134 Object-oriented paradigm ~ Module 3 -COM SCI Unit! Object-oriented paradigm(OOP) ‘The idea behind object-oriented paradigm is that a computer program is composed of a collection of individual objects, as opposed to a traditional view in ‘which a program is a list of instructions to the computer. Object-oriented programming is characterised by the defining of classes of objects, and their properties. Fach object is capable of sending messages, processing data, and receiving messages from other objects. Objects are treated as real life objects. Just like real life objects these objects have their own identities, behaviours and states. . Object-oriented programming provides inheritance of variables and methods(procedures/functions) which is ‘one way of reducing the amount of programming. requires in developing a program. Objéct-oriented paradigm also provides libraries in the programming Environment that can also reduce the effort required in developing a program. Example of object-oriented programs is Java, C++, and so on, Object-oriented jgramming(OOP) has made programming easier by providing the facilities of data hiding, reusability by inheritance and polymorphism. Keer it $impLe AND STUDY Call: | (876) 433-3163 anytime (to buy book) Module 3 — Object-oriented Paradigm (OOP) 135 Characteristics of Obectorientedparadigm(00P) ~ Object-oriented program consists of a collection of individual objects, Object-oriented paradigm provides inheritance of variables and methods, meaning that existing ‘ariables and methods can be reused by other programs reducing the time need.to create new Variables and methods. leclared private, that is i data attributes to objects of that class or sub-class. Whereas, methods that are declared Public are available to any objects, Encapsulation or hiding restricts object’s data. It ensures that users of an object neo Change the intemal state of the object in ‘unexpected ways, > _tprovides polymorphi Polymorphism is the to respond to the ‘sam ways. 136 Advantages of Object-oriented paradigm - Module 3 Advantages of Object-oriented paradigm(OOP) - It is not complex as it uses objects that ‘model real life objects ‘There is restriction and access control of data as the data anid functions/procedures which are supposed to have accessed to a particular data are packed into one unit known as an object, thus preventing unauthorised access. Programmers can define their own data types and the corresponding procedures/functions as there are no restrictions to the use of predefined data types such as integer, float and character. ‘The objects made in a particular program can be reused by any othe ‘program, This increases the reusability of the programs once they are written. Program written in an OOP can be easily updated by using the facilities of inheritance. Inheritance usually reduces maintenance time because of the domino effect it has on derived(child) classes when a change is made in a base(parent) class. OOP is more reliable than procedural paradigm. x pve ano Srupy Call: 1 (676) 433-3163 anytime Module 3-Disadvantages of Object-oriented paradigm Disadvantages of Object-oriented paradigm(OOP) Subclasses(child classes) can be very complex because of inheritance and polymorphism. Once an instance of a derived class (subclass) is initiated; all the data and functions from base class (parent class) that may not be used are carried along with the inheritance. This can compromise security. ‘You must develop strong base classes and understand the functionality of class libraries before you can use the OOP effective. OOP can be di fo learn. Itis difficult to establish base classes - a ‘g0od foundation must be created before you derive other classes. Base classes have to be broad enough to meet the needs of your application and any future upgrades to that application. Call: | (876) 433-3163 anytime (to buy book) Functional paradigm-Module 3-COM SCI Unit / : + Lech Functional paradigm Functional paradigm involves the construction “and integration of «series of functions. In functional paradigm programs are designed using the right combination of functions. Funetions can be viewed as a series of black boxes(rectangles) ina structure char/Hierarchical Input Process Output (HIPO) chart. The functions are represented on a structure chart/HIPO chart as rectangles and can show valuable information about the functions before the actual writing of ‘codes. Functions can accept inputs and produce outputs. r For example: Write a program to compute the average of a list of numbers using a functional program. Functional paradigm would view this task as a combination of functions: ‘Sum the Numbers Count the Numbers Divide the Sum by the Count to get the Average keer ir st Call: 1 (B76) 433-3163 anytime (to buy book) COM SCI Unit “Module 3 — Funetional paradigm Let say you have the following numbers: 43 2 8910 and you want o find their average *you add the numbers first “Then you find the average by dividing the sum by the count ‘MAIN PROGRAM ‘Void mai) ‘SUM_FUNCTION {nt Sun, funetion(num: {count fanetion!) intsum=0 sum=sum-+ aum retwn sum 1 COUNT_FUNCTION Tt Count funetion() ( int count = 0 ‘ount=count + 1 ‘return count : AVERAGE_FUNCTION Int Average Tonction(nt sum, int count) t im average average =" Y/eount | feu average 3 anytime (to buy book) 140 Advantages of Functional paradigm - Module 3 Advantages of Functional paradigm = Functional paradigm uses the modular approach, that is, a large problem(program) can be divided into smaller sub problems (sub-program) and if necessary, sub problems (sub-programs) are further subdivided and so on, For each sub problem, a function is developed to solve it. Note: A function isa sub-program. Programs are easy to understand and reason out. _Functions are reusable- Functional paradigm allows programmers to create libraries of reusable functions which can be used whenever needed. It is possible to develop and maintain very large programs consisting of thousands of functions, because functions have no side effects. Itis straightforward to fully test functions. Disadvantages of Functional paradigm Some aspects of problem solving cannot be easily or sensibly performed in a functional manner. Itis difficult to use a functional program to solve a complex problem which might not be easy to visualise from a functional viewpoint. Errors are hard to find in very large program. Call: | (@76)433-3163 anytime (to buy book) COM SCI Unit -Module 3-Declarative paradigm 141 Declarative paradigm Declarative paradigm describes programs that specify the necessary steps to obtain the desired result of a user. Declarative programming is an approach to computer programming that involves the creation of a set of conditions to reach a solution, but leaves the interpretation of the specific steps needed to arrive at that solution up to an unspecified interpreter. Declarative programming thus takes a different approach from the traditional imperative programming found in C or Pascal which requires the programmer to provide a list of instructions to execute in a specified order. In other words, declarative programming provides the what, but leaves the how up to interpretation. This is why declarative solutions are said to have two independent phases: declaratiqn and interpretation. Examples of declarative are: Prolog SOL GPSS (Simulation) Le ano stupy 33 anytime (to order book ) 142. Advantages of Declarative paradigm - Module 3 Advantages of Declarative paradigm ~The solution can address the problem at an abstract level without being complex and ‘echnical. This makes the solution easier for humans to understand. To solve a problem you can simulate the problem environment. Complex problem solving is isolated and hhandled by an interpreter. Interpreters written years ago can apply its logic to a wide variety of declarative specifications. ‘Re-use/re-interpretation in different contexts is possible. It can be used to solve problems related to artificial intelligence, databases et. Problems can be solved using formal logic. Disadvantages of Declarative paradigm ‘The declarative program is not efficient as it ‘ay be time consuming to arive at a solution. It is assumed that considerable effort must be used in understanding a problem. Emphasis will be on understanding what is the problem Users need an in depth knowledge of how a declarative program works to use it effectively. keep iT SIMPLE AND STUDY Call: | (876) 433-3163 anytime (to order book ) ‘Module 3 — Logical programming languages 143 Logical programming languages, like Prolog, are ofien thought of as defining “what” is to be computed, rather than “how” the computations are to take place, as an imperative programming language does. Logic programming adopts a different approach to problem solving from both procedural programming and functional programming. In essence, logic programming requires a logical declarative description of the nature of the problem. How the program is to be executed is not something the designér needs to ‘worry about, and this is left to the PROLOG inference engine to deal with. As a result this raises the level of abstraction considerably as the program contains no explicit control information. A PROLOG program consists of facts and rules. Running a prolog program consists of asking a query about a fact. PLE AND stuDY Calls 1 (876) 433-3163 anytime (wo order book ) Logical programming language - Module 3 “Advantages of logical programming language jing includes the development of concise solutions to problems, and the ability to reason out what the program does. Fact and rules driven Relations are used instead of functions Problem can be simulated in a problem environment. Disadvantages of logical programming language Required considerable effort to understand a problem. Slow execution difficult to learn complex logical program. It can be difficult to understand complex cogram and procedural semantics. Itis difficult to debug( find errors) in Jarge logical program. ‘Alot of emphasis will be on understanding “what is the problem” before a solution can be found, keep iT SIMPLE Call: 1 (876) 433-3163 anytime (to order book ) COM SCI Unit -Module 3 — Aspect and Scripting 145 Aspect and Scripting Aspect is a feature connected to many parts of a program, but is not necessarily the primary function of the program. For example, a logging code can be connected to many modules, yet the aspect of logging is separate from the functional concernis of the modules it is connected. Script is an interpreted language used to write simple programs. Examples of Scripting languages are JavaScript, Perl, Tel and Python. ~ Scripts can be used to enhance the functions of a program. For example, JavaScript is widely used on Web pages for calculations as well as for displaying messages, drop-down menus and other user interface elements. Scripts are interpreted/translated than compiled ahead of time. Scripts can be modified by an end-user. MPLE AND stuDY 3-3163 anytime (to order book) Programming languages ~ Module 3 The differences between programming languages Programming languages differ in the following ways: ‘Run-time: is the rate at which a program runs (or js being executable) on a computer. That is, when ‘you Stgrt a program running on a computer, itis ‘runtime for that program. Efficiency: refers to how efficient a software is at performing the task(s) it was built for. Portability: describes the ability of a computer program to be used by different computers and software without problem. Usability: refers to how effective a user can use software to perform the task(s) it was built for. ‘Maintainability: describes the ability of a software to be modified or evolved or changed or updated according to the changing demand of a user. “Appropriateness to application: refers to the ‘suitability of the software to perform a given task(s)- MA Run-time I Efficienc Portabil Usability. Maintainability Appropriateness 7 SIMPLE AND STUDY 176) 433-3163 anytime (to order book ) COM SCI Unit 1- Module 3-Programming Language 147 Factors to consider when selecting a programming language: Find out the programming language being uses by the company, that is, the organisation standard programming language. Programmer's skill level The intended use of the program ~ choose a programming language that best suits the ‘characteristics of the application. For example: * Cobol suits business application * Fortran suits the scientific application * Ada suits the embedded system ‘application etc. Design methodology — choose a programming language that matches the programmer's design methodology. Quality requirements Training, technical support and document(end-user manual) requirements Keep iT SIMPLE AND STUDY Call: 1 (876) 433-3163 anytime (to order book ) 148 Translators ~ Module 3-CoM SCI Unit t Interpreters, [A Compiler translates the whole source code fa ‘gaitten in a high-level language into rrchine code befare its executed. The compiler mone slates al the instructions before carrying them out. ‘An Interpreter analyses the Source code, statement by statement as execution proceeds, and calling routines to carry out each instruction. No object code is stored by an inter ‘which means that a is language into different from a compil ‘and instructs and then carries {instructions of a program. ‘An Assembler is a program that translates programs from assembly language 1° machine language. Virtual machine (VM) is a software Jinplementation of a machine (compute?) that vxecutes programs like a real machine. keep 17 S1MPLe AND STUDY Gall, 1 (876) 433-3163 (to order book) almost always run faster than an interpreted one, as no translation is taking place at the same time. object program (the machine code generated by the compiler) may be saved on a storage device and mn whenever recompiled, or requiring the user to haye the compiler does it of object code, thus purchasers from listi cations to it(perhaps with the intention of selling the | modified version as their own roduct) Inept ae very for program development sce making modifications ‘does not mean that the ‘whole program has to be recompiled, which can take a considerable time for a large program. Many interpreters will allow a program to run up to the point where an error occurs, let the programmer fix the error ‘and then continue from that ‘An interpreter is simpler to programmer and cheaper to buy. ‘An interpreter generally ‘occupies much less ‘memory than a compiler. x 17 SimpLE AND STUDY CCall: 1 (876) 433-3163 (to buy book) 150 Translation/Compilation Process ~ Module 3 ‘Stages in the Translation/Compilation process “The Translation/Compilation process involves the following stages: (1) Lexical analysis stage (2) Syntax analysis stage @) Semantic analysis stage (4) Intermediate code generation stage (5) Code generation stage € \ a Lara ‘Scores Sixes In Cricket nes Sine pA LA Lexical |), | code generation ‘Semantic \ sents Intermediate code generation keep it simpee AND STUDY Call: 1 (876) 433-3163 (fo buy book) Module 3 — Translation/Compilation Process Lexical analysis stage _At the Lexical analysis stage all comments and unnecessary ‘spaces are removed and some simple error checking is ‘performed. For example, if a programming language requires ‘a limit on the length of identifiers or string constants, the lexical analyser will determine if the identifier is too long. The instructions ofa program is scanned and actual ‘symbols of the program such as integers, identifiers, reserved words etc. are created E Symax analysis stage Syntax analysis is the process of determining whether the ‘syntax of the program is correct. The codes are checked to see if they follow the rules of a particular programming language. Note: The synax rules of a language define how a programmer can put symbols, reserved words, and identifiers together to make a valid program. ~ Semantic analysis stage ‘Semantic analysis is concerned with the meaning or interpretation of codes/commands in the context in which they are used. During semantic analysis, the commands/codes in the program are tested to see if they are correctly used ‘according to the structure of the programming language used. The constructs of the program is check for semantic correctness and the necessary information about the constructs are stored in a construct symbol table. Note: The ‘semantics of a program statement define what that statement ‘means (its purpose or role in a program). A program that is ‘symtacticaily correct is not necessarily logically (semantically)correct. Keep it SiMPLE AND STUDY Call: 1 (876) 433-3163 (to buy book) 152 Translation Process ~Module 3-COM SCI Unit! Intermediate code generation (Code optimization and code generation) Code optimization ‘At the optimisation stage the codes are modified so that they can be ‘executed faster. This reduces the ‘execution time for a program by looking at how the codes could execute more efficiently. Code generation [At this stage the actual machine codes or assembly Janguage are generated from the internal source program. Its the most detailed part of the compilation. Keer 1T SIMPLE AND STUDY Call: 1 (876) 433-3163 (to buy book) Module 3 — Translation/Compilation Process 153 of the ion. Source code (program written in high-level programming language) Lexical analyser ‘Syntax analyser Semantic Analyser Intermediate Code generator Code oftimiser Code Object Code (machine language)

You might also like