Introduction - Concepts And Principles In Programming

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Introduction - Concepts And Principles In Programming as PDF for free.

More details

  • Words: 2,699
  • Pages: 9
CONCEPTS

AND

PRINCIPLES

IN

PROGRAMMING

Introduction A 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 needs knowledge of the basic computer architecture concepts to clearly 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 executes the instructions in memory to perform some transformation 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 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. 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, people who write programs use different programming languages to communicate 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 is the instruction for solving a problem or sub-problem in a finite amount of time using a finite amount of data. An algorithm is a verbal or written description of a logical sequence of 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. 3. 4. 5.

Look up the employee’s pay rate. Determine the hours worked during the week. If the number of hours worked is less than or equal to 40, multiply the hours by the pay rate to calculate regular wages. If the number of hours work is greater than 40, multiply 40 by the pay rate to calculate 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 wages to the overtime wages (if any) to determine total wages for the week.

Information and Communication Technology Department Palompon Institute of Technology

i

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 Programming languages fall into two fundamental categories – low-level and high-level languages. Lowlevel languages are machine-dependent; that is, every computer has a unique machine language instruction. In contrast, high-level languages are machine-independent and can be executed on various computers. Low-level Languages: Machine Language is the “natural 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 the machine language is assembly language, which allows “higher-level” symbolic programming. Instead of writing programs as a sequence of bits, assembly language allows programmers to write programs by using symbolic operation codes. Translator programs 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, to represent instructions. For example, most assembly languages use the mnemonic ADD to represent “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 to accomplish even the simplest tasks. To speed the programming process, high-level 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. This process can take a considerable amount of computer time. In this connection, interpreter programs were developed to translate English language statements into machine code and immediately executing the code. Interpreters are very flexible and allow the programmer Information and Communication Technology Department Palompon Institute of Technology

ii

CONCEPTS

AND

PRINCIPLES

IN

PROGRAMMING

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 program becomes the object program. This object 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 languages or interpreted languages. Compiler – a translation program that convert the programmer’s entire high-level program, which is called the source code, into a machine language code, which is called the object code. This translation process is called COMPILATION. 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 than interpreted languages as they can be executed 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 Many programmers plan their programs using a sequence of steps, referred to as the program development 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 Identify exactly what needs to be done. In this step you break the problem into its basic components for analysis using the “divide and conquer” strategy. Be sure you understand what the program should do (output). Have a clear idea of what data are given (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.

Information and Communication Technology Department Palompon Institute of Technology

iii

CONCEPTS

AND

PRINCIPLES

IN

PROGRAMMING

The General Design. The general design of the program is oriented primarily 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. In the detailed design you will produce a graphic representation of the program logic that includes all processing activities and their relationships, calculations, data manipulations, logic operations, and all input/output. 3.

Code the Program Coding is the technical word for writing the program. Whether you “write” or “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 the process of finding bugs in a program. 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 (with internal comments).

Programming Tools A number of programming tools are available to help programmers analyze a problem and design a program. Two most popular tools are flowcharts and pseudocode. •

FLOWCHART

A 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.

Information and Communication Technology Department Palompon Institute of Technology

iv

CONCEPTS

AND

PRINCIPLES

Terminal

IN

PROGRAMMING

Used to represent the beginning (Start) or the end (End) of a task.

Input/Output

Used for input and output operations, such as reading and printing. The data to be read or printed are described inside.

Processing

Used for arithmetic and data-manipulation operations. The instructions are listed inside the symbol.

Decision

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 join different flowlines.

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.

Annotation

Used to provide additional information about another flowchart symbol.

The following are some guidelines in flowcharting: a. b. c.

In drawing a proper flowchart, all necessary requirements should be listed out in logical order. The flowchart should be clear, neat and easy to follow. There should not be any room for ambiguity in understanding the flowchart. The usual direction of the flow of a procedure or system is from left to right or top to bottom.

Information and Communication Technology Department Palompon Institute of Technology

v

CONCEPTS

AND

PRINCIPLES

IN

PROGRAMMING

d.

Only one flow line should come out from a process symbol.

e.

Only one flowline should enter a decision symbol, but two or three flow lines, one for each possible answer, should leave the decision symbol.

f.

Only one flowline is used in conjunction with terminal symbol.

g.

Write within standard symbols briefly. As necessary, you can use the annotation symbol to describe data or computational steps more clearly. This is top secret data.

h. i. j.

If the flowchart becomes complex, it is better to use connector symbols to reduce the number of flow lines. Avoid the intersection of flow lines if you want to make it more effective and better way of communication. Ensure that the flowchart has a logical beginning and end. It is useful to test the validity of the flowchart by passing through it with a simple test data.

Information and Communication Technology Department Palompon Institute of Technology

vi

CONCEPTS

AND

PRINCIPLES

IN

PROGRAMMING

Some examples on Flowcharting: Example 1: Problem 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

vii

CONCEPTS

AND

PRINCIPLES

IN

PROGRAMMING

Example 2: Problem Specification: Draw a flowchart to find the largest of three numbers. Problem Specification: Draw a flowchart for computing Factorial n (n!), where n! = 1 ‘ 2 ‘3.....n

YES

START

INPUT A, B, C

NO

IS B>

IS A>

YES

NO PRIN T

IS A>

YES

NO

PRIN T

PRIN T

PRIN T

END Example 3: START

INPUT N

M=1 F=1

F=F*M

M=M+1

NO

IS M YES PRINT F

END

Information and Communication Technology Department Palompon Institute of Technology

viii

CONCEPTS



AND

PRINCIPLES

IN

PROGRAMMING

PSEUDOCODE

Pseudocode consists of short, English phrases used to explain specific tasks within a program’s algorithm. Pseudocode should not include keywords in any specific computer languages. It should be written as a list of consecutive phrases. Pseudocode looks more like a computer code than does a flowchart. It allows the programmer to focus on the steps required to solve a problem rather than on how to use the computer language. Pseudocode Examples: Example 1: Problem Specification: Find the sum of the first 50 numbers. Pseudocode:

1. 2. 3. 4.

5.

Assign 0 as default value for SUM (holds the sum of the first 50 numbers) and N (the number from 1 to 50). Add 1 to the value of N (until N reaches 50). Add N to the value of SUM. Check if N is equal to 50. • If YES proceed to step 5. • If NO go back to step 2. Display SUM.

Example 2: Problem Specification: Find the largest of three numbers. Pseudocode:

1. 2.

Prompt the user to enter three numbers (A, B, C). Compare A if it is greater than B (is A > B?). • If YES, compare A if it is greater than C (is A > C?). • If YES proceed to Step 3. • If NO proceed to Step 5. • If NO, compare B if it is greater than C (is B > C?). • If YES proceed to Step 4. • If NO proceed to Step 5.

3. 4. 5.

Display A. Display B. Display C.

Example 3: Problem Specification: Find the sum of two integers. Pseudocode: 1. 2. 3. 4.

Prompt the user to enter the first integer. Prompt the user to enter the second integer. Compute the sum of the two integers. Display the result.

Information and Communication Technology Department Palompon Institute of Technology

ix

Related Documents