Loop invariant solution |
Document last modified: |
Sample Loop
|
|
i=0
Therefore 0 ≤ j < 0 implies A[j] is empty and A[j]=2*A[j]
Since A[i]=2*A[i] and i=i+1
When i=0,1,2,3 then 0 ≤ j < i implies 0 ≤ j < 3 and A[j]=2*A[j]
A[i]←2*old(A[i]) where old(A[i]) is the A[i] value before assignment
Add 1 to i:
i←i+1
loop invariant A[j]=2*A[j] holds when 0 ≤ j < i.
loop invariant holds
loop condition (i < 3) does not hold
i=3
loop invariant A[j]=2*A[j] holds for 0 ≤ j < i
Therefore: A[j]=2*A[j] holds for 0 ≤ j < 3