0
  int y = 0;
  while(y<x){
    y = y+1;
  }

I wanted to prove that y is equal to x if x is > 0, otherwise it equals to 0. What's the loop invariant that allows me to prove that? For the case where x > 0, I came up with the invariant i <= x and y = i where i is an extra variable that I introduced to represent the current loop iteration. Is there a way to write the invariant w/o introducing the extra variable?

JRR
  • 6,014
  • 6
  • 39
  • 59
  • 1
    The invariant is simply `y <= x or x < 0`. – kaya3 Jun 26 '21 at 21:41
  • @kaya3 is your invariant strong enough to prove that y = 0 if x < 0? Doesn't seem like the case but maybe I am missing something – JRR Jun 30 '21 at 06:50

0 Answers0