SoftwareChapter 4
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
364
Outline• Evolution of programming languages
� Definition
� Machine languages / Assembly languages / High-level languages
• Translation
� Compilation
� Interpretation
� Translation process
• Developing a program
� The Edit-Compile-Link-Execute Process
� Flowchart
• Programming Paradigms
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
365
Outline• Evolution of programming languages
� Definition
� Machine languages / Assembly languages / High-level languages
• Translation
� Compilation
� Interpretation
� Translation process
• Developing a program
� The Edit-Compile-Link-Execute Process
� Flowchart
• Programming Paradigms
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
366
References
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
367
• Foundations of Computer ScienceBehrouz A. Forouzan, Firouz Mosharraf
Evolution• To write a program, we must use a computer language.
• Computer language = set of predefined words that are combined into a program according to predefined rules (syntax).
• Languages has evolved over the years.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
368
ExampleSum of 2 integers
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
369
1. Input the first number into memory
2. Input the second number into memory
3.Add the two together and store the result in memory
4.Output the result
Algorithm written in English
ExampleSum of 2 integers / Machine languages
hexadecimal Code in machine language
(1FEF)16 0001 1111 1110 1111
(240F)16 0010 0100 0000 1111
(1FEF)16 0001 1111 1110 1111
(241F)16 0010 0100 0001 1111
(1040)16 0001 0000 0100 0000
(1141)16 0001 0001 0100 0001
(3201)16 0011 0010 0000 0001
(2422)16 0010 0100 0010 0010
(1F42)16 0001 1111 0100 0010
(2FFF)16 0010 1111 1111 1111
(0000)16 0000 0000 0000 0000
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
370Code in machine language
READ
READ
STORE M40
STORE M41
LOAD R0 M40
LOAD R1 M41
ADD R2 R0 R1
STORE M42
WRITE M42
SCREEN
HALT
ExampleSum of 2 integers / Machine languages• In the earliest days of computers, the only programming languages available were machine languages.
• Each computer has its own machine language (made of streams of 0s and 1s)
• Machine language is the only language understood by the computer hardware.
• 2 drawbacks:
� Machine dependent
� Tedious to write programs and find errors in this language
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
371
ExampleSum of 2 integers / Assembly languagesCode in assembly language Description
LOAD RF Keyboard Load from keyboard controller to register F
STORE Number1 RF Store register F into Number1
LOAD RF Keyboard Load from keyboard controller to register F
STORE Number2 RF Store register F into Number2
LOAD R0 Number1 Load Number1 into register 0
LOAD R1 Number2 Load Number2 into register 1
ADDI R2 R0 R1 Add registers 0 and 1 with result in register 2
STORE Result R2 Store register 2 into Result
LOAD RF Result Load Result into register F
STORE Monitor RF Store register F into monitor controller
HALT Stop
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
372Code in assembly language
ExampleSum of 2 integers / Assembly languages• Replace binary code for instructions and addresses with symbols.
• A special program called assembler is used to translate code in assembly language into machine language.
• Requires programmer to concentrate on the hardware they were using.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
373
ExampleSum of 2 integers / High-level languages
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
374Code in C language
#include <stdio.h>int main( ){
int num1;int num2;int sum;
printf("Enter two integers:\n");scanf("%d", &num1); scanf("%d", &num2);
sum=num1+num2;printf("Sum= ");printf("%d",sum);return 0;
}
Variable declaration.Reservation of words in memory
Instruction
Symbols
Token
ExampleSum of 2 integers / High-level languages• High-level languages are portable to many different computers.
• High-level languages must be converted to machine language: process called interpretation or compilation.
• Various languages, most notably BASIC, COBOL, Pascal, Ada, C, C++, Java, …. were developed.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
375
Translation• A program written in high-level language needs to be translated into the machine language.
• Source program= program in a high-level language.
• Object program= translated program in machine language.
• Two methods are used for translation:
� Compilation
� Interpretation
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
376
Compilation• A compiler translates the whole source program into the object program.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
377
Interpretation• Interpretation refers to the process of translating each line of the source program into the corresponding line of the object program and executing the line.
• 2 trends:
1. (prior to Java): Each line of the source program in translated into the machine language and executed immediately. If there are any errors, the process displays an error message and the rest of the process is aborted.Since it’s a slow process, most languages use compilation instead of interpretation.
2. Java language is designed to be portable. Translation is done in 2 steps: compilation + interpretation. A Java source program is first compiled to create Java bytecode (object program for a virtual machine called Java Virtual Machine JVM). The byte code then can be compiled or interpreted by any computer that runs a JVM emulator.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
378
Translation process• Both compilation and interpretation follow the same translation process:
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
379
Source file
Lexical
analyzer
Syntax
analyzer
Semantic
analyzerCode
generator Object file
Symbols Tokens Instructions CodeCodable
instructions
Translation process• Lexical analyzer: reads the source code, symbol by symbol, and creates a list of tokens. Example, the two symbols i and f are read and grouped together as the token if in the C, C++ or Java languages.
• Syntax analyzer: parses a set of tokens to find instructions. Example, the tokens x, = and 0 are used to create the assignment statement in the C language x=0.
• Semantic analyzer: checks the sentences created by the syntax analyzer to be sure that they contain no ambiguity.
• Code generator: each instruction is converted to a set of machine language instructions.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
380
Developing a programThe Edit-Compile-Link-Execute Process• Developing a program in a compiled language such as C requires at least four steps:
� editing (or writing) the program
� compiling it
� linking it
� executing it
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
381
Developing a programThe Edit-Compile-Link-Execute Process• Editing: Develop the algorithm and then write the source file with the extension .c for C programming language.
• Compiling: The source file is translated into binary numbers understandable to the computer's Central Processing Unit.This process produces an intermediate object file - with the extension .obj, the .obj stands for Object.
• Linking: Many compiled languages come with library routines which can be added to your program. Theses routines are written by the manufacturer of the compiler to perform a variety of tasks, from input/output to complicated mathematical functions. In the case of C the standard input and output functions are contained in a library (stdio.h). After linking the file extension is .exe which are executable files.
• Executable files: run the .exe files in memory.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
382
Developing a programDevelop the algorithm• Algorithm: A detailed description of the exact methods used for solving a particular problem.
• To develop the algorithm, the programmer needs to ask:
� What data has to be fed into the computer?
� What information do I want to get out of the computer?
� Logic: Planning the processing of the program. It contains the instructions that cause the input data to be turned into the desired output data.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
383
Developing a programDevelop the algorithm• A step-by-step program plan is created during the planning stage.
• The three major notations for planning detailed algorithms:
� Flowchart: Series of visual symbols representing the logical flow of a program.
� Nassi-Schneidermann charts: Uses specific shapes and symbols to represent different types of program statements.
� Pseudocode: A verbal shorthand method that closely resembles a programming language, but does not have to follow a rigid syntax structure.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
384
Developing a programDevelop the algorithm
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
385
IF account_balance < $ 300
Yes No
Service_charge = $ 5 Service_charge = $ 2
Nassi-Schneidermann chart:
If account_balance < $ 300
then Service_charge = $ 5
else Service_charge = $ 2
End
Pseudocode:
Start
Account
_balance
< 300
Service_charge = 2
End
Yes No
Flowchart:
Service_charge = 5
Developing a programDevelop the algorithm• 2 types of statements
1. Input/output statement (read, write)
2. Assignment statementExamples:
x10yx+5
• In the algorithm (or program), use the following statements:
1. Sequencing: sequence of ordered statements
2. Selection: uses a condition with one or multiple branching
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
386
Flowchart• Some of the symbols which are used in flowchart are:
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
387
Start & End
Decision Box
Read
Sequence of instructions
Write
ExampleSum of 2 integers / Flowchart
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
388
start
nb1
nb2
sumnb1+nb2
“the sum is equal to”, sum
end
Begin
Read nb1
Read nb2
Sum=nb1+nb2
Output “the sum is equal to” sum
End
ExerciseFlowchart• Draw a flowchart that reads the length and width of a rectangle and calculates its perimeter and area.
• Perimeter = 2*(length+width)Area = length * width
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
389
ExerciseFlowchart• Draw a flowchart that allows to read the price and the quantityof an article and then calculates and displays the amount of the bill knowing that the VAT rate is 20.6%.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
390
ExerciseFlowchart• Draw a flowchart that permutes the values of 2 variables.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
391
ExampleMaximum of 2 integers / FlowchartVersion 1
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
392
ExampleMaximum of 2 integers / FlowchartVersion 2
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
393
Programming Paradigms• Computer languages are categorized according to the approach they use to solve a problem.
• Paradigm = is a way in which a computer language looks at the problem to be solved.
• Computer languages are divided into 4 paradigms:
1. Procedural (imperative)
2. Object-oriented
3. Functional
4. Declarative
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
394
The evolution of programming paradigms
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
395
The procedural (imperative) paradigm• Program = active agent that manipulates passive objects (e.g. stone, lamp, …), referred as data or data items.
• Passive object cannot initiate an action by itself, but it can receive actions from active agents.
• To manipulate a piece of data, the active agent (program) issues an action, referred to as a procedure.
• Example: a program that prints the content of a file.The file is a passive object. To print the file, the program uses a procedure, which we call print.
• To avoid writing a new procedure each time we need to print a file, we can create a general procedure that can print any file. However we need to replace every reference to the file name by a symbol.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
396
The procedural (imperative) paradigm
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
397
The procedural (imperative) paradigm• Several high-level imperative (procedural) languages have been developed over the last few decades, such as FORTRAN, COBOL, Pascal, C, and Ada.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
398
The procedural (imperative) paradigmFORTRANFORTRAN (FORmula TRANslation):
• available since 1957
• First high-level language
• During the last 40 years, FORTAN has gone through several versions: FORTRAN, FORTRAN II, FORTRAN IV, FORTRAN 77, FORTRAN 99, and HPF (High Performance FORTRAN).
• HPF is used in high-speed multiprocessor computer systems.
• Still an ideal language for scientific and engineering applications:
� High-precision arithmetic
� Capability of handling complex numbers
� Exponentiation computation (ab)
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
399
The procedural (imperative) paradigmCOBOLCOBOL (Common Business-Oriented Language)
• Used as a business programming language
• Programming needs of the business world:
� Fast access to files
� Fast updating of files
� Large amounts of generated reports
� User-friendly formatted output
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
400
The procedural (imperative) paradigmPascalPascal:
• Designed to teach programming to novices
• Most popular language in academia
• Never attained the same popularity in industry
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
401
The procedural (imperative) paradigmCC:
• Developed in the early 1970s by Dennis Ritchie at Bell Laboratories.
• Originally intended for writing operating systems (most of the UNIX OS is written in C).
• Became popular among programmers for several reasons:
� C has all the high-level instructions: it hides the hardware details from the programmer
� C has some low-level instructions that allow the programmer to access the hardware directly and quickly: it is a good language for system programmers
� C is a very efficient language: its instructions are shorts.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
402
The object-oriented paradigm• It deals with active objects (e.g. vehicle, automatic door, dishwasher, …).
• The action to be performed on these objects are included in the objects: objects need to receive the appropriate stimulus from outside to perform one of the actions.
• Example:A file in an object-oriented paradigm can be packed with all the procedures (here called methods) to be performed by the file.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
403
The object-oriented paradigm• Methods are shared by all objects of the same type, and also for other objects that are inherited from these objects.
• Comparing the procedural paradigm with the object-oriented paradigm, we see that the procedures in the procedural paradigm are independent entities, but the methods in the object-oriented paradigm belong to the object’s territory.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
404
The object-oriented paradigmClasses• Objects of the same type (e.g. files) need a set of methods that show how an object of this type reacts to stimuli from outside the object’s territories.
• To create such methods, object-oriented languages use a unit called a class.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
405
The object-oriented paradigmInheritance• Inheritance: An object can inherit from another object.
• When a general class is defined, we can define a more specific class that inherits some of the characteristics of the general class, but also has some new characteristics.
• Example:When an object of the type GeometricalShapes is defined, we can define a class called Rectangles.Rectangles are geometrical shapes with additional characteristics.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
406
The object-oriented paradigmPolymorphism• Polymorphism means “many forms”.
• We can define several operations with the same name that can do different things in related classes.
• Example:Assume we define 2 classes, Rectangles and Circles, both inherited from the class GeometricalShapes.We define 2 operations both named area, one in Rectangles and one in Circles, that calculates the area of a rectangle or a circle.
The 2 operations have the same name but do different things, as calculating the area of a rectangle and the area of a circle need different operands and operations.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
407
The object-oriented paradigm• Several object-oriented languages have been developed, notably:
� C++
� Java
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
408
The object-oriented paradigmC++• Developed by Bjarne Stroustrup at Bell Laboratory as an improvement of the C language.
• Uses classes to define the general characteristics of similar objects and the operations that can be applied to them.
• 3 principles were used in the design of the C++ language: encapsulation, inheritance and polymorphism.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
409
The object-oriented paradigmJava• Based on C and C++, but some features of C++ (multiple inheritance) are
removed to make the language more robust.
• Language is totally class-oriented.
• A program in Java can be either an application or an applet (is embedded
HTML, stored on a server and run by a browser).
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
410
The functional paradigm• A program is considered as a mathematical function.
• A function is a black box that maps a list of inputs to a list of outputs.
• For example, summation can be considered as a function with n inputs and only one output. The function takes the n inputs, adds them, and creates the sum.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
411
The functional paradigm• A functional language does the following:
� Predefines a set of primitive (atomic) functions that can be used by any programmer.
� Allows the programmer to combine primitive functions to create new functions.
• Example:We define 2 primitive functions called first that extracts the first element of a list and rest that extracts all the elements except the first.
A program can define a function that extracts the third element of a list by combining these 2 functions
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
412
The functional paradigm• 2 advantages over a procedural language:
� It encourages modular programming
� It allows the programmer to make new functions out of existing ones
• We will cover LISP and Scheme as examples of functional languages.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
413
The functional paradigmLISPLISP:
• Designed at MIT in the early 1960s
• It is a list-processing programming language in which everything is considered a list.
• Suffers from a lack of standardization. There were different versions of LISP everywhere. The de facto standard is the one developed by MIT in the early 1970s called Scheme.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
414
The functional paradigmSchemeScheme:
• Defines a set of primitive functions that solves problems.
• The function name and the list of inputs to the function are enclosed in parentheses. The result is an output list, which can be used as the input list to another function.
• Example:There is a function car that extracts the first element of a list.There is a function cdr that extracts the rest of the elements in a list except
the first one.(car 2 3 7 8 11 17 20) 2(cdr 2 3 7 8 11 17 20) 3 7 8 11 17 20
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
415
The functional paradigmScheme(car 2 3 7 8 11 17 20) 2(cdr 2 3 7 8 11 17 20) 3 7 8 11 17 20
Combine these 2 functions to extract the third element of any list.(car (cdr (cdr list)))
(car (cdr (cdr 2 3 7 8 11 17 20))) (car (cdr 3 7 8 11 17 20 )) (car 7 8 11 17 20 ) 7
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
416
The declarative paradigm• Uses the principle of logical reasoning to answer queries.
• It is based on formal logic defined by Greek mathematicians.
• Logical reasoning is based on deduction.
� Some statements (facts) are given that are assumed to be true
� Logician uses solid rules of logical reasoning to deduce new statements (facts)
• Example: If(A is B) and (B is C), then (A is C)Fact 1: Socrates is a human A is B
Fact 2: A human is mortal B is CWe can deduce a new fact:Fact 3: Socrates is mortal A is C
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
417
The declarative paradigm• Programmers need to be expert in logic to carefully define the rules.
• The program can then deduce and create new facts.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
418
The declarative paradigmProlog• Prolog
• Developed in France in 1972.
• A program in Prolog is made up of facts and rules.
• Example:human(john).mortal(X):-human(X).
• The user can ask:
• ?- mortal(john).The program will respond with yes.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
19
-20
20
419
Fact: john is a human
Rule: X, if X is human X is mortal