Chapter One Class2015
Chapter One Class2015
INTRODUCTION TO PROGRAMMING
1
as English), that are defined by Syntax and semantic rules which describe their structure
and meaning respectively. The syntax of a language describes the possible combinations of
symbols that form a syntactically correct program. The meaning given to a combination of
symbols is handled by semantics. A main purpose of programming languages is to provide
instructions to a computer. Available programming languages come in a variety of forms
and types. Programming languages can be divided in to two major categories: low-level and
high-level languages.
Low-Level Languages
Computers only understand one language and that is binary language or the language in
the form of string of 0s and 1s. Binary language is also known as machine language. Machine
language referred to as first generation language (one of low-level languages). Computer
programming, all the instructions were given in binary form which computer easily
understood.
Assembly language: These languages use symbols or mnemonic codes to describe
machine instructions, which is easier to learn and understand. Assembly language referred
to as second generation language. ADD A, B – adds two numbers in memory location A and
B. The assembly language must be translated to machine code by a separate program called
assembler. The machine instruction created by the assembler from the original program
(Assembly language source code) is called object code.
High-level languages
Although programming in assembly language is not as difficult and error prone as stringing
together ones and zeros, but lack of portability between different computers, this led to the
development of high-level languages which permitted a programmer to ignore many low-
level details of the computer's hardware. High-level languages that look like natural
language which more English-like. High-level languages also require translation to
machine language before execution. This translation is accomplished by either a compiler
or an interpreter. Compilers translate the entire source code program written in high level
languages before execution. Interpreters translate source code programs one line at a time,
before going onto the next instruction. FORTRAN, BASIC, PASCAL, C, C++, Java are some
examples of high-level languages.
2
Major Programming Paradigms
Most fundamental ways programming languages are characterized (categorized) is by
programming paradigm. A programming paradigm provides the programmer's view of
code execution. Three basic programing paradigms are:
Procedural Programming
Structured Programming
Object-Oriented Programming
3
Object-Oriented Programming
Object-oriented programming is one of the newest and most powerful paradigms.
The idea behind OOP is that, a computer program is a collection of cooperating
objects, rather than a list of instructions.
Object-oriented programming is claimed to give more flexibility, easing changes to
programs.
The OOP approach is often simpler to develop and maintain. Example: Java
4
There are two Approaches of Problem Solving:
Top down design: is a systematic approach based on the concept that the structure
of the problem should determine the structure of the solution and what should be
done in lower level. This approach will try to split (disintegrate) a larger problem into
more smaller and manageable problems to narrow the problem domain.
Bottom up design: is the reverse process where the lowest level component are built
first and the system builds up from the bottom until the whole process is finally
completed.
Basic Program Development tips
The program we design in any programming language need to be:
Reliable: the program should always do what it is expected to do and handle all types
of exception.
Maintainable: the program should be in a way that it could be modified and
upgraded when the need arises.
Portable: It needs to be possible to adapt the software written for one type of
computer to another with minimum modification.
Efficient: the program should be designed to make optimal use of time, space and
other resources of the computer.
Algorithm Designing and Modeling the Logic using Flow chart
A digital computer is a useful tool for solving a great variety of problems. Writing a
logical step-by-step method to solve the problem is called the algorithm. A solution to a
problem is called an algorithm, it describes the sequence of steps to be performed for the
problem to be solved. Generally, an algorithm is a finite set of well-defined instructions
for accomplishing some task which, given an initial state, will terminate in a
corresponding recognizable end-state. The algorithm should be: Precise and
unambiguous, Simple, Correct and Efficient.
5
Modeling a Programs Logic using Flow Chart
Algorithm could be designed using many techniques and tools. One tool of designing
algorithm is by using flowcharts. Flowchart is a graphical way of expressing the steps
needed to solve a problem. A flow chart is a schematic (diagrammatic description)
representation of a process.
Basic flowcharting Symbols are:
A flowchart is the graphical or pictorial representation of an algorithm with the help of
different symbols, shapes, and arrows to demonstrate a process or a program. With
algorithms, we can easily understand a program. The main purpose of using a flowchart is
to analyze different methods. Several standard symbols are applied in a flowchart:
Step 1: Initialize X as 0
Step 2: Increment X by 1
Step 3: Print X
Step 4: If X is less than 20
then go back to step 2
Flowchart
7
Compiler and Interpreter
Any program written in a language other than machine language needs to be
translated to machine language. The set of instructions that do this task are known as
translators. There are different kinds of translator software among which compiler
and interpreters are of interest for most programmers’.
Compilers: a compiler is a computer program that translates a serious of statements
written in source code (a collection of statements in a specific programming language)
into a resulting object code (translated instructions of the statements in a programming
language). A compiler changes or translates the whole source code into executable machine
code (also called object code) which is output to a file for latter execution. E.g. C++, Pascal,
FORTRAN, etc.
Interpreters: is a computer program that translates a single high level statement
and executes it and then goes to the next high level language line etc. E.g. QBASIC, Lisp
etc.
C++ Program Execution Phases
C++ programs (source code) typically go through five phases to be executed these are edit,
preprocess, compile, link, and load:
Edit: this is accomplished with an editor program the programmer types C++
statements with the editor and makes corrections if necessary. The programs source
file is then stored on secondary storage device such as a disk with a “.cpp” file name. After
the program is edited, C++ is principally compiled in three phases. Preprocessing,
translation to object code, and linking (the last two phases are what is generally thought of
as the "compilation" process).
Preprocess: In a C++ system, a preprocessor program executes automatically before
the compiler’s translation phase begins. The C++ preprocessor obeys command called
preprocessor directives, which indicate that certain manipulations are to be performed
on the program before compilation. The preprocessor is invoked by the compiler before
the program is converted to machine language. The C++ preprocessor goes over the
program text and carries out the instructions specified by the preprocessor directives
8
(e.g., #include). The result is a modified program text which no longer contains any
directives.
Compile: Then, the C++ compiler translates the program code. The compiler may be a
true C++ compiler which generates native (assembly or machine) code. The outcome may
be incomplete due to the program referring to library routines which are not defined as
a part of the program. For example, the << operator which is actually defined in a
separate IO library.
Linking: C++ programs typically contain reference to function and data defined elsewhere,
such as in the standard libraries. The object code produced by the C++ compiler typically
contains “holes” due to these missing parts. A linker links the object code with the code
for the missing function to produce an executable image (with no missing pieces).
Generally, the linker completes the object code by linking it with the object code of any
library modules that the program may have referred to. The final result is an executable file
Loading: the loader takes the executable file from disk and transfers it into memory.
Additional components from shared libraries that support the program are also loaded.
Finally, the computer, under the control of its CPU, executes the program.
Example: Write the algorithmic description and draw a flow chart to find the following sum.
Algorithmic Solution
#Sum = 1+2+3+…. + 50
Algorithmic description
1. Initialize sum to 0 and counter to 1 Sum = 0
1.1 If the counter is less than or equal to 50 Counter =1
Add counter to sum
Increase counter by 1
Repeat step 1 .1
1.2 else
Exit
2. Write sum 9