Exercise
Set
5.5
Exercises
15
contain a while loop and a predicate.
In
each case
show that if the predicate
is
true before entry to the loop, then it
is
also true after exit from the loop.
1.
loop:
while
(m
2:
0 and
m
~
100)
m:=
m
+
I
n
:=
n

I
end while
predicate:
m
+
n
=
100
2. loop:
while
(m
2:
0 and
m
~
100)
m
:=m+4
n:=
n

2
end while
predicate:
m
+
n
is
odd
3. loop:
while
(m
2:
0 and
m
~
100)
m
:=3·m
n:=
5·n
end while
predicate:
m
3
>
n
2
4. loop:
while
(n
2:
0 and
n
~
'100)
n
:=
n
+
I
end while
predicate:
2"
<
(n
+
2)!
5.
loop:
while
(n
2:
3 and
n
~
100)
n
:=n+
1
end while
predicate:
2n
+
I
~
2"
Exercises
69
each contain a while loop annotated with a pre
and a postcondition and also a loop invariant.
In
each case, use
the loop invariant theorem
to
prove the correctness
of
the loop
with respect to the pre and postconditions.
6.
[Precoruiition: m is a nonnegative
integer,
x is a real number,
i
=
0,
andexp
=
J.]
while
(i
=1=
m)
I.
exp
:=
exp·x
2.i
:=i+
I
end while
[Postcondition: exp
=
x
m
]
loop invariant:
J
(n)
is
"exp
=
x"
and
i
=
n."
7. [Precoruiition: largest
=
A[I]
arui i
=
II
while
(i
=1=
m)
I.
i
:=
i
+
I
2.
irA
[i]
>
largest
then
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 WOUTERS
 Algebra, Natural number, Euclidean algorithm, Loop invariant, postcondition

Click to edit the document details