You are on page 1of 47

Structured Programming

Lecture 02: Introduction to Programming Languages

Md. Zahidul Islam


www.cseku.ac.bd/faculty/˜zahid.

Computer Science & Engineering Discipline


Khulna University
Bangladesh.

Khulna University.
Structured Programming
Introduction

Table of Contents 1 | 46

Elements of a computer system

Computer Program

The Evolution of Programming Languages

How a Program Works

The CPU in More Detail

Abstraction for Programming Languages

Khulna University.
Structured Programming
Elements of a computer system

Elements of a Computer System 2 | 46


• Basic commands of a computer - input (get data), output
(display results), store (save data) and arithmatic and
logical operations.
• The main componets of a computer - hardware and software.
• Hardware is considered as the heart of a computer and
software is the soul.
• Hardware: CPU, RAM, HDD, monitor, mouse, keyboards
etc.
• Software: OS(windows, linux, iOS etc.), microsoft office,
movie players, music players etc.

Khulna University.
Structured Programming
Elements of a computer system

CPU and Main Memory(1/3) 3 | 46


• CPU is the “brain” of a computer - does everything by a set
of arithmatic and logical operations.
• Main memory, or RAM, is connected directly to the CPU.
All programs must be loaded into main memory before they
can be executed.
• Similarly, all data must be brought into main memory before
a program can manipulate it.
• Main memory is an ordered sequence of memory cells.
Each cell has a unique location called the address of the cell.
• Main memory stores everything as sequences of 0s and 1s.
The memory addresses are also expressed as sequences of 0s
and 1s.

Khulna University.
Structured Programming
Elements of a computer system

CPU and Main Memory (2/3) 4 | 46

Khulna University.
Structured Programming
Elements of a computer system

CPU and Main Memory (3/3) 5 | 46


• Main memory, or RAM, is connected directly to the CPU.
• All programs and data must be loaded into main memory
before they can be used by CPU.
• Computer memory is an ordered sequence of cells. Each cell
has a unique location in memory, called the address of the
cell.
• CPU uses the memory cel using these addresses.
• Memory stores everything as sequences of 0s and 1s. The
memory addresses are also expressed as sequences of 0s and
1s.
• Memory works as a group of units (depending on the
machine architecture) together. The smallest unit is called
bits and groups are called bytes, kilo bytes, mega bytes etc.
Khulna University.
Structured Programming
Elements of a computer system

Input and Output Devices 6 | 46


• Everything in RAM is lost when the computer is turned off.
• The device that stores information permanently is called
secondary storage.
• The devices that feed data and programs into computers are
called input devices. The keyboard, mouse, and secondary
storage are examples of input devices.
• The devices that the computer uses to display results are
called output devices. A monitor, printer, and secondary
storage are examples of output devices.

Khulna University.
Structured Programming
Elements of a computer system

Software 7 | 46
• Software are programs written to perform specific tasks. For
example, word processors are programs that are used to
compose documents.
• All software are written in programming languages.
Problem: there are 40 students attending this course. We want to
calculate the grades of each student in the course. The students take
parts in three class tests, best two of those are added to the final
marks. Final 100 marks is calculated with 10 in attendance, 30 in
class tests and 60 from the final exam. Letter grades are assigned as
follows.

Khulna University.
Structured Programming
Computer Program

Computer Program 8 | 46
• All digital computers transmit, store, and manipulate
information.
• Several different types of data can be processed by a
computer. For example - numeric data, character data (or
strings), graphic data (charts, drawings, photographs etc.),
and sound data (music, speech pattern etc.)
• To process particular set of data the computer must be given
an appropriate set of instructions called a program. These
instructions are entered into a computer and then stored into
a portion of computers memory.

Khulna University.
Structured Programming
Computer Program

Basic Instructions 9 | 46
The basic set of instructions those appear in almost every language:
input: Get data from the keyboard, a file, or some other device.
output: Display data on the screen or send data to a file or other
device.
math: Perform basic mathematical operations like addition and
multiplication.
conditional execution: Check for certain conditions and execute
the appropriate sequence of statements.
repetition: Perform some action repeatedly, usually with some
variation.

We can describe programming as the process of breaking a large,


complex task into smaller and smaller subtasks until the subtasks are
simple enough to be performed with one of these basic instructions.

Khulna University.
Structured Programming
Computer Program

The Language of a Computer 10 | 46


• Electrical signals (digital signals) are used inside the
computer to process information.
• Digital signals represent information with a sequence of 0s
and 1s. A 0 represents a low voltage, and a 1 represents a
high voltage.
• Computers understands nothing but a sequence of 0s and 1s.
• The language of a computer is called machine language and
is a sequence of 0s and 1s.
• The digit 0 or 1 is called a binary digit, or bit. A sequence of
0s and 1s is referred to as a binary code or a binary number.

Khulna University.
Structured Programming
Computer Program

Bits, Bytes, Numbers and Characters 11 | 46


Bit: A binary digit 0 or 1.
Byte: A sequence of eight bits.
• Everything we type on our keyboards (letter, number, or special
symbol - ∗, %, & etc.) is encoded as a unique sequence of bits.
• One such encoding scheme is the seven-bit American Standard
Code for Information Interchange (ASCII).
The ASCII data set consists of 128 characters numbered 0
through 127. In this scheme, ‘A’ is encoded as the binary
number 1000001. The character 3 is encoded as 0110011.
• Inside the computer, every character is represented as a sequence
of eight bits i.e., as a byte (a 0 is added to the left of ASCII code).
• Other encoding schemes include EBCDIC (used by IBM, consists
of 256 characters) and Unicode (consists of 65,536 characters.)

Khulna University.
Structured Programming
The Evolution of Programming Languages

Early Languages (1/2) 12 | 46


• Charles Babbage’s difference engine could only be made to
execute tasks by changing the gears which executed the
calculations.
• Physical motion was replaced by electrical signals when the
US Government built the ENIAC in 1942. Could be
“programmed” by presetting switches and rewiring the entire
system for each new “program” or calculation.
• In 1945, John Von Neumann developed two important
concepts:
“Shared-program technique” - the actual computer
hardware should be simple and not need to be hand-wired
for each program. Instead, complex instructions should be
used to control the simple hardware, allowing it to be
reprogrammed much faster.
Khulna University.
Structured Programming
The Evolution of Programming Languages

Early Languages (2/2) 13 | 46


• Von Neumann’s second concept
“Conditional control transfer” - this idea gave rise to the
notion of subroutines.
Subroutines - small blocks of code that could be jumped to
in any order, instead of a single set of chronologically
ordered steps for the computer to take.
The second part of the idea stated that computer code
should be able to branch based on logical statements such as
IF (expression) THEN, and looped such as with a FOR
statement.
“Conditional control transfer” gave rise to the idea of
“libraries,” which are blocks of code that can be reused over
and over.

Khulna University.
Structured Programming
The Evolution of Programming Languages

FORTRAN 14 | 46
• In 1949, the language Short Code appeared. It was the first
computer language for electronic devices and it required the
programmer to change its statements into 0’s and 1’s by
hand.
• In 1951, Grace Hopper wrote the first compiler, A-0.
A compiler is a program that turns the language’s
statements into 0’s and 1’s for the computer to understand.
• In 1957, the first of the major languages appeared in the
form of FORTRAN (FORmula TRANslating system). The
language was designed at IBM for scientific computing.
The basic types of data in use today got their start in
FORTRAN, these included logical variables (TRUE or
FALSE), and integer, real, and double-precision numbers.

Khulna University.
Structured Programming
The Evolution of Programming Languages

COBOL and LISP 15 | 46


• Though FORTAN - good at handling numbers, but not so
good at handling input and output, which mattered most to
business computing.
• In 1959, COBOL was developed as the language for
businessmen. Its only data types were numbers and strings
of text. It also allowed data to be grouped into arrays and
records.
• In 1958, John McCarthy of MIT created the LISt Processing
(or LISP) language. It was designed for Artificial Intelligence
(AI) research.

Khulna University.
Structured Programming
The Evolution of Programming Languages

Pascal and C 16 | 46
• Pascal was begun in 1968 by Niklaus Wirth. Its development
was mainly out of necessity for a good teaching tool. The
combination of features, input/output and solid
mathematical features, made it a highly successful language.
Pascal also improved the “pointer” data type, a very
powerful feature. It also added a CASE statement. Pascal
also helped the development of dynamic variables.
• Dennis Ritchie developed C for the new Unix system being
created at the same time. Unix gives C many advanced
features - dynamic variables, multitasking, interrupt
handling, forking, and strong, low-level, input-output.

Khulna University.
Structured Programming
The Evolution of Programming Languages

OOP and Java 17 | 46


• In the late 1970’s and early 1980’s, a new programing method was
being developed. It was known as Object Oriented Programming,
or OOP.
Objects - pieces of data that can be packaged and manipulated
by the programmer. Bjarne Stroustroup liked this method and
developed extensions to C known as “C With Classes.” This set of
extensions developed into the full-featured language C++, which
was released in 1983.
• In the early 1990’s, interactive TV was the technology of the
future. Sun Microsystems decided that interactive TV needed a
special, portable, language. In 1994, the Java project team
changed their focus to the web, which was becoming “the cool
thing” after interactive TV failed.
It has serious optimization problems - programs written in it run
very slowly.
Khulna University.
Structured Programming
The Evolution of Programming Languages

Visual Basic and Perl 18 | 46


• Visual Basic is often taught as a first programming language today
as it is based on the BASIC language developed in 1964 by John
Kemeny and Thomas Kurtz. Microsoft has extended BASIC in its
Visual Basic (VB) product. The heart of VB is the form, or blank
window on which you drag and drop components such as menus,
pictures, and slider bars. VB is most often used today to create
quick and simple interfaces to other Microsoft products such as
Excel and Access without needing a lot of code, though it is
possible to create full applications with it.
• Perl has often been described as the “duct tape of the Internet,”
because it is most often used as the engine for a web interface or in
scripts that modify configuration files. It has very strong text
matching functions which make it ideal for these tasks. Perl was
developed by Larry Wall in 1987.

Khulna University.
Structured Programming
How a Program Works

Machine Language 19 | 46
Concept: A CPU can only understand instructions that are written in
machine language. As it is very difficult to write entire programs in
machine language, other programming languages have been invented.
• To see how instructions are written in machine language, suppose we
want to use the equation:
wages = rate × hours
to calculate weekly wages. Further, suppose that the binary code
100100 stands for load, 100110 stands for multiplication, and 100010
stands for store. In machine language teh instructions to calculate
weekly wages:
100100 010001
100110 010010
100010 010011
To represent the weekly wages equation in machine language, the
programmer had to remember the machine language codes for various
operations.

Khulna University.
Structured Programming
How a Program Works

Assembly Language 20 | 46
• Assembly languages were developed to make the programmer’s job
easier. An instruction is an easy-to-remember form called a
mnemonic. some examples of instructions in assembly language
and their corresponding machine language code are shown below:
Assembly Language Machine Code
LOAD 100100
STOR 100010
MULT 100110
ADD 100101
SUB 100011
• Using assembly language instructions, you can write the equation
to calculate the weekly wages as follows:
LOAD rate
MULT hours
STOR wages
Khulna University.
Structured Programming
How a Program Works

Assembler 21 | 46
The CPU only understands machine language. A special
program known as an assembler is used to translate an
assembly language program to a machine language program.
The machine language program that is created by the assembler
can then be executed by the CPU.

Khulna University.
Structured Programming
How a Program Works

Fetch-decode-execute Cycle 22 | 46
A CPU executes the instructions in a program in a process that
is known as the fetch-decode-execute cycle. This cycle consists
of the following three steps:
1. Fetch: read the next instruction from memory into the
CPU.
2. Decode: the CPU decodes the instruction that was just
fetched from memory, to determine the operation to perform.
3. Execute: The last step in the cycle is to execute, or
perform, the operation.
These steps are repeated for each instruction in the program.

Khulna University.
Structured Programming
How a Program Works

Fetch-decode-execute Cycle 23 | 46

Khulna University.
Structured Programming
The CPU in More Detail

A Simple Model of Memory 24 | 46


• We think of memory abstractly, as being split into discrete
chunks, each given a unique address
• We can read or write in whole chunks
• Modern memory is big

Khulna University.
Structured Programming
The CPU in More Detail

Model of a CPU 25 | 46

Khulna University.
Structured Programming
The CPU in More Detail

A Simple Command Set 26 | 46


• A program is just a sequence of instructions. The
instructions available depend on the CPU manufacturer.
• We will consider some very simple instruction labels
LIJ: Load value at memory address I into register J
AIJK: Add register I to J and put the result in K
SIJ: Store register I in memory address J

Khulna University.
Structured Programming
The CPU in More Detail

Fetch-Execute Cycle I 27 | 46

Khulna University.
Structured Programming
The CPU in More Detail

Fetch-Execute Cycle II 28 | 46

Khulna University.
Structured Programming
The CPU in More Detail

Fetch-Execute Cycle III 29 | 46

Khulna University.
Structured Programming
The CPU in More Detail

Fetch-Execute Cycle IV 30 | 46

Khulna University.
Structured Programming
Abstraction for Programming Languages

Levels of Abstraction for Programming 31 | 46

Khulna University.
Structured Programming
Abstraction for Programming Languages

High Level Languages (1/2) 32 | 46


• As assembly language is vey close in nature to machine
language, it is referred to as a low-level language.
• In the 1950s, a new generation of programming languages
known as high-level languages began to appear.
• Create powerful and complex programs without knowing the
details of CPU
• No need to write large numbers of low-level instructions.
• High-level languages use words that are easy to understand.
• Allow programmers to concentrate on the tasks they want to
perform with their programs.
• For example, we need to write Display “Hello world” to
display the message “Hello world” on the computer screen.
In assembly language would require several instructions, and
knowledge of how the CPU interacts with the computer’s
video circuitry.
Khulna University.
Structured Programming
Abstraction for Programming Languages

High Level Languages (2/2) 33 | 46


Each high-level language has its own set of words that the
programmer must learn in order to use the language.
Components of a high-level language:
• Key words/reserve words: a set of words that make up a
high-level programming language. Each key word has a specific
meaning, and cannot be used for any other purpose.
• Operators: to perform various operations on data. For example,
math operators performing arithmetic (+, −, ∗, /, %).
• Syntax: a set of rules that must be strictly followed when writing
a program. The syntax rules dictate how key words, operators,
and various punctuation characters must be used in a program.
• Statements: individual instructions that are used to write a
program in a high-level programming language are called
statements. A programming statement can consist of key words,
operators, punctuation, and other allowable programming
elements, arranged in the proper sequence to perform an operation.
Khulna University.
Structured Programming
Abstraction for Programming Languages

Compilers 34 | 46
A programmer writes in a high-level language and uses a compiler or
an interpreter to translation the program to machine language.

Compiler
a program that translates a high-level language program into
corresponding machine language program. The machine language
program can then be executed any time it is needed.

Typically programmers wite the statements in an editor and then


saves the code in a file on the computer’s disk.
Next, the programmer uses a compiler to translate the code into a
machine language program, or an interpreter to translate and execute
the code.
If the code contains a syntax error, it cannot be translated and
When this happens the compiler or interpreter displays an error
message indicating that the program contains a syntax error.
Khulna University.
Structured Programming
Abstraction for Programming Languages

Compilers 35 | 46

Khulna University.
Structured Programming
Abstraction for Programming Languages

Interpreter 36 | 46

Interpreters
a program that both translates and executes the instructions in
a high-level language program. The interpreter reads each
individual instruction in the program, it converts it to a
machine language instruction and then immediately executes it.
This process repeats for every instruction in the program.

Khulna University.
Structured Programming
Abstraction for Programming Languages

Interpreter 37 | 46

