CONCEPTS
AND
PRINCIPLES
IN
PROGRAMMING
Introduction
A computer (or computer (or computer system) is basically an electronic machine that can carry out specific tasks by following sequences of instructions within a program. A program includes such a sequence of instructions together with data definitions. The computer executes the program by performing one instruction after the other in the specified order. The programmer programmer needs knowledge knowledge of the basic computer computer architectur architecture e concepts concepts to clearly clearly understand understand the structure of the computer to be able to develop solutions to problems, and to use the computer as a tool to execute the solutions to problems. All computer systems have the fundamental functions: input, processing, and output. Input involves entering data into the computer for processing. Data is entered into the computer with an input device (for example, the keyboard).
Processing execut executes es the instru instructi ctions ons in memory memory to perfor perform m some some transf transform ormati ation on and/or and/or computation on the data in the computer’s memory. This emphasizes the fact that the instructions and data must be located in memory in order for processing to proceed.
Output involves transferring data from the computer to an output device such as the computer screen or monitor.
What is PROGRAMMING?
Programming is Programming is the process of writing a sequence of instructions to be executed by a computer to solve a problem. It is also considered as the act of writing computer programs. Computer programs are set of instructions that tell a computer to perform certain operations. The instructions in programs are logically sequenced and assembled through the act of programming programming . Computer programming has many facets: It is like engineering because computer programs must be carefully designed to be reliable and inexpensive to maintain. It is an art because good programs require that the programmer use intuition and a personal sense of style. It is a literary effort because programs must be understood by computers, and this requires mastery of a programming language. Programmers, Programmers, peop people le who who writ write e prog progra rams ms use use diff differ eren entt programmi programming ng languages languages to communicat communicate e instructions to the computer. The programmer begins the programming process by analyzing the problem, breaking it into manageable pieces, and developing a general solution for each piece called an algorithm. algorithm. Algorithm is the instruction for solving a problem or sub-problem in a finite amount of time using a finite amount amount of data. An algorithm algorithm is a verbal or written written description description of a logical sequence of actions actions applied to objects. Suppose a programmer needs an algorithm to determine an employee’s weekly wages. The algorithm reflects what would be done by hand: 1. 2.
Look Look up up the the empl employ oyee ee’s ’s pay pay rat rate. e. Determ Determine ine the the hour hours s worked worked during during the the week week..
Information and Communication Technology Department Palompon Institute of Technology
i
CONCEPTS
3. 4.
5.
AND
PRINCIPLES
IN
PROGRAMMING
If the number number of hours hours worked worked is less than than or equal equal to 40, multipl multiply y the hours hours by the pay rate rate to calculate regular wages. If the number number of hours hours work is is greater greater than 40, multip multiply ly 40 by the pay pay rate to calcula calculate te regular regular wages, and then multiply the difference between the hours worked and 40 by 1 ½ times the pay rate to calculate overtime wages. Add the regular regular wages wages to the overti overtime me wages (if (if any) to determ determine ine total total wages for for the week. week.
Information and Communication Technology Department Palompon Institute of Technology
ii
CONCEPTS
AND
PRINCIPLES
IN
PROGRAMMING
Programming Languages
A programming language is a set of rules, symbols, and special words that provides a way of instructing the computer to perform certain operations. It has a well-defined set of syntax and semantic rules. The syntax rules describe how to write program statements. The semantic rules describe the meaning of those program statements. These two types of rules must be consistent.
Categories of Programming Languages
Programmi Programming ng languages languages fall into two fundamenta fundamentall categories categories – low-level and high-level languages. Low- level languages languages are machin machine-d e-depe epende ndent; nt; that that is, every every comput computer er has a unique unique machin machine e langua language ge instruction. In contrast, high-level languages are machine-indepe machine-independent ndent and can be executed executed on various various computers. Low-level Languages: Machine Language is the “natural “natural language” language” of the computer system. It is the only programming language the CPU can understand. In machine language, instructions are coded as a series of 1s and 0s. It was simply too slow, tedious, and time consuming. A program written in machine language might look like this: 10110011 01111010 10011111 01011100 10111011
00011001 11010001 10010100 00011001 11010001 10010000 11010001 10010110
One level above above the machine machine langua language ge is assembly language, which h allo allows ws “higher-level ” symbolic symbolic language, whic programming. Instead of writing programs as a sequence of bits, assembly language allows programmers to write write progra programs ms by using using symbol symbolic ic operat operation ion codes. codes. Transl Translato atorr progra programs ms called called assemblers were developed to convert early assembly-language programs to machine language at computer speeds for program execution. Assembly languages uses easily recognized symbols, called mnemonics, mnemonics , to represent instructio instructions. ns. For example, example, most assembly assembly languages languages use the mnemonic ADD to repres represent ent “ Addition” Addition” instruction. A program written in assembly language might look like this: MOV MOV ADD STO
0, SUM NUM, AC SUM, AC SUM, TOT
Compared to writing programs in machine language, writing programs in assembly language is much faster, but not fast enough for writing complex programs. High-level Languages: Computer usage increased rapidly with the advent of assembly languages, but programmers still had to use many instructions instructions to accomplish accomplish even the simplest tasks. To speed the programmin programming g process, process, high-level Information and Communication Technology Department iii Palompon Institute of Technology
CONCEPTS
AND
PRINCIPLES
IN
PROGRAMMING
languages were developed in which single statements could be written to accomplish substantial tasks. High-level languages allow programmers to write instructions that look almost like everyday English and contain commonly used mathematical notations. Translator programs called compilers convert high-level language programs into machine language. The process of compiling a high-level language program into machine language is called compilation. compilation. This process can take a considerable amount of computer time. In this connection, connection, interpreter programs programs were developed to translate translate English English language language statements statements into machine code and immediately executing the code. Interpreters are very flexible and allow the programmer to immediately see results. They are popular in program-development environments in which new features are being added and errors are corrected. An example of a program written in high-level language might look like this: X = (Y + Z) / 2
In high-level programming language, a source program is needed by the compiler for compilation. A source program is a program written in a particular programming language. In the process of compilation, a source source program program becomes the object object program program. This object object program program is necessary in the next step after compilation, called linking , which will generate an executable version of the object program for program execution.
Compilers and Interpreters
For a high-level language to work on the computer it must be translated into machine language. There are two kinds of translators – compilers and interpreters – and high-level languages are called either compiled either compiled languages or interpreted or interpreted languages. languages. Compiler – Compiler – a translation program that convert the programmer’s entire high-level program, which is called the source code, code, into a machine language code, which is called the object code. code. This translation process is called COMPILATION . Interpreter – Interpreter – a translation program that converts each program statement (line by line) into machine code just before the program statement is to be executed. Translation and execution occur immediately, one after another, one statement at a time. Unlike the compiled languages, no object code is stored and there is no compilation. This means that in a program where one statement is executed several times, that statement is converted to machine language each time it is executed. Compiled languages are better better than than interp interpret reted ed langua languages ges as they they can be execut executed ed faster faster and more efficiently once the object code has been obtained. On the other hand, interpreted languages do not need to create object code and so are usually easier to develop – that is, to code and test.
Program Development Cycle
Information and Communication Technology Department Palompon Institute of Technology
iv
CONCEPTS
AND
PRINCIPLES
IN
PROGRAMMING
Many Many prog progra ramm mmer ers s plan plan thei theirr prog progra rams ms usin using g a sequ sequen ence ce of step steps, s, refe referr rred ed to as the the program development cycle. cycle. The following step-by-step process will enable you to use your time efficiently and help you design programs that produce the desired output. 1.
Define and Analyze the Problem Identi Identify fy exactl exactly y what what needs needs to be done. done. In this step step you break the proble problem m into into its basic components for analysis using the “divide “divide and conquer” strategy. conquer” strategy. Be sure you understand what the program should do (output (output ). ). Have a clear idea of what data are given (input ( input ) and the relationship between the input and the desired output.
2.
Design the General and Detailed Logic of the Program At this point you need to put the pieces together in the form of a logical program design. A program is designed in a hierarchical manner – that is, from general to specific. The General General Design Design.. The general general design design of the program program is orient oriented ed primar primarily ily to the major processing activities and the relationships between these activities. By first completing a general program design, you make it easier to investigate alternative design approaches. Once you are confident of which approach is best, you may complete a more detailed design. The Detailed Design. Design. In the detailed design you will produce a graphic representation of the program program logic that includes includes all processing activities activities and their relationships, relationships, calculations, calculations, data manipulations, logic operations, and all input/output.
3.
Code the Program Coding is Coding is the technical word for writing the program. Whether you “write “ write”” or “code “code”” the program is a matter of personal preferences. During this stage, the design of the program is translated into machine-readable instructions, or programs.
4.
Test and Debug the Program Once the program has been entered into the system, it is likely that you will encounter at least one of those cantankerous bugs. Bugs are mistakes or faults found within the program. Testing is Testing is the process of finding bugs in a program. Debugging is Debugging is the process of correcting and removing bugs that are found.
5.
Document the Program Documentation is an important part of the programming process. Documentation is the written text and comments that make a program easier for others to understand, use, and modify. It includes written explanations of the problem being solved and the organization of the solution, comments embedded within the program itself, and user manuals that describe how to use the program. At a minimum, the documentation package for each program should include a program description, a structure chart, a flowchart, and a program listing ( listing (with internal comments). comments).
Programming Tools
A number number of progra programmi mming ng tools tools are availa available ble to help help progra programme mmers rs analyz analyze e a proble problem m and design design a program. Two most popular tools are flowcharts and pseudocode and pseudocode..
Information and Communication Technology Department Palompon Institute of Technology
v
CONCEPTS
•
AND
PRINCIPLES
IN
PROGRAMMING
FLOWCHART
A flowchart is flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed to get the solution of a problem. Flowcharts are generally drawn in the early stages of formulating computer solutions. These flowcharts play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems. Once the flowchart is drawn, it becomes easy to write the program in any high level language. Flowcharts are usually drawn using some standard symbols as illustrated below. Symbol
Name
Meaning
Flowline
Used to connect symbols and indicate the flow of logic.
Terminal
Used to represent the beginning (Start) or the end (End) of a task.
Input/Output
Processing
Decision
Used for input and output operations, such as reading and printing. The data to be read or printed are described inside.
Used for arithmetic and data-manipulation operations. The instructions are listed inside the symbol.
Used for any logic or comparison operations. Unlike the input/output and processing symbols, which have one entry and one exit flowline, the decision symbol has one entry and two exit paths. The path chosen depends on whether the answer to a question is “yes” or “no”.
Connector
Used to to jo join di different flflowlines.
Off-page Connector
Used to indicate that the flowchart continues to a second page.
Predefined process
Used to represent a group of statements that perform one processing task.
Information and Communication Technology Department Palompon Institute of Technology
vi
CONCEPTS
AND
Annotation
PRINCIPLES
IN
PROGRAMMING
Used to provide additional information about another flowchart symbol.
The following are some guidelines in flowcharting: a. b. c.
In drawing drawing a proper proper flowchart, flowchart, all all necessary necessary requiremen requirements ts should should be listed listed out in logical logical order. order. The flowchart flowchart should should be clear, clear, neat neat and easy easy to follow. follow. There There should should not be any any room for for ambiguity ambiguity in understanding the flowchart. The usual usual direction direction of the the flow of a procedur procedure e or system is from left left to right right or top to to bottom. bottom.
Information and Communication Technology Department Palompon Institute of Technology
vii
CONCEPTS
AND
PRINCIPLES
IN
PROGRAMMING
d.
Only one flow flow line line should should come out from from a process process symbol. symbol.
e.
Only one flowli flowline ne should should enter a decision decision symbol, symbol, but but two or three three flow flow lines, lines, one for each each possible possible answer, should leave the decision symbol.
f.
Only Only one flow flowlin line e is used used in conju conjunct nction ion with with termi terminal nal symb symbol. ol.
g.
Write within within standard standard symbols symbols briefl briefly. y. As necessary, necessary, you you can use the the annotation annotation symbol symbol to describe describe data or computational steps more clearly.
This is top secret data. h.
i. j.
If the flowchar flowchartt becomes becomes complex, complex, it is better better to use use connector connector symbols symbols to reduce reduce the number number of flow lines. Avoid the intersection of flow lines if you want to make it more effective and better way of communication. Ensure Ensure that that the the flowc flowchar hartt has a logica logicall beginn beginning ing and and end. end. It is useful useful to test the the validit validity y of the flowchart flowchart by passing passing through through it with with a simple simple test data. data.
Information and Communication Technology Department Palompon Institute of Technology
viii
CONCEPTS
AND
PRINCIPLES
IN
PROGRAMMING
Some examples on Flowcharting: Example 1: 1: Problem Specification: Specification: Draw a flowchart to find the sum of first 50 numbers.
START
SUM = 0 N=0
N=N+1
SUM = SUM + N
NO
Is N = 50?
YES
Print SUM
END
Information and Communication Technology Department Palompon Institute of Technology
ix