Docsity

Loop Invariants-Methods of Software Engineering-Lecture Slides, Slides of Software Engineering

Partial preview of the text

Download Loop Invariants-Methods of Software Engineering-Lecture Slides and more Slides Software Engineering in PDF only on Docsity!

Loop Invariants^ s = 0;for i := 1 to n do

s = s + a[i];

What is the ‘loop invariant’?

s is the sum of elements from a[1] to a[i] immediately before i is incremented!

Weakest Precondition for While

Statement

{P} while B do S {Q}

-^ Let W be while B do S -^ condition for termination of the loopP

 0 (not B) P^1

^ B and wp(S,P

)^0

^ wp(S, not B)

Pk

^ B and wp(S, P

)k-

  • wp(W, true)

wp(W, not B)

(k: k

^

0: P

)k docsity.com

a = 0;i = 0;while (i < N)

a = a + i++; Loop Invariant: a =

^1 N ^0 i

i