Chap 4 Programming and Basics

download Chap 4 Programming and Basics

of 11

Transcript of Chap 4 Programming and Basics

  • 8/12/2019 Chap 4 Programming and Basics

    1/11

    Programming and Basics

    LOOP

    While writing a program it is necessary to repeat a part of a program for a number of times. Thebest solution for this is to use a loop it is a method which executes a part of the programrepeatedly as specified by the programmer.

    TYPES OF LOOPS

    a) Pre-Test Loopb) Post-Test Loop

    a) PRE TEST LOOP

    As its name indicates, checs the looping condition at the start of the loop if the condition is truethe loop execution starts otherwise stops. This loop is used in a situation when part of program isto be repeated for a fixed number of time.e.g., !"# $$ %&'T.

    b) POST TEST LOOP

    This loop checs the looping condition at the end of loop and decides whether or not to continuethe execution. This loop is used when a part of the program needs to be repeated until a specified

    condition is met.e.g., W(L& $$ W&%*.

    FLOW CHART

    !low chart is a symbolic representation of flow of programme. t can graphically represent dataprocessing procedure !low chart ser+es two purposes, i.e., the planning of the program structureas an aid in writing the computer program. econdly the documentation of the program logic andwor flow for the purpose of the communication with other person and to recall a program at alater time. f the program needs to be changed. The second purpose is +ery important because itis +ery difficult to chec the logic of the program.

    SYMBOLS OF FLOW CHART

    TERMINAL

    The terminal symbol is used to indicate the beginning, ending of the program logic flow. t is thefirst and last symbol of the flow chart.

  • 8/12/2019 Chap 4 Programming and Basics

    2/11

    INPUT / OUTPUT BOX

    The inputoutput box is used to denote any function of an input and output in the program. fthere is a program instruction to input or output the data from any " de+ice that step will beindicated on this symbol.

    PROCESS BOX

    A processing symbol is used in a flow chart to represent arithmetical and data mo+ementinstructions.

    FLOW LINES

    !low lines are used to indicate the flow of operations. The exact seuence in which theinstructions are to be executed. The flow of flow chart is normally from top to bottom and left toright.

    DECISION BOX

    The decision box is used in a flow chart to indicate a point at which a decision has to be madeand to branch to one or more alternati+e points that is possible during execution. The appropriatepath followed, depends on the result of the decision.

    CONNECTOR

    A flow chart becomes complex, when the number and direction of flow lines is confusing or itspread o+er more than one page. t is useful to utili/e the connector symbol as a substitute for

    flow lines.

    SYSTEM FLOW CHART

    ystem !low 0hart is used to describe complete data processing cycle including the hardwarede+ices and media used. A ystem !low 0hart shows flow of data in a system where and in whatform it is recei+ed by the system, how it is input to the computer and what storage media areused to hold it. The symbol used in system flow chart indicates the peripherals to be used in dataprocessing procedure, where output will be produced. ystem flow chart do not explain the logicof the programs.

    ERRORS

    &rrors are the mistaes made by the programmer during program writing. A program relays runperfectly for the first time and each program needs to be tested to ensure that it is correct andcontains no errors. There are three types of errors generally encountered.a) yntax &rrorb) Logical &rrorc) #un Time &rror

  • 8/12/2019 Chap 4 Programming and Basics

    3/11

    a. SYNTAX ERROR

    The syntax of a programming language is the set of rules to be followed when writing a programin that language. These rules are similar to the grammar rules of 1rdu or &nglish language.When a program +iolates these rules, computer generates a syntax error. yntax must be correct

    before program execution.

    b. LOGICAL ERROR

    These errors are the most difficult ones to find, if a programmer enters a sign of multiply insteadof addition at the time of maing the program, the answer will not be correct because interpreterwill not be able the detect these types of errors. A logical error will not stop the execution of theprogram but the result will not be accurate.

    c. RUN TIME ERROR

    These types of errors stop the execution of the program. t may be due to entering the in+aliddata at the time of executing a program. !or example, if a program is expecting the user to enterthe numeric data and the user enters a string data, computer will display an error message.

    BITS & BYTES

    The smallest piece of data that can be recogni/ed and used by a computer, is the bit, a binarydigit. A bit is a single binary +alue, either a 2 or 3. A group of eight bits is called a byte. The byteis the basic unit for measure of the si/e of the memory, with today4s computer memory si/es. tis more common to hear the term ilobyte 567) or megabyte 587).

    COMPUTER WORD

    A computer word is the number of ad9acent bits that can be stored and manipulated as a unit. :ustan &nglish +ocabulary words are of +arying lengths so are computer words. 8anymicrocomputers ha+e the ability to manipulate a ;< bit word, while some models ha+e wordslength of = and 2> bits.

    LIBRARY FUNCTIONS

    ?W-7A0 pro+ides many built in functions which are called binary functions. &ach function iscomposed of a format or a short program, that reuires at least one parameter to be passed. A

    parameter is a +alue which is in that formula or program and this formula or program returns aresult. This result can be stored in any appropriate +ariable or directly displayed on monitor. Alibrary function which has @B sign is called string library function while others are callednumeric library function. A string function returns string +alue while a numeric function returnsnumeric +alue. !ollowing are some library functions.A7, 8*, L&%, L&!T, A0, 0(#, TA7

    EXPRESSIONS

  • 8/12/2019 Chap 4 Programming and Basics

    4/11

    An expression is a combination of constants and +ariables lined by arithmetic operators lie 5C,-, D, , E). &xpressions are used to perform different operations. The expressions are e+aluatedfrom left to right but some operators ha+e priority o+er the others. Parenthesis are e+aluated firstthen multiplication and di+ision ha+e the same priority from left to right. imilarly the additionand subtraction are e+aluated with eual priorities. f all the operators are used in an expression

    ha+e the same priority then the expression be executed from left to right.The expressions can be of three types.

    Arithmetic expressions

    Logical expressions

    #elational expressions.

    ARITHMETICAL EXPRESSIONS

    n an arithmetic expression the following operators are used in con9unction with the operands.ymbol8eaningC Addition- ubtraction

    8ultiplication

    *i+isionF &xponentiation5 Left parenthesis) #ight parenthesis

    RELATIONAL EXPRESSION

    A #elational expression is composed of operands lined by the relational operators. Therelational operators used in the relational expression are gi+en.ymbol8eaningG eual toH greater thanI less thanIH not eual toHG greater than or eual toIG less than or eual to &xample A H 77 IH 0

    LOGICAL EXPRESSIONS

    When a selections is based upon one or more condition being true. t is possible to combine thecondition together using logical operators and the resulting condition would either be true orfalse the most commonly used logical operators are A%*, "# and %"T.

  • 8/12/2019 Chap 4 Programming and Basics

    5/11

    COMPUTER LANGUAGE

    Language is a system for representation and communication of information or data. Lie humanbeings, a language or signal is reuired to communicate between two persons. imilarly, wecannot obtain any result by computer without langtage. 0omputer does not understand directly

    what we are communicating with computer as &nglish or Arabic, it understands only machinelanguage 5binary codes 3-2). 0omputer translates &nglish language into machine codes throughinterpreter then process instructions and gi+e us the results.The computer languages can be di+ided into two main le+els.

    8achine language 53-2)

    ymbolic language 5A-J)

    ymbolic languages are further di+ide into two main le+els

    (igh-le+el language

    Low-le+el language

    MACHINE LANGUAGE

    Although computers can be programmed to understand many different computer language. Thereis only one language understood by the computer without using a translation program. Thislanguage is called the machine language or the machine codes. 8achine codes are thefundamental language of the computer and is normally written as strings of binary 3-2.

    ADVANTAGES AND LIMITATIONS OF MACHINE LANGUAGE

    Programs written in machine language can be executed +ery fast by the computer. This is mainly

    because machine instructions are directly understood by the 0P1 and no translation of programis reuired.(owe+er, writing a program in machine language has se+eral disad+antage.

    MACHINE DEPENDENT

    7ecause the internal design of e+ery type of computer is different from e+ery other type ofcomputer and needs different electrical signals to operate. The machine language also is differentfrom computer to computer.

    DIFFICULT TO PROGRAM

    Although easily used by the computer, machine language is difficult to program. t is necessary

    for the programmer either to memori/e the do/ens of code number for the commands in themachine4s instruction set or to constantly refer to a reference card.

    DIFFICULT TO MODIFY

    t is difficult to correct or modify machine language programs. 0hecing machine instructions tolocate errors is difficult as writing them initially.n short, writing a program in machine language is so difficult and time consuming.

  • 8/12/2019 Chap 4 Programming and Basics

    6/11

    SYMBOLIC LANGUAGES

    n symbolic languages, alphabets are used 5a-/). symbolic languages are further di+ide into twomain le+els.

    (igh le+el languages Low le+el languages

    LOW LEVEL LANGUAGE

    A language which is one step higher than machine language in human readability is calledAssembly Language or a low-le+el language. n an assembly language binary numbers arereplaced by human readable symbols called mnemonics. Thus a low-le+el language is better inunderstanding than a machine language for humans and almost has the same efficiency asmachine language for computer operation. An assembly language is a combination of mnemonic,operation codes and symbolic codes for addresses. &ach computer uses and has a mnemoniccode for each instruction, which may +ary from computer to computer. ome of the commonly

    used codes are gi+en in the following table.0"88A%* %A8&8%&8"%0Add K A**ubtract K 178ultiply K 81L0ompare #egistry K 0#0ompare K 0"8P7ranch 0ondition -700ode #egister -L#8o+e 0haracters -8&tore 0haracters -T0

    tore Accumulator K TA

    An assembly language is +ery efficient but it is difficult to wor with and it reuires good sillsfor programming. A program written in an assembly language is translated into a machinelanguage before execution. A computer program which translates any assembly language into itseui+alent machine code is nown as an assembler.

    HIGH LEVEL LANGUAGE

    A language is one step higher than low-le+el languages in human readability is called high-le+ellanguage. (igh K le+el languages are easy to understand. They are also called &nglish oriented

    languages in which instruction are gi+en using words. uch as add, subtract, input, print, etc.high le+el language are +ery easy for programming, programmer prefer them for softwaredesigning that4s why these languages are also called user4s friendly languages. &+ery high le+ellanguage must be con+erted into machine language before execution, therefore e+ery high le+ellanguage has its own separate translating program called compiler or interpreter. That4s whysome time these languages are called compiler langauges. 0"7"L, 7A0, PA0AL, #P?,!"#T#A% are some high le+el languages.

  • 8/12/2019 Chap 4 Programming and Basics

    7/11

    INTERPRETER

    An interpreter is a set of programs which translates the high-le+el language into machineacceptable form. The interpreters are slow in speed as compared to compilers. The interpretertaes a single line of the source code, translates that line into ob9ect code and carries it out

    immediately. The process is repeated line by line until the whole program has been translatedand run. f the program loops bac to earlier statements, they will be translated afresh each timeround. This means that both the source program and the interpreter must remain in the mainmemory together which may limit the space a+ailable for data. Perhaps the biggest drawbac ofan interpreter is the time it taes to translate and run a program including all the repetition whichcan be in+ol+ed.

    PROGRAM DEVELOPMENT PROCESS

    n order to de+elop a computer program, a programmer has to go through the following stagesM

    1. DEFINING AND ANALYSING THE PROBLEM

    n this step a programmer studies the problem and decides how the problem will be best sol+ed.tudying a problem is necessary because it helps a programmer to decide aboutM

    The facts and figures to be collected.

    The way in which the program will be designed.

    The language in which the program will be most suitable.

    What is the desired output and in which form it is needed, etc.

    . DESIGNING THE ALGORITHM

    An algorithm is a set of instructions or seuence of steps that must be carried out before aprogrammer starts preparing his program. The programmer designs an algorithm to help+isuali/e possible alternati+es in a program.

    !. FLOWCHARTING

    A flow chart is a graphical representation of a program which helps a programmer to decide on+arious data processing procedures with the help of labeled geometrical diagrams. A flow chartis mainly used to describe the complete data processing system including the hardware de+ices

    and media used. t is +ery necessary for a programmer to now about the a+ailable de+icesbefore de+eloping a program.

    ". CODING OR WRITING THE PROGRAM

    The next 9ob after analysing the problem is to write the program in a high-le+el language, usuallycalled coding. This is achie+ed by translating the flow chart in an appropriate high-le+ellanguage, of course according to the syntax rules of the language.

  • 8/12/2019 Chap 4 Programming and Basics

    8/11

    #. TEST EXECUTION

    The process of execution of any program to find out for errors or bugs 5mistaes) is called testexecution. This is +ery important because it helps a programmer to chec the logic of theprogram and to ensure that the program is error-free and worable.

    $. DEBUGGING

    t is a term which is used extensi+ely in programming. *ebugging is the process of detecting,locating and correcting the bugs by running the programs again and again.

    %. FINAL DOCUMENTATION

    t is written information about any computer software. !inal document guides the user about howto use the program in the most efficient way.

    MODES OF OPERATION

    There are two modes of operation for 7A0. The mode that you are in determines what 7A0will do with the instruction you gi+e it. When you start 7A0 you recei+e the "6 prompt. Nouthen ha+e two modes a+ailable to you immediately.

    DIRECT MODE

    n the direct mode 7A0 acts lie a calculator. %o line numbers are reuired. *irect mode isnot of course the main purpose of 7A0, but it is useful at times particularly when you aredebugging program or short problems in which you want to perform uic calculation e.g.,

    P#%T ;CO.

    INDIRECT MODE

    n this mode you first put a line number on each statement. "nce you ha+e a program you canrun it and get your results. The indirect mode sa+es your instructions in the computer along withtheir line number, you can execute the program as many times as you wish simply by typing#1%.

    STEPS OF PROGRAMMING

    There are fi+e steps in preparing a computer programme which are also called A70*& ofProgramming.

    ANALYSIS

    n this step the system analyst tries to become familiar with the problem. (e has to study theproblem and prepare some notes upon that problem. (e also notes that what is gi+en, what isreuired and what will computer can do.

  • 8/12/2019 Chap 4 Programming and Basics

    9/11

    BLOC&ING

    n this step the programmer con+erts the analyst report to a series of steps through which thecomputer will gi+e the reuired result. The steps are commonly nown as Algorithm. There aredifferent ways to write those detailed seuential steps. The most common method used is flow

    charting. A flow chart is symbolic representation of flow of a programme.

    CODING

    n this step the programmer writes the programme in any computer language. This step is nownas coding. After this programme is fed into the computer and is compiled with the help of agi+en compiler.

    DEBUGGING

    *ebugging is a step in which a programmer corrects a syntax error which may come after the

    compilation.

    TESTING

    Testing is a step where the programmer is finally testing the programme for execution 5there maybe any logical mistae which compiler cannot trace).

    EXECUTION

    n this step we send the programme for execution where company4s data will be fed and process.

    VARIABLES AND CONSTANTS

    VARIABLES

    ariables are names used to represent +alues used in 7A0 programme. The +alues of a+ariable may be assigned by the programmer or it may be assigned as the result of calculation inthe programme. 7efore a +ariable is assigned a +alue is assumed to be /ero 5or null for string+ariable).

    VARIABLES NAMES ' DECLARATION CHARACTERS

    ariable names may be of any length up O3 characters are significant. ariable name can containletters, numbers and decimal point. (owe+er, the first character must be a letter. A +ariableshould not be a reser+e word.ariable may represent either a numeric +alue or a string.

    NUMERIC VARIABLE

    %umeric +ariable names may be declared as integer, single precision, double precision +alues.The type of declaration characters for these +ariable names are as follows.

  • 8/12/2019 Chap 4 Programming and Basics

    10/11

    STRING VARIABLE

    n programming we often use to handle alphanumeric or string +ariables. tring +ariables consistof names, addresses or in general any string or series of letters, number or special characters.Without the use of string +ariable whene+er any differentiation needs to be made e.g., fromemployee to employee or from application no. to application no., a number has to be assign to

    each employee or each application.A string +ariable name starts with an alphabet and terminate with a dollar sign 5). n a string the+alue must be in uotation mars. %o arithmetic operations except addition and in addition the+alues do not add they are 9ust written together. The maximum number of character allowed in astring +aries from system to system but a typical +alue is .

    CONSTANTS

    0onstants are those that do not change while executing the programme. 7A0 deals with twotypes of constant.2. %umeric constant

  • 8/12/2019 Chap 4 Programming and Basics

    11/11

    n recent years some new +ersion of 7A0 ha+e been de+eloped which included a +ariety offeatures that are not included in more traditional +ersions.

    STRUCTURE OF BASIC PROGRAM

    &ach instruction in a 7A0 program is written as a separate statement. Thus a program will becomposed of a seuence of statements.The following rules apply to all 7A0 statementsM2. &+ery statement must appear on a separate line number.