Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer
Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer
description
Transcript of Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer
![Page 1: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/1.jpg)
Chair of Software Engineering
Einführung in die ProgrammierungIntroduction to Programming
Prof. Dr. Bertrand Meyer
Lecture 8: Control Structures I
![Page 2: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/2.jpg)
2
In this (double) lecture
The notion of algorithm Basic control structures: sequence, conditional,
loop Decision structures: variants of conditional
instruction Repeating operations: the loop Loops as approximation strategy: the loop invariant What does it take to ensure that a loop
terminates? A look at the general problem of loop termination Lower-level control structures: “Goto” and
flowcharts; see rationale for the “control structures of Structured Programming”
Undecidability of the Halting Problem
![Page 3: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/3.jpg)
3
The notion of algorithm
General definition:
An algorithm is the specification of a process to be carried out by a computer
![Page 4: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/4.jpg)
4
Not quite an algorithm
![Page 5: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/5.jpg)
5
5 properties of an algorithm
1. Defines data to which process will be applied
2. Every elementary step taken from a set of well-specified actions
3. Describes ordering(s) of execution of these steps
4. Properties 2 and 3 based on precisely defined conventions, suitable for an automatic device
5. For any data, guaranteed to terminate after finite number of steps
![Page 6: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/6.jpg)
6
Algorithm vs program
“Algorithm” usually considered a more abstract notion, independent of platform, programming language etc.
In practice, the distinction tends to fade: Algorithms need a precise notation Programming languages becoming more abstract
However: In programs, data (objects) are just as important
as algorithms A program typically contains many algorithms
and object structures
![Page 7: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/7.jpg)
7
What makes up an algorithm
Basic steps:
Feature call x.f (a ) Assignment ...
Sequencing of these basic steps:
CONTROL STRUCTURES
(Actually, not much else!)
![Page 8: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/8.jpg)
8
“Control structures of Structured Programming”
Control structures
Definition: program construct that describes the scheduling of basic actions
Three fundamental control structures: Sequence Loop Conditional
They are the
![Page 9: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/9.jpg)
9
Control structures as problem-solving techniques
Sequence: “To achieve C from A, first achieve an intermediate goal B from A, then achieve C from B”
Loop: solve the problem on successive approximations of its input set
Conditional: solve the problem separately on two or more subsets of its input set
![Page 10: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/10.jpg)
10
The sequence (or Compound)
instruction 1
instruction 2
...
instruction n
![Page 11: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/11.jpg)
11
Semicolon as optional separator
instruction1 ;
instruction2 ;
... ;
instructionn
![Page 12: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/12.jpg)
12
Not quite an algorithm
![Page 13: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/13.jpg)
13
Correctness of a Compound
Precondition of instruction 1 must hold initially
Postcondition of each instruction i must imply precondition of each instruction i + 1
Final effect is postcondition of instruction n
instruction 1
instruction 2
...
instruction i
...
instruction n
{P1
}{Q1
}{P2
}{Q2
}
{Pi
}{Qi
}
{Pn
}{Qn
}
implie
s
![Page 14: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/14.jpg)
14
Conditional instruction
ifCondition
thenInstructions
elseOther_instructions
end
-- Boolean_expression
-- Compound
-- Compound
![Page 15: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/15.jpg)
15
Computing the greater of two numbers
if
a > b
then
max := a
else
max := b
end
![Page 16: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/16.jpg)
16
As a query
maximum (a, b : INTEGER): INTEGER-- The higher of a and b.
do
end
ifa > b
thenResult := a
elseResult := b
end
![Page 17: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/17.jpg)
17
The conditional as problem-solving technique
Region 1
Region 2
PROBLEM SPACE
Use technique 1
Use technique 2
![Page 18: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/18.jpg)
18
Basic form
if Condition thenInstructions
elseOther_instructions
end
![Page 19: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/19.jpg)
19
A variant of the conditional
if Condition thenInstructions
end
![Page 20: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/20.jpg)
20
Means the same asif Condition then
Instructionselse
end
A variant of the conditional
if Condition thenInstructions
end
Empty clause
![Page 21: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/21.jpg)
21
Nesting
if Condition1 thenInstructions 1
else
end
if Condition2 then
Instructions 2
else
end
if Condition3 then
Instructions 3
else
end
...if Condition3 then
Instructions 4else
end
![Page 22: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/22.jpg)
22
Nested structure
![Page 23: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/23.jpg)
23
Comb-like structure
![Page 24: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/24.jpg)
24
Comb-like conditional
if Condition1 thenInstructions 1
elseif Condition 2 thenInstructions 2
elseif Condition3 thenInstructions 3
elseif
...
else Instructions 0
end
![Page 25: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/25.jpg)
25
Comb-like structure
![Page 26: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/26.jpg)
26
More control structure topics
Loops and their invariants
See what it takes to ensure that a loop terminates
Look at the general problem of loop termination
Examine lower-level control structures: “Goto” and flowcharts; see rationale for the “control structures of Structured Programming”
Prove the undecidability of the Halting Problem
![Page 27: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/27.jpg)
27
Loop
fromInitialization -- Compound
variantVariant_expression -- Integer expression
invariantInvariant_expression -- Condition
untilExit_condition -- Boolean_expression
loopBody -- Compound
end
![Page 28: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/28.jpg)
28
Loop, full form
fromInitialization -- Compound
invariantInvariant_expression -- Boolean_expression
variantVariant_expression -- Integer_expression
untilExit_condition -- Boolean_expression
loopBody -- Compound
end
![Page 29: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/29.jpg)
29
Another loop syntax
![Page 30: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/30.jpg)
30
Loop, full form
fromInitialization -- Compound
invariantInvariant_expression -- Boolean_expression
variantVariant_expression -- Integer_expression
untilExit_condition -- Boolean_expression
loopBody -- Compound
end
![Page 31: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/31.jpg)
31
Looping over stations of a line
fromfancy.start
untilfancy.after
loop-- “Do something with fancy.item”
fancy.forth
end
Previously: fancy_lineIn textbook: Line8
![Page 32: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/32.jpg)
32
Operations on a list
item
before after
count
forthback
index
start
Commands
Queries
(boolean)
1
(The cursor)
![Page 33: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/33.jpg)
33
Operations on a list
item
before after
count
forth
index
start
(The cursor)
1
![Page 34: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/34.jpg)
34
Looping over stations of a line
fromfancy.start
untilfancy.after
loop-- “Do something with fancy.item”
fancy.forth
end
![Page 35: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/35.jpg)
35
fromfancy.start
untilfancy.after
loop-- Display name of next station:
Console.show (fancy.item)
fancy.forth
end
Displaying station names
![Page 36: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/36.jpg)
36
Computing the “maximum” of station names
from
fancy.start ; Result := ""until
fancy.afterloop
Result := greater (Result, fancy.item.name)
fancy.forthend
![Page 37: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/37.jpg)
37
Computing the “maximum” of station names
from
fancy.start ; Result := " "until
fancy.afterloop
Result := greater (Result, fancy.item.name)
fancy.forthend
The greater of two strings, alphabetically, e.g.
greater ("ABC ", "AD ") = "AD"
![Page 38: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/38.jpg)
38
highest_name: STRING -- Alphabetically greatest station name of
linedo
from
fancy.start ; Result := ""
untilfancy.after
loop Result := greater (Result,
fancy.item.name)
fancy.forthend
end
In a function
![Page 39: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/39.jpg)
39
Forms of loop
fromInstructions
untilCondition
loopInstructions
end
while Condition do
Instructionsend
repeatInstructions
untilCondition
endfor i : a..b do
Instructionsend
for (Instruction; Condition; Instruction) do
Instructionsend
![Page 40: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/40.jpg)
40
Loop as approximation strategy
name
1
name
2
name
i
name n
Result = name 1Result = Max (names 1 2)
Result = Max (names 1 i )
Result = Max (names 1 n )
= Max (names 1 1)i := i + 1 R e s u lt := g r e a te r
( R e s u lt , fa n c y .i t e m .n a m e )
i := i + 1 Result := greater
(Result , fancy.item.name)
Loop body:
Loop body:..
..
..
..
Slice
![Page 41: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/41.jpg)
41
highest_name : STRING-- Alphabetically greatest station name of line
dofrom
fancy.start ; Result := ""until
fancy.afterloop
Result := greater (Result, fancy.item.name)
fancy.forthend
ensure Result /= Void and then not Result.empty
-- Result is the alphabetically greatest of station names
end
Postcondition?
![Page 42: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/42.jpg)
42
Computing the “maximum” of station names
from
fancy.start ; Result := ""
untilfancy.after
loop Result := greater (Result, fancy.item.name)
fancy.forthensure
-- Result is the alphabetically greatest of station names
end
![Page 43: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/43.jpg)
43
from
fancy.start ; Result := ""invariant
fancy.index >= 1fancy.index <= fancy.count -- Result is the alphabetically greatest of the-- names of previous stations
untilfancy.after
loop Result := greater (Result, fancy.item.name)
fancy.forthensure
-- Result is the alphabetically greatest of station names
end
The loop invariant
+ 1
Something missing?
![Page 44: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/44.jpg)
44
Loop as approximation strategy
name
1
name
2
name
i
name n
Result = name 1Result = Max (names 1 2)
Result = Max (names 1 i )
Result = Max (names 1 n )
= Max (names 1 1)i := i + 1 R e s u lt := g r e a te r
( R e s u lt , fa n c y .i t e m .n a m e )
i := i + 1 Result := greater
(Result , fancy.item.name)
The loop invariant
Loop body:
Loop body:..
..
..
..
![Page 45: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/45.jpg)
45
Loop invariant
(Do not confuse with class invariant)
Property that is:
Satisfied after initialization (from clause)
Preserved by every loop iteration (loop clause) executed with the exit condition (until clause)not satisfied
![Page 46: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/46.jpg)
46
from
fancy.start ; Result := ""invariant
fancy.index >= 1fancy.index <= fancy.count + 1-- Result is the alphabetically greatest of the-- names of previous stations
untilfancy.after
loop Result := greater (Result, fancy.item.name)
fancy.forthensure
-- Result is the alphabetically greatest of station namesend
The loop invariant
Result = Max (names 1 i )
..
![Page 47: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/47.jpg)
47
from
fancy.start ; Result := ""invariant
index >= 1index <= count + 1-- If there are any previous stations,-- Result is the alphabetically greatest of their names
untilfancy.after
loop Result := greater (Result, fancy.item.name)
fancy.forthensure
-- Result is the alphabetically greatest of station names if anyend
The loop invariant (better)
Result = Max (names 1 i )
..
![Page 48: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/48.jpg)
48
fromfancy.start ; Result := ""
invariantindex >= 1index <= count + 1-- Result is greatest of previous station names
untilfancy.after
loop Result := greater (Result, fancy.item.name)
fancy.forthend
The effect of the loop
At end: invariant and exit condition•
All stations visited (fancy.after )
• Result is greatest of their names
Exit condition satisfied at end
Invariant satisfied after each iteration
Invariant satisfied after initialization
![Page 49: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/49.jpg)
49
Quiz: what’s the invariant?
xxxx (a, b: INTEGER): INTEGER-- ?????????????????????????????????
requirea > 0 ; b > 0
localm, n : INTEGER
do from
m := a ; n := binvariant
-- “????????”variant
????????until
m = nloop
if m > n thenm := m − n
elsen := n − m
endendResult := m
end
![Page 50: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/50.jpg)
50
Quiz: what’s the invariant?
euclid (a, b: INTEGER): INTEGER-- Greatest common divisor of a and b
requirea > 0 ; b > 0
localm, n : INTEGER
do from
m := a ; n := binvariant
-- “????????”variant
????????until
m = nloop
if m > n thenm := m − n
elsen := n − m
endendResult := m
end
![Page 51: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/51.jpg)
51Intro. to Programming, lecture 11 (complement): Levenshtein
Another example
MI C H A E L J A C K S O N
E N D S HOperation
S D S S S D D D D I
“Michael Jackson” to “Mendelssohn”
Distance 1 2 3 4 5 6 7 8 9 100
I H A
![Page 52: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/52.jpg)
52
Levenshtein distance
B
Operation
“Beethoven” to “Beatles”
Distance
E E
A
E T N
S
NH V EO
L
OB E T H V E
0 0 1
R
1 2
D
3
R
4
D
5
R
4
![Page 53: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/53.jpg)
53
Levenshtein distance
Also called “Edit distance”
Purpose: to compute the smallest set of basic operations
Insertion Deletion Replacement
that will turn one string into another
Intro. to Programming, lecture 11 (complement): Levenshtein
![Page 54: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/54.jpg)
54
B E A T L E S
B
E
E
T
H
30 1 2 5 6 74
0
1
2
3
5
4
30 1 2 5 6 74
1
2
3
5
4
0 2 3 4 5 6
1
1
1
0 1 2 3 4 5
2 1 2 3 3 4
3 2 2 1 2 3 4
4 3 3 2 2 3 44
![Page 55: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/55.jpg)
55
Levenshtein distance algorithm
distance (source, target: STRING): INTEGER-- Minimum number of operations to turn source into
targetlocal
dist : ARRAY_2 [INTEGER]i, j, del, ins, subst : INTEGER
docreate dist.make (source.count, target.count)from i := 0 until i > source.count loop
dist [i, 0] := i ; i := i + 1end
from j := 0 until j > target.count loopdist [0, j ] := j ; j := j + 1
end-- (Continued)
![Page 56: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/56.jpg)
56
Levenshtein distance algorithmfrom i := 1 until i > source.count loop
from j := 1 until j > target.count invariant
loop if source [i ] = target [ j ] then dist [i, j ] := dist [ i -1, j -1]
else deletion := dist [i -1, j ]
insertion := dist [i , j - 1]substitution := dist [i - 1, j - 1]
dist [i, j ] := minimum (deletion, insertion, substitution) + 1
endj := j + 1
end i := i + 1 endResult := dist (source.count, target.count)
end
???
![Page 57: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/57.jpg)
57
How do we know a loop terminates?
fromfancy.start ; Result := ""
invariantindex >= 1index <= count + 1-- If there are any previous stations,-- Result is the alphabetically greatest of their names
untilfancy.after
loop Result := greater (Result, fancy.item.name)
fancy.forthend
![Page 58: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/58.jpg)
58
fromfancy.start ; Result := ""
invariantindex >= 1index <= count + 1-- If there are any previous stations,-- Result is the alphabetically greatest of their names
untilfancy.after
loop Result := greater (Result, fancy.item.name)
fancy.forthend
How do we know a loop terminates?
![Page 59: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/59.jpg)
59
Loop variant
Integer expression that must:
Be non-negative when after initialization (from)
Decrease (i.e. by at least one), while remaining non-negative, for every iteration of the body (loop) executed with exit condition not satisfied
![Page 60: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/60.jpg)
60
fancy.count − fancy.index + 1
fromfancy.start ; Result := ""
invariantindex >= 1index <= count + 1-- If there are any previous stations,-- Result is the alphabetically greatest of their names
variant
untilfancy.after
loop Result := greater (Result, fancy.item.name)
fancy.forthend
The variant for our example
![Page 61: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/61.jpg)
61
The general termination problem
Can EiffelStudio find out if your program will terminate?
No, it can’t
No other program, for any other realistic programming
language, can!
![Page 62: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/62.jpg)
62
The halting problem and undecidability
(“Entscheidungsproblem”, Alan Turing, 1936.)
It is not possible to devise an effective procedure that will find out if an arbitrary program will terminate on arbitrary input
(or, for that matter, if an arbitrary program with no input will terminate)
![Page 63: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/63.jpg)
63
The halting problem in Eiffel
Assume we have a feature
terminates (my_program: STRING ): BOOLEAN-- Does my_program terminate?
do... Your algorithm here ...
end
![Page 64: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/64.jpg)
64
The halting problem in practice
Some programs do not terminate in certain cases...
That’s a bug!
Yours had better terminate in all cases
Use variants
![Page 65: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/65.jpg)
65
Control structures at the machine level
Unconditional branch:
BR label
Conditional branch, for example:
BEQ loc_a loc_b l abel
![Page 66: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/66.jpg)
66
The equivalent of if-then-else
BEQ loc_a loc_b 111
101 ... Code for Compound_2 ...
BR 125
111 ... Code for Compound_1 ...
125 ... Code for continuation of program ...
if a = b then Compound_1 else Compound_2 end
![Page 67: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/67.jpg)
67
Flowcharts
a = b
Compound_2Compound_1
True False
![Page 68: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/68.jpg)
68
In programming languages: the Goto
test condition goto else_part
Compound_1
goto continue
else_part : Compound_2
continue : ... Continuation of program ...
a = b
Compound_2Compound_1
True False
![Page 69: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/69.jpg)
69
“Goto considered harmful”
Dijkstra, 1968Arbitrary Goto instructions lead to messy, hard to maintain programs (“spaghetti code”)
Böhm and Jacopini theorem: any program that can be expressed with gotoinstructions and conditionals can alsobe expressed without gotos, usingsequences and loops
For an example of transformation seeTouch of Class
![Page 70: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/70.jpg)
70
The Goto today
Almost universally decriedStill exists in some programming languagesAlso hides under various disguises, e.g. break
loop...if c then break end...
end
![Page 71: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/71.jpg)
71
One-entry, one-exit
(Compound) (Loop) (Conditional)
![Page 72: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/72.jpg)
72
Quiz: what’s the invariant?
xxx (a, b: INTEGER): INTEGER is-- ?????????????????????????????????
requirea > 0 ; b > 0
localm, n : INTEGER
do from
m := a ; n := binvariant
-- “????????”variant
????????until
m = nloop
if m > n thenm := m − n
elsen := n − m
endendResult := m
end
![Page 73: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/73.jpg)
73
Quiz: what’s the invariant?
euclid (a, b: INTEGER): INTEGER is-- Greatest common divisor of a and b
requirea > 0 ; b > 0
localm, n : INTEGER
do from
m := a ; n := binvariant
-- “????????”variant
????????until
m = nloop
if m > n thenm := m − n
elsen := n − m
endendResult := m
end
![Page 74: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/74.jpg)
74
Levenshtein, continued
from i := 1 until i > source.count loop from j := 1 until j > target.count invariant
loop if source [i ] = target [ j ] then new := dist [ i -1, j -1]
else deletion := dist [i -1, j ] insertion := dist [i , j - 1] substitution := dist [i - 1, j - 1] new := deletion.min (insertion.min (substitution)) + 1
end dist [i, j ] := new
j := j + 1 end i := i + 1 end
Result := dist (source.count, target.count)
-- For all p : 1 .. i, q : 1 .. j –1, we can turn source [1 .. p ]-- into target [1 .. q ] in dist [p, q ] operations
![Page 75: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/75.jpg)
75
Levenshtein distance algorithm
distance (source, target: STRING): INTEGER-- Minimum number of operations to turn source
into targetlocal
dist: ARRAY_2 [INTEGER]i, j, new, deletion, insertion, substitution :
INTEGERdo
create dist.make (source.count, target.count)from i := 0 until i > source.count loop
dist [i, 0] := i ; i := i + 1end
from j := 0 until j > target.count loopdist [0, j ] := j ; j := j + 1
end-- (Continued)
![Page 76: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/76.jpg)
76
Levenshtein , continued
from i := 1 until i > source.count loop from j := 1 until j > target.count invariant
loop if source [i ] = target [ j ] then new := dist [ i -1, j -1]
else deletion := dist [i -1, j ] insertion := dist [i , j - 1] substitution := dist [i - 1, j - 1] new := deletion.min (insertion.min (substitution)) + 1
end dist [i, j ] := new
j := j + 1 end i := i + 1 end
Result := dist (source.count, target.count)
-- For all p : 1 .. i, q : 1 .. j –1, we can turn source [1 .. p ]-- into target [1 .. q ] in dist [p, q ] operations
![Page 77: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/77.jpg)
77
Paradoxes in logic
The Russell paradox Some sets are members of themselves, e.g. the
set of all infinite sets is infinite Others are not members of all sets, e.g. the set
of all finite sets is not finite Consider the set of all sets that are not
members of themselves
The barber paradox (Russell) In Zurich there is a hairdresser that does the
hair of all the inhabitants who do not do their own hair
Who does her hair?
![Page 78: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/78.jpg)
78
The Grelling paradox
An adjective in English is: “Autological” is if applies to itself (e.g.
“polysyllabic”) “Heterological” otherwise
What is the status of “Heterological”?
Another form:
The first proposition appearing in red on this pageis false
![Page 79: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/79.jpg)
79
The liar’s paradox
(Very old!)
Epaminondas says all Cretans are liars Epaminondas is a Cretan
![Page 80: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/80.jpg)
80
The halting problem and undecidability
(“Entscheidungsproblem”, Alan Turing, 1936.)
It is not possible to devise an effective procedure that will find out if an arbitrary program will terminate on arbitrary input
(or, for that matter, if an arbitrary program with no input will terminate)
![Page 81: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/81.jpg)
81
The halting problem in Eiffel
Assume we have a feature
terminates (file_name : STRING): BOOLEAN-- Does program in file file_name
terminate?do
... Your algorithm here ...end
![Page 82: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/82.jpg)
82
Then…
Root procedure of the system:
what_do_you_think -- Terminate only if not.
dofromuntil
not terminates (“/usr/home/turing.e”)loopend
end
Store this program text in /usr/home/turing.e
![Page 83: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/83.jpg)
83
The halting problem in practice
Some programs do not terminate in certain cases...
That’s a bug!
Yours had better terminate in all cases
Use variants
![Page 84: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/84.jpg)
84
What we have seen
The notion of algorithmBasic propertiesDifference with “program”
The notion of control structureCorrectness of an instruction
Control structure: sequenceControl structure: conditionalNesting, and how to avoid it
![Page 85: Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer](https://reader035.fdocuments.in/reader035/viewer/2022062808/568153da550346895dc1d185/html5/thumbnails/85.jpg)
85
What we have seen
The notion of algorithm Basic properties Difference with “program”
The notion of control structureCorrectness of an instruction
Control structure: sequenceControl structure: conditionalNesting, and how to avoid it