INTRODUCTION - Concepts and Principles in Programming
INTRODUCTION - 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?
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:
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.
Programming languages fall into two fundamental categories – low-level and high-level languages. Low-
level 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 00011001
01111010 11010001 10010100
10011111 00011001
01011100 11010001 10010000
10111011 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 0, SUM
MOV NUM, AC
ADD SUM, AC
STO 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
to immediately see results. They are popular in program-development environments in which new features
are being added and errors are corrected.
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.
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.
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.
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.
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 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.
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.
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.
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
Flowcharts are usually drawn using some standard symbols as illustrated below.
a. In drawing a proper flowchart, all necessary requirements should be listed out in logical order.
b. The flowchart should be clear, neat and easy to follow. There should not be any room for ambiguity
in understanding the flowchart.
c. The usual direction of the flow of a procedure or system is from left to right or top to bottom.
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.
g. Write within standard symbols briefly. As necessary, you can use the annotation symbol to describe
data or computational steps more clearly.
h. 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.
i. Ensure that the flowchart has a logical beginning and end.
j. It is useful to test the validity of the flowchart by passing through it with a simple test data.
Example 1:
START
SUM = 0
N=0
N=N+1
SUM = SUM + N
NO
Is N =
50?
YES
Print
SUM
END
Example 2:
NO NO
END
Example 3:
START
INPUT
N
M=1
F=1
F=F*M
NO
M=M+1 IS
M
YES
PRINT
F
END
• 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:
Pseudocode:
1. Assign 0 as default value for SUM (holds the sum of the first 50 numbers) and N (the number from
1 to 50).
2. Add 1 to the value of N (until N reaches 50).
3. Add N to the value of SUM.
4. Check if N is equal to 50.
• If YES proceed to step 5.
• If NO go back to step 2.
5. Display SUM.
Example 2:
Pseudocode:
3. Display A.
4. Display B.
5. Display C.
Example 3:
Pseudocode: