CSE-141: Structured Programming
Lecture 02: Basics of Structured Programming
Instructor
Md. Sabir Hossain
Lecturer
Dept. of CSE, CUET
Mobile: 01737143868/01882826575
Email: sabirndc08cuet10@gmail.com
Acknowledgment: Most of the content of this slide is adopted from wikipedia
Outlines
Introduction to Computer Programming
Programming Languages
Programming Paradigms
Lecture Objectives
Be able to explain the differences between programming
languages and programming paradigms.
Be able to differentiate between low-level and high-level
programming languages and their associated advantages and
disadvantages
Be able to list four programming paradigms and describe their
strengths and weaknesses.
What’s your aim?
• Do you want to be a software Developer?
• Yes/No
Software Development Life Cycle
https://online.husson.edu/software-development-cycle/
Software Development Method
• Requirement Specification
– Problem Definition
• Analysis
– Refine, Generalize, Decompose the problem definition
• Design
– Develop Algorithm
• Implementation
– Write Code (Programming)
• Verification and Testing
– Test and Debug the code
Computer Programming
• The functions of a computer system are
controlled by computer programs
• A computer program is a clear, step-by-step,
finite set of instructions
• A computer program must be clear so that only
one meaning can be derived from it
• A computer program is written in a computer
language called a programming language
A Question?
• Do you know the number of programming
languages exist?
• Imagine the number
• https://en.wikipedia.org/wiki/List_of_programmin
g_languages
Terms
• It is interesting and useful to investigate the meaning
of the word 'paradigm‘
Terms
• Programming paradigm (in this course)
– A pattern that serves as a school of thoughts for programming of
computers
• Programming technique
– Related to an algorithmic idea for solving a particular class of problems
– Examples: 'Divide and conquer' and 'program development by stepwise
refinement'
• Programming style
– The way we express ourselves in a computer program
– Related to elegance or lack of elegance
• Programming culture
– The totality of programming behavior, which often is tightly related to a
family of programming languages
– The sum of a main paradigm, programming styles, and certain
programming techniques.
Definitions
• Programming Paradigm
– programming “technique” (?)
– way of thinking about programming
– view of a program
• Programming Language
– consists of words, symbols, and rules for writing a
program
Programming Paradigms
• Main programming paradigms
– The imperative paradigm
– The functional paradigm
– The logical paradigm
– The object-oriented paradigm
• Other possible programming paradigms
– The visual paradigm
– One of the parallel paradigms
– The constraint based paradigm
Programming (Language) Paradigms
• FOUR PARADIGMS OF COMPUTING
• Imperative:
• WHAT DO WE DO NEXT ?
• Functional:
•
• Object-Oriented:
• WHAT ARE THE TERMS OF THE
CONTRACT ?
• Logic:
• WHEN IS IT TRUE THAT … ?
Programming Paradigms
• Imperative Programming
– WHAT DO WE DO NEXT ?
– program as a collection of statements and procedures affecting
data (variables)
• Object-Oriented Programming
– WHAT ARE THE TERMS OF THE CONTRACT ?
– program as a collection of classes for interacting objects
• Functional Programming
– WHAT IS THE FUNCTION’S VALUE ?
– program as a collection of (math) functions
• Logic Programming
– WHEN IS IT TRUE THAT … ?
Some Languages by Paradigm
• Imperative (also called Structured or
Procedural) Programming
– FORTRAN, BASIC, COBOL, Pascal, C
• Object-Oriented Programming
– SmallTalk, C++, Java
• Functional Programming
– LISP, ML, Haskell
• Logic Programming
– Prolog, CHIP (programming language), Ciao
(programming language), CLACL, CycL
Paradigm Change
• For example, from Structured to Object-Oriented
• Arises from problems encountered in one
paradigm but addressed in another
• Case study: from C to C++
– Evolution from structured, to modular, to object-
based, to object-oriented programming
Programming Paradigms
Why are there thousands of programming languages in use today?
Some programming languages are specifically designed for use in
certain applications.
Different programming languages follow different approaches to solving
programming problems
A programming paradigm is an approach to solving programming
problems.
A programming paradigm may consist of many programming
languages.
Common programming paradigms:
Imperative or Procedural Programming
Object-Oriented Programming
Functional Programming
Logic Programming
Programming Paradigms: Imperative
In this paradigm, a program is a series of statements containing variables.
Program execution involves changing the memory contents of the
computer continuously.
Example of imperative languages are: C, FORTRAN, Pascal, COBOL etc
Advantages
low memory utilization
relatively efficient
the most common form of programming in use today
Disadvantages
difficulty of reasoning about programs
difficulty of parallelization
Tend to be relatively low level
Structured Programming
• Structured programming is a programming
paradigm aimed at improving the clarity, quality,
and development time of a computer program by
making extensive use of
– subroutines,
– block structures,
– for and while loops
• in contrast to using simple tests and jumps such
as the goto statement, which could lead to
"spaghetti code" that is difficult to follow and
maintain
Elements of Structured Programming
• Control structures
– "Sequence"; ordered statements or subroutines executed in
sequence.
– "Selection"; one or a number of statements is executed
depending on the state of the program (if..then..else..endif).
– "Iteration"; a statement or block is executed until the program
reaches a certain state, or operations have been applied to
every element of a collection (for, while loop).
– "Recursion"; a statement is executed by repeatedly calling itself
until termination conditions are met.
Elements of Structured Programming
• Subroutines
– callable units such as procedures, functions,
methods, or subprograms are used to allow a
sequence to be referred to by a single statement
• Blocks
– are used to enable groups of statements to be treated
as if they were one statement.
•Block-structured languages have a syntax for enclosing
structures in some formal way, such as the curly braces {...} of
C and many later languages.
Programming Paradigms: Object-Oriented
A program in this paradigm consists of objects which communicate
with each other by sending messages
Example object oriented languages include: C++, Java, C#,
Smalltalk, etc
Advantages
Conceptual simplicity
Models computation better
Increased productivity
Disadvantages
Can have a steep learning curve, initially
Doing I/O can be cumbersome
Programming Paradigms: Functional
A program in this paradigm consists of functions and uses functions in a similar
way as used in mathematics
Program execution involves functions calling each other and
returning results. There are no variables in functional
languages.
Example functional languages include: ML, MirandaTM, Haskell
Advantages
Small and clean syntax
Better support for reasoning about programs
They allow functions to be treated as any other data values.
They support programming at a relatively higher level than the imperative languages
Disadvantages
Difficulty of doing input-output
Functional languages use more storage space than their imperative cousins
Programming Paradigms: Logic
A program in the logic paradigm consists of a set of predicates and rules of
inference.
Predicates are statements of fact like the statement that says: water is wet.
Rules of inference are statements like: If X is human then X is mortal.
The predicates and the rules of inference are used to prove statements that
the programmer supplies.
Example: Prolog
Advantages
Good support for reasoning about programs
Can lead to concise solutions to problems
Disadvantages
Slow execution
Limited view of the world
That means the system does not know about facts that are not its predicates
and rules of inference.
Difficulties in understanding and debugging large programs
Programming Languages
There are three categories of programming
languages:
Machine languages.
Assembly languages.
High-level languages.
Machine languages and assembly languages are also called
low-level languages
Programming languages
• Various programming languages
• Some understandable directly by computers
• Others require “translation” steps
– Machine language
• Natural language of a particular computer
• Consists of strings of numbers(1s, 0s)
• Instruct computer to perform elementary operations one at a
time
• Machine dependant
Programming languages
•Assembly Language
– English like abbreviations
– Translators programs called “Assemblers” to convert
assembly language programs to machine language.
– E.g. add overtime to base pay and store result in gross pay
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY
Programming languages
• High-level languages
– To speed up programming even further
– Single statements for accomplishing substantial tasks
– Translator programs called “Compilers” to convert
high-level programs into machine language
– E.g. add overtime to base pay and store result in
gross pay
grossPay = basePay + overtimePay
Programming Languages (cont’d)
A Machine language program consists of a
sequence of zeros and ones.
Each kind of CPU has its own machine language.
Advantages
Fast and efficient
Machine oriented
No translation required
Disadvantages
Not portable
Not programmer friendly
Assembly Language
Assembly language programs use mnemonics to represent
machine instructions
Each statement in assembly language corresponds to one
statement in machine language.
Assembly language programs have the same advantages and
disadvantages as machine language programs.
Compare the following machine language and assembly language
programs:
8086 Machine language program for 8086 Assembly program
var1 = var1 + var2 ; for
var1 = var1 + var2 ;
1010 0001 0000 0000 0000 0000 MOV AX , var1
0000 0011 0000 0110 0000 0000 0000 ADD AX , var2
0010 MOV var1 , AX
1010 0011 0000 0000 0000 0000
High-Level Programming Languages
A high-level language (HLL) has two primary components
(1) a set of built-in language primitives and grammatical rules
(2) a translator
A HLL language program consists of English-like
statements that are governed by a strict syntax.
Advantages
Portable or machine independent
Programmer-friendly
Disadvantages
Not as efficient as low-level languages
Need to be translated
Examples : C, C++, Java, FORTRAN, Visual Basic, and
Delphi.
Which Programming Paradigm is Best?
Which of these paradigms is the best?
The most accurate answer is that there is no best paradigm.
No single paradigm will fit all problems well.
Human beings use a combination of the models represented by these
paradigms.
Languages with features from different paradigms are often too complex.
So, the search of the ultimate programming language continues!
Review Questions
1. List two advantages and two disadvantages of low-level languages.
2. Explain the similarities and differences between an assembly language
and a machine language.
3. Mention the programming paradigm to which each of the following
languages belongs: Visual Basic, Java, C#, Haskell, Lisp, Prolog,
Pascal.
4. Which programming paradigms give better support for reasoning about
programs?
5. Which programming paradigms give better support for doing I/O?
References
• https://en.wikipedia.org/wiki/Structured_program
ming
• https://en.wikipedia.org/wiki/Programming_parad
igm
• http://people.cs.aau.dk/~normark/prog3-
03/html/notes/paradigms-slide-paradigms.html
Thank You
• Allah Hafez