# Loop invariant

**URL:** <https://forum.kirupa.com/t/loop-invariant/224974>\
**Category:** random\
**Created:** [May 7, 2007, 6:32pm UTC](https://forum.kirupa.com/t/loop-invariant/224974 "2007-05-07T18:32:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kdd](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kdd](https://forum.kirupa.com/u/kdd)\
**Post date:** [May 7, 2007, 6:32pm UTC](https://forum.kirupa.com/t/loop-invariant/224974/1 "2007-05-07T18:32:40Z")

</div>

Can anyone please tell me what the loop invariant of this code is?

```auto

int a = n;
int b = 0;
while ( a > 0 )
{
     a--;
     b = b + 2;
}

```

At first, I thought n, but it’s wrong…  
After reading stuff about loop invariants, I think it’s a\>0 and b = b+2, because they both hold true…

So, what’s the loop invariant?  
Thanks. :beam:
