Structured Programming: Lecture 02: Introduction To Programming Languages
Structured Programming: Lecture 02: Introduction To Programming Languages
Khulna University.
Structured Programming
Introduction
Table of Contents 1 | 46
Computer Program
Khulna University.
Structured Programming
Elements of a computer system
Khulna University.
Structured Programming
Elements of a computer system
Khulna University.
Structured Programming
Elements of a computer system
Khulna University.
Structured Programming
Elements of a computer system
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.
Khulna University.
Structured Programming
Computer Program
Khulna University.
Structured Programming
Computer Program
Khulna University.
Structured Programming
The Evolution of Programming Languages
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
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
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
Khulna University.
Structured Programming
The CPU in More Detail
Model of a CPU 25 | 46
Khulna University.
Structured Programming
The CPU in More Detail
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
Khulna University.
Structured Programming
The CPU in More Detail
Fetch-Execute Cycle IV 30 | 46
Khulna University.
Structured Programming
Abstraction for Programming Languages
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.
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.