Lect6_PLangs
Lect6_PLangs
• 6.1 History
• 6.2 Traditional Programming Concepts
• 6.3 Procedural Units
• 6.4 Language Implementation
Programming Languages
°
°
Evolution of Programming Paradigms
• Paradigm:
– informally: ‘method of expression’ / ‘philosophy’
Programming Paradigms
int main()
{
char names[10][9], pivot[9];
int i, j;
• Variables:
– descriptive name for location(s) in main memory
– declared in combination with data type
Declarative statements: data structures (1)
• Data Structures:
– arrangement of multiple data units (possibly of
different types)
– e.g.: homogeneous array, such as one-dimensional lists,
and two-dimensional tables (all elements of same type)
• Data Structures:
• e.g.: heterogeneous array, a block of data in which
different elements can have different types
(total: 14 bytes)
Declarative statements: constants and literals
• Constants:
– descriptive names for non-changeable values
– for better readability / understanding of code
• Example:
const WaterDensity20C = 998.2071
….
wMass = waterVol * WaterDensity
Q. A company’s number of employees rises from 5 to 6. Two
programs now must be altered to reflect this.
- Indicate what changes must be made.
- What problems arise in Program 1 that are avoided in Program 2?
Program 1: Program 2:
: :
DailySalary := TotalSal/5; NumEmpl constant Integer := 5;
AvgSalary := TotalSal/5; DaysWk constant Integer := 5;
DailySales := TotalSales/5; :
AvgSales := TotalSales/5; DailySalary := TotalSal/NumEmpl;
: AvgSalary := TotalSal/DaysWk;
DailySales := TotalSales/NumEmpl;
AvgSales := TotalSales/DaysWk;
:
Imperative statements: assignment
• Assignment:
– pseudo-code example: TotalCost 100
– requests that a value be stored in the memory area
identified by a variable
• General form:
– name expression
– where ‘expression’ can be complex, and needs
evaluation before the assignment takes place:
• Z = X + Y;
• searchSuccessful = doSearch(namesList, ‘David’);
• result = 2 * 4 + 6 / 2; (operator precedence must be defined!!!)
Operator precedence
• Control statements:
– change the
execution sequence
of a program
Imperative statements: control structures (2)
• Possible, but bad programming practice:
GOTO statement
goto 40
20 Apply procedure StayIndoors
Goto 70
40 If (PollutionLevel < Hazardous) then goto 60
Goto 20
60 Apply procedure GoOutdoors
70 . . .
• Parameter Passing:
– (1) pass by value
calling environment procedure’s environment
actual formal
5 does not change! 16
5
• By value: 75 is printed
– value of ‘X’ is given to Modify, but ‘X’ cannot be changed
• By reference: 77 is printed
– address of ‘X’ is given to Modify, so ‘X’ can be changed
Functions
• Function:
– procedure that produces a value (rather than
performing an action only)
– produced value is returned back to the calling
program unit
typedef struct {
int table[ MAX_SIZE ];
13 int top;
stack_state state;
32 } Stack;
return element;
}
Imperative vs. Object-Oriented (2)
• Translation / Compilation:
– converting high-level language code into machine language
3 stages of activities:
lexical analysis
parsing
code generation
– Lexical analyser:
• recognizes / classifies strings of symbols into single entities
(tokens)
• token packages each unit and its classification
• all program comments are ignored
Lexeme Token
int maximum(int x, int y) { int Keyword
if (x > y) return x;
maximum Identifier
else
return y; ( Operator
} int Keyword
x Identifier
– Parser (syntax analysis):
• groups lexical units(tokens) into statements (i.e. finds the
grammatical structure)
• based on ‘syntax diagrams’
– Linker:
• connects several object programs to produce executable program
(usually in form of: program.exe)
– Loader:
• places program code in main memory
• notifies OS scheduler
Chapter 6 - Programming Languages:
Conclusions
• High-level languages
– allow concentration on problem to be solved
– important property: machine independence
• Multiple programming paradigms
– most popular: imperative & object-oriented
• General program statements:
– declarative, imperative, comments
• Procedure calls:
– generic due to parameter passing (by value or reference)
• Imperative vs. Object-Oriented
– OO encourages putting together what belongs together
Software - Programming Languages