Transcript y + 1
14.1 Roles of a Programming
Language
Programming Language
A set of grammar rules that tells the
computer what to do
Flowchart or
pseudocode
Programming
language
Solution
algorithm
Problem
Computer
program
Sample program statements in C and Pascal
C program
int sum = 0;
int y = 1;
do
{
sum += y;
y += 1;
}
while (y <= 100);
printf (“The sum is
%d\n”, sum);
Pascal program
sum := 0;
y := 1;
REPEAT
sum := sum +y;
y := y + 1;
UNTIL y > 100;
writeln (‘The sum is ‘,
sum);
14.1 Roles of a Programming
Language
Programming Language
A set of grammar rules that tells the
computer what to do
Flowchart or
pseudocode
Programming
language
Solution
algorithm
Problem
Computer
program
Sample program statements in C and Pascal
C program
int sum = 0;
int y = 1;
do
{
sum += y;
y += 1;
}
while (y <= 100);
printf (“The sum is
%d\n”, sum);
Pascal program
sum := 0;
y := 1;
REPEAT
sum := sum +y;
y := y + 1;
UNTIL y > 100;
writeln (‘The sum is ‘,
sum);
14.2 Historical Development of
Language
Classification of Programming
Languages
The development of programming
languages is always classified by
‘generations’.
A programming language is classified as,
Low-level
when it is closer to the language that
computer uses
High-level
when the instructions are more English-like
14.2 Historical Development of
Language
Low-level Programming Languages
Include first-generation and secondgeneration languages
First-generation Languages (1GLs)
Machine languages
Instructions, memory locations and data
are all presented by strings of 0s and 1s
Readily understood by computers; less
intelligible by humans
Each instruction carries its own unique
numeric code called operation code
(opcode)
14.2 Historical Development of
Language
First-generation Languages (1 GLs)
Shortcomings
Difficult to program - numerical codes
are less intelligible by human
x 86 machine language
Remark
1011 1001 0011 0100 0001 0010
Set CX register to be
1234H
Lengthy
Machine-dependent – each type of CPU
has its own machine language
Not portable – no standard language
exists
Difficult to learn
14.2 Historical Development of
Language
Second-generation Languages
(2GLs)
Assembly languages
Easier to understand than machine
language
Stored as text
Each assembly instruction represents
exactly one machine instruction
An example:
x 86 assembly language
Remark
Mov CX, 1234H
Set CX register to be 1234H
14.2 Historical Development of
Language
Second-generation Languages
(2GLs)
Mneumonic
a word / code that helps remind us of an
operation
The general form of an instruction is,
Mneumonic operand(s)
14.2 Historical Development of
Language
Second-generation Languages
(2GLs)
Instruction
Example
mov dest,
src
Description
The data specified by src
is copied to dest.
add
add eax, 4
eax = eax + 4
sub
sub bx, 10
bx = bx – 10
inc
inc ecx
ecx = ecx + 1
dec
dec dl
dl = dl – 1
mov
Examples of assembly language instructions
14.2 Historical Development of
Language
Second-generation Languages
(2GLs)
An assembly language instruction cannot
be processed by computer directly
An assembler converts assembly language
programs into machine codes
Mov CX, 1234H
Assembler
1011 1001 0011
0100 0001 0010
14.2 Historical Development of
Language
Second-generation Languages
(2GLs)
Shortcomings
Lengthy – each instruction corresponds
to one instruction in machine language
Machine-dependent – each type of
computer has its own assembly
language
Not portable – no standard assembly
language
14.2 Historical Development of
Language
High-level Programming Languages
Instructions are English-like
A single instruction contains many
operations at the machine level
Easier to learn than low-level languages
Have to be converted to machine languages
before execution
This conversion is done by compilers or
interpreters (to be discussed later)
14.2 Historical Development of
Language
Fourth-generation Languages
(4GLs)
Declarative languages
Designed for specific problems
More user-oriented
Allow users to develop program with fewer
commands compared with procedural
language
Examples: Structured Query Language (SQL)
SELECT * FROM RECORDS WHERE NAME = “CHAN”
An SQL program statement
14.2 Historical Development of
Language
Fourth-generation Languages
(4GLs)
Declarative languages
Designed for specific problems
More user-oriented
Allow users to develop program with fewer
commands compared with procedural
language
Examples: Structured Query Language (SQL)
SELECT * FROM RECORDS WHERE NAME = “CHAN”
An SQL program statement
14.2 Historical Development of
Language
Third-generation Languages (3GLs)
Procedural languages
Used for general purpose
Examples: FORTRAN, COBOL, BASIC, Pascal,
Ada, C, C++, Java
BEGIN
writeln (PowerOf2);
PowerOf2 := PowerOf2 * 2
Count := Count + 1;
END;
writeln (‘There are’, Count : 4, ‘powers of 2
less than 100.’);
A sample of Pascal Statement
14.3 Assembler, Compiler and
Interpreter
Assembler
Converts assembly instructions into machine
codes
Assembler
Assembly
instruction
Machine code
14.3 Assembler, Compiler and
Interpreter
Compiler
Convert the source program into object
codes
Assembler
Source program
Object code
14.3 Assembler, Compiler and
Interpreter
Compiler
A programmer writes programming
statements in an editor
The editor usually has an Integrated
Development Environment (IDE) containing
a compiler
14.3 Assembler, Compiler and
Interpreter
Compiler
The file created by the editor is called the
source program
Compilation
batch process
The compiler first analyzes all statements
in the source program syntactically.
It then builds the output code, called the
object code.
14.3 Assembler, Compiler and
Interpreter
Interpreter
Translate and execute each source language
statement into a computer program before
translating and executing the next statement
Compiled programs generally run faster than
interpreted programs
Interpreter
Data
+
Source program
Results
14.3 Assembler, Compiler and
Interpreter
Interpreter
Advantages of using the interpreter
Can skip the compilation stage which is
time-consuming for a long program
Can immediately execute high-level
programs – useful for program
development
Efficient for small programming tasks
Interpreter-typed programming languages
are used in education as they allow
students to program interactively
14.3 Assembler, Compiler and
Interpreter
Interpreter
Advantages of using the interpreter
Can skip the compilation stage which is
time-consuming for a long program
Can immediately execute high-level
programs – useful for program
development
Efficient for small programming tasks
Interpreter-typed programming languages
are used in education as they allow
students to program interactively
14.4 Summary of Programming
Languages
Programming
languages
Generation
1GLs
Machine language
High-level
programming
languages
Less technical
Portable
More user-friendly
Run slower
3GLs
Procedural language
e.g. FORTRAN, COBOL, BASIC,
Pascal, Ada, C, C++ and Java
2GLs
Assembly language
4GLs
Declarative language
e.g. Structured Query
Language (SQL)
easier to use
closer to natural languages
Low-level
programming
languages
More technical
Not portable
Less user-friendly
Run faster
Trends
14.4 Summary of Programming
Languages
Programming
languages
Generation
1GLs
Machine language
High-level
programming
languages
Less technical
Portable
More user-friendly
Run slower
3GLs
Procedural language
e.g. FORTRAN, COBOL, BASIC,
Pascal, Ada, C, C++ and Java
2GLs
Assembly language
4GLs
Declarative language
e.g. Structured Query
Language (SQL)
easier to use
closer to natural languages
Low-level
programming
languages
More technical
Not portable
Less user-friendly
Run faster
Trends