Khulna University.
Structured Programming
Abstraction for Programming Languages

Analysis-Coding-Execution Cycle 38 | 46
• Programming is a process of problem solving.
• In a programming environment, the problem-solving process
requires the following three steps:
1. Analyze the problem, outline the problem and its solution
requirements, and design an algorithm to solve the problem.
Algorithm: A step-by-step problem-solving process in which a
solution is arrived at in a finite amount of time.
2. Implement the algorithm in a programming language and
verify that the algorithm works.
3. Maintain the program by using and modifying it if the problem
domain changes.

Khulna University.
Structured Programming
Abstraction for Programming Languages

Analysis-Coding-Execution Cycle 39 | 46

Khulna University.
Structured Programming
Abstraction for Programming Languages

Problem Analysis 40 | 46
Analyzing the problem is the first and most important step in
problem solving.
• Thoroughly understand the problem.
• Understand the problem requirements. Requirements include -
whether the program requires user interaction, whether it
manipulates data, whether it produces output, and what the
output looks like.
• If the problem is complex, divide the problem into subproblems
and repeat the previous steps for the subproblems.
• Your overall programming experience will be successful if you
spend enough time to complete the problem analysis before
attempting to write the programming instructions.
• Usually, this is done on paper using a pen or pencil.

Khulna University.
Structured Programming
Abstraction for Programming Languages

Algortihm Design 41 | 46
• After problem analysis, the next step is to design an
algorithm to solve the problem.
• If the problem is broaken into subproblems, an algorithm is
needed for each subproblem.
• Once an algorithm is designed, we need to check it for
correctness.
• We can test an algorithm’s correctness by using sample data.
Sometimes we might need to perform some mathematical
analysis to test the algorithm’s correctness.

Khulna University.
Structured Programming
Abstraction for Programming Languages

Coding 42 | 46
• After designing and verification of algorithm’s correctness,
the next step is to convert it into an equivalent programming
code.
• Usually a text editor is used to enter the programming code
or the program into a computer.
• Next, correctness of the syntax of the entred program is
verified using a compiler.
• If the compiler generates error messages, we must remove
the errors.
• After reoving all the syntax errors, the compiler generates
the equivalent machine code, the linker links the machine
code with the system’s resources, and the loader places the
program into main memory to be executed.
Khulna University.
Structured Programming
Abstraction for Programming Languages

Execution 43 | 46
• The compiler guarantees only that the program follows the
language’s syntax.
• During execution, the program might terminate abnormally
due to logical errors, such as division by zero.
• Even if the program terminates normally, it may still
generate erroneous results.
• Under these circumstances, we may have to re-examine the
code, the algorithm, or even the problem analysis.

Khulna University.
Structured Programming
Programming Methodologies

Structured Programming 44 | 46
• Structured design
• divide a problem into smaller subproblems.
• analyze and solve each sub problems.
• Combine the solution of each subproblems to solve the overall
problem.
• This process of implementing a structured design is called
structured programming.
• The structured-design approach is also known as top-down
design, bottom-up design, stepwise refinement, and modular
programming.

Khulna University.
Structured Programming
Programming Methodologies

Object-Oriented Programming 45 | 46
• In OOD, the first step is to identify the components called
objects, and to determine how these objects interact with
one another.
• The next step is to specify for each object the relevant data
and possible operations to be performed on that data. An
object combines data and operations on the data into a
single unit.
• The final program is a collection of interacting objects.
• A programming language that implements OOD is called an
object-oriented programming (OOP) language.

Khulna University.
Structured Programming
Programming Methodologies

Aspect-Oriented Programming 46 | 46
• AOP is a programming paradigm that aims to increase
modularity by allowing the separation of cross-cutting
concerns.
• AOP includes programming methods and tools that support
the modularization of concerns at the source code level.
• AOP entails breaking down program logic into distinct parts.
• Some concerns cannot be covered by others form of
implementation (moldularity, function, object orientation
etc.) and are called crosscutting concerns because they “cut
across” multiple abstractions in a program.

Khulna University.

You might also like