Incorrect Answer.
We consider the body of the program's only while-statement
z = z + x;
a = a + 1;
To see whether
z = a * y
is an invariant for the while-statement,
we place it as a postcondition at the bottom of the loop-body and
push it upwards through that body to determine the weakest
precondition that guarantees
z = a * y
to hold after the execution of the loop's body:
(z + x) = (a + 1) * y
Weakest Precondition
z = z + x;
z = (a + 1) * y
Assignment
a = a + 1;
z = a * y
Assignment
- The weakest precondition we determined is
(z + x) = (a + 1) * y
.
- With that at hand, the postcondition
z = a * y
is an invariant for the
program's only while-statement if, and only if,
the weakest precondition is implied by the invariant candidate
z = a * y
in conjunction with the condition of the while-statement's boolean
guard a != y.
- However, the implication
(a = y)
(z = a * y)
((z + x) = (a + 1) * y) is not true in general.
(Please find values for a, x, y, and z for which this implication is false.)
Back to Question.
Next Question.