0

So this is the loop: predicate

$ m + n = odd$

$while (m>=0 $ and $m <=100)$

$m = m +4$

$n = n -2$

$end while$

So this loop is supposed to be true before and after the loop, but i dont know why. Before the loop we have:

$m +n $ = $m+4 +n -2$

if we then have m = 0, because thats the start we get an even number. So how is m +n supposed to be odd before and after the loop

bassie
  • 19
  • $Odd + Even = Odd$ –  Oct 16 '17 at 11:37
  • We imagine to start with values of $m,n$ such that $m+n=odd$ holds (if $m=0$, this means $n = odd$). After each step we move from $m+n$ to $m+4+n-2=m+n+2$. If $m+n=odd$, also $m+n+2=odd$. – Mauro ALLEGRANZA Oct 16 '17 at 11:53
  • @AnotherJohnDoe yes, but that theorem doesnt help with me understanding the loop – bassie Oct 16 '17 at 15:40
  • @MauroALLEGRANZA So by saying $m+n = odd$, so if m =0, then n needs to be odd, we have proven the predicate before the loop. And by saying that we have m+n+2 in the loop and we add even numbers to the equation, while there is an odd one in there, we have proven the predicate at the end of the loop? – bassie Oct 16 '17 at 15:46
  • Yes, because adding to to an odd number produce always an odd number (see: $7+2, 5+2, 11+2$ and so on). – Mauro ALLEGRANZA Oct 16 '17 at 15:48
  • Thanks for your help ! – bassie Oct 16 '17 at 15:56

0 Answers0