1.programming Concepts
1.programming Concepts
PROGRAMMING CONCEPTS
Program Planning and Development
Each program has to be well thought of. Instructions must be ordered in a logical
sequence that can easily be understood by the computer. The problem must be analyzed to such
a level of detail that all logical conditions that may be encountered during the process are taken
into consideration. Otherwise, wrong results will be obtained.
There are 5 stages in developing a program: (1) defining the problem; (2) designing a
solution; (3) writing the program; (4) compiling, debugging and testing the program; (5) documenting
the program.
The first step in defining a problem begins with recognizing the need for information.
This information may be a request for a solution to a particular problem. The problem is then
thoroughly analyzed in order to determine what is required of its solution.
To fully analyze the problem, there is a need to define what outputs are required of the
program. The programmer must determine what data are needed, what form they are to be in,
what information is to be outputted, and how the data are to be manipulated to produce this
output.
Defining a Solution
When the definition of the problem is finished, the design of the solution begins.
Designing the solution requires breaking the problem into discrete steps. This sequence of steps,
a strategy for solving the problem is called an algorithm.
Flowcharting
Pseudocode
A pseudocode is a version of the instructions describing each step the computer must
follow. It is written in an abbreviated form of spoken language and there lies somewhere
commands written in ordinary English and those in a computer language.
Writing the Program
After the programmer has defined the problem and designed a solution, the next step is
to write the program in a specific programming language. Although the definition and solution
of the problem are independent of the programming language, the proposed solution to the
problem may limit the choices of languages that can be used.
Despite the fact that programming languages differ in the commands they use, most
high-level programming languages have certain types of programming statements in common.
These are comments, declarations, input/output statements, computations, transfer of control,
and comparison.
Comments are statements that have no effect on the program. They are simply used to
make the program easier to understand. They are inserted at key points in the program and
serve as an internal documentation of the program.
The programmer uses declarations to define items used in the program. Examples
include definitions of files, records, initial values, reusable functions and the like.
Input/output statement transfer data to and from the primary storage for use by the
program, as well as to and from other I/O devices like the monitor and the keyboard.
Commands such as READ and PRINT are examples of these types of statements.
Comparisons allow two items to be compared. Based on the result of the comparison,
input/output, computation, or transfer of control could occur.
As the program is being coded, the programmer should be aware that although
generating the correct output is the primary goal of the program, it is not the only requirement
of a good program. The programmer should try to incorporate the following qualities into any
program:
Programs should be easy to read and understandable. Data names should be descriptive.
Statements should be placed in a format that is easy to read and follow. Placing enough
comments can help in making the program easier to understand.
Programs should be efficient. Programs should execute as little time as possible.
Programs should be reliable. Programs should consistently produce the correct output.
All formulas and computations, as well as all logic test and transfer control, must be
accurate.
Programs must be robust. Programs should work under all conditions. Reliability alone
is no guarantee for a successful program. Internal logic maybe correct but an incorrect
data item could produce an incorrect output. For example, how a program would react
if a person’s age were 2, 45 or -54?
Programs should be maintainable. They should be easy to update and modify.
Programs should be written in independent modules so that a change in one module
does not necessitate a change in others.
Instructions must be translated into machine language before they can be executed. A
compiler is a special program for each programming language that is located into the computer
when that language is used. It translates each line of code into the machine instruction that can
be understood by the computer.
The process of documentation is an ongoing one. It begins with the initial request for
information. The individual making the request should be identified. So should those
responsible for designing the system and required programs.
During the problem definition stage, the problem should be described clearly in a short
narrative statement. The objectives of the program should be included with the problem
statement. Several other descriptions are needed.
A complete description of the contents and formats of all data inputs, outputs and files
to be used.
A statement of the hardware requirements for running the program, as well as
estimated processing time and storage requirements.
A statement of software requirements, such as utility programs and library programs.
In the planning phase, the most important documentation produced is the flowchart.
Descriptive comments may be included for each processing step. The test data used to test the
program should also be included.
Programming Language
Machine Language
It is the only language that the computer understands. It consists only of numbers. Each
different type of CPU has its own unique machine language. A programming language, on the
other hand defines a vocabulary and a set of grammatical rules for instructing a computer to
perform specific tasks. Each language has a unique set of keywords and a special syntax for
organizing program instructions.
Machine Language is the lowest level programming language. It is the only language
understood by computers and consists of pure numbers. Machine language functions as the
target language of other language programs. The other language programs must be translated
into machine language before the computer can execute the instructions. Because the data in
digital computers is stored as either on or off electrical states, machine language takes the form
of either 1 or 0.
Low-Level Language
Low level languages are also called Assembly language and are similar to machine
language. But in contrast, assembly language is much easier to understand than a machine
language. Assembly languages were developed to overcome the disadvantage of machine
language. Instead of using 1’s and 0’s, to specify machine instructions, programmers use
mnemonics or machine op codes. The mnemonics are English-like abbreviations for the
machine language instructions.
Programmers who write low-level language programs must be highly skilled in two
areas. First, they must know a great deal about the internal workings of the microprocessor, and
a broad technical knowledge of the computer is needed. Secondly, the programmer must be
detailed oriented, every step of the computer must be coded and the actual numerical address
of the instructions and data must be specified.
High-level programming languages enable the programmer to write programs that are
more or less independent of a particular type of computer. Such languages are considered high-
level because they are closer to human language than the machine language the computer truly
understands. The main advantage of a high level language over low level language is that they
are easier to understand. They allow the programmers to focus on solving the problem rather
than knowing how to program the computer.
Ultimately, programs written in a high level language must be translated into machine
language. Examples of High Level Languages are C, FORTRAN and Pascal. Since their development
in the 1950’s, many other languages have been developed today. These include Ada, Algol,
BASIC, COBOL, C++, LISP and Prolog. Most programming languages were created to serve
specific purposes, such as teaching programming concepts, aiding in scientific research, creating
graphics or controlling input and output devices. However, programmers found that languages
could develop for one purpose, with few modifications serve equally well in writing other
programs.
The question of what programming language to use depends on the type of problem
you want to solve. Every language has its advantage and disadvantage. For example FORTRAN
is a good language for processing numerical data, but it is not good in organizing very large
programs. C language is a very good language for writing well-structured and readable
programs. C++ has a powerful object oriented capability but it is complex to learn.
1. BASIC
BASIC is an acronym for Beginner’s All-Purpose Symbolic Instruction Code. It
was developed by John Kemeny and Thomase Kurtz in the early 1960’s. Their goal was
to create a language that could be easily learned and that would help students to
understand programming. BASIC is a simplified version of the first high-level language
that was FORTRAN.
An example of a BASIC program that will multiply two numbers, 243 and 87 and
printing the results is shown.
10 LET A = 87
20 LET B = 243
30 LET C = A * B
40 PRINT A; “ TIMES ” ;
50 PRINT B; “ = “; C
60 END
2. FORTRAN
An example of a FORTRAN program that will multiply two numbers, 243 and 87
and printing the result is shown.
INTEGER A, B, C
A = 87
B = 243
C = A * B
WRITE (6,50) A, B, C
FORMAT (1X, I3M ‘ TIMES ‘, * I3, ‘=’ , I6)
STOP
3. COBOL
COBOL is short for Common Business-Oriented Language and it is the most
frequently used business programming language. It is used extensively in business,
education and government. COBOL was stated in the early 1960’s when a committee led
by the U.S. Department of Defense and other computer users and manufacturers, known
as CODASYL ( Conference of Data Systems Language ) wanted to develop a common
business programming language. They were the ones who established the specifications
for the COBOL language. Dr. Grace Murray Hopper was a major contributor to the
structure and development of the COBOL language. By mid 1960’s the first commercial
version of the COBOL language was offered. In 1968 ANSI published guidelines for
standardized COBOL that became known as ANSI COBOL.
An example of a COBOL program that will multiply two numbers 243 and 87
and printing the result is shown.
IDENTIFICATION DIVISION
PROGRAM-ID. SAMPLE.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE COMPUTER. IDIAC-747.
OBJECT COMPUTER. IDIAC-747.
DATA DIVISION.
WORKING–STORAGE SECTION .
01 A; PICTURE IS S9 (3) .
01 B; PICTURE IS S9 (3) .
01 C; PICTURE IS S9 (3) .
PROCEDURE DIVISION.
000–MAIN–LINE.
MOVE 87 TO A.
MULTIPLY B GIVING C.
4. PASCAL
PASCAL was the language named after the seventeenth century French
mathematician Blaise Pascal who constructed one of the first adding machines. It is a
high-level language developed by Nicklaus Wirth of Zurich Switzerland in the late
1960’s. Pascal is one of the first languages developed using a structured programming
approach. Pascal reflects a top-down modular programming design. It forces the
programmers to design programs methodically and carefully. It is for this reason that
Pascal became a popular teaching language. Pascal language is a balance between a
wordy language like COBOL and terse language like FORTRAN. Originally intended
for teaching purposes, Pascal rapidly expanded and was used in business and scientific
applications.
An example of a PASCAL program that will multiply two numbers, 243 and 87
and printing the result is shown.
PROGRAM SAMPLE;
VAR
A, B, C : INTEGER;
BEGIN
A := 87;
B := 243;
C := A * B;
WRITELN(OUTPUT, ‘ ‘, A, ‘TIMES’ , B, ‘ = ‘, C) ;
END.
5. C LANGUAGE
C is a high-level programming language developed by Dennies Ritchie at the
Bell Laboratories in 1972. The language was named C, because it was influenced by
another language called B developed by Ken Thompson. It was originally designed as a
systems programming language, which means that it was used to write operating
system programs. The UNIX operating system and its utilities were written in over
300,000 lines of C code.
An example of a C program that will multiply two numbers, 243 and 87 and
printing the results is shown.
#include <stdio.h>
void main()
{
int a, b, c;
a = 87;
b = 243;
c = a * b;
printf(“\n%d times %d equals %d\n”, a,b,c);
}
6. C++ LANGUAGE
During the late 1980’s until the early 1990’s object-oriented programming
started to become popular. This led to the development of object-oriented languages:
Object-Pascal, Modula-2, Mesa, Cedar, Neon, Objective-C, LISP and C++. C++ was
developed by Bjarned Stroustrup while working at the Bell Laboratories. C++ is actually
an extension of C, and while it is not entirely a new language, significant extensions to
the C language have been adopted. C++ is considered a superset of C, and it has the
same capabilities as C with added object-oriented features. C++ is a powerful language,
but the languages still has the flexibility and efficiency of C with added support for
creating object classes. Although C++, was originally designed to aid in the management
of very large programs, its applications are not limited to this. The object-oriented
capabilities of C++ can be applied to virtually any programming task. C++ is one of the
most powerful and efficient programming languages today.
An example of C++ program that will multiply two numbers, 243 and 87 and
printing the result is shown.
#include <iostream.h>
void main()
{
int a = 243, b = 87, c;
c = a * b;
cout<< a<<” times “<<b <<” equals “ << c;
}
7. JAVA
The JAVA programming language is the popular programming for creating
applications on the Web. These applications are called “applets” because they are small
application program. Before the development of Java web pages were viewed as static
documents on the Web browser. These “static” web pages were creating using HTML
(Hypertext Markup Language) and most companies organize web pages using CGI
(Common Gateway Interface).
Java, whose original name was Oak, was developed in December 1990 by Un
Microsystems. Java was created as a programming tool, part of the Green project of Sun
whose task was to create something new and exciting. The original team members of the
Green project (also known as the secret “Green Team”) were Patrick Naughton, Bill Joyo
and James Gosling and were later joined by Chris Warth, Ed Frank and Craig Forrest.
Regardless of what type of language you use, eventually you will convert your
programs into a machine language so that the computer can understand it. When we write our
programs in a high-level language the computer cannot understand the language because it
only operates in bit. There is a need for translation process that will convert the programs
written in high-level language into machine language. There are two ways of doing this either
compile the program or interpret the program. Compiling and interpreting a program can be
accomplished by using other programs called compilers and interpreters, these programs
translate the program written into a machine language.
Compiler
Interpreter
The advantage of an interpreter, however, is that it does not need to go through the
compilation stage during which machine instructions are generated. This compilation process
can be time-consuming if the program is long. The interpreter, on the other hand, can
immediately execute high-level programs.