<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
loop	B-Application
invariant	I-Application
is	O
a	O
property	O
of	O
a	O
program	B-Application
loop	O
that	O
is	O
true	O
before	O
(	O
and	O
after	O
)	O
each	O
iteration	O
.	O
</s>
<s>
Knowing	O
its	O
invariant(s )	O
is	O
essential	O
in	O
understanding	O
the	O
effect	O
of	O
a	O
loop	O
.	O
</s>
<s>
In	O
formal	O
program	B-Application
verification	O
,	O
particularly	O
the	O
Floyd-Hoare	O
approach	O
,	O
loop	B-Application
invariants	I-Application
are	O
expressed	O
by	O
formal	O
predicate	O
logic	O
and	O
used	O
to	O
prove	O
properties	O
of	O
loops	O
and	O
by	O
extension	O
algorithms	O
that	O
employ	O
loops	O
(	O
usually	O
correctness	O
properties	O
)	O
.	O
</s>
<s>
The	O
loop	B-Application
invariants	I-Application
will	O
be	O
true	O
on	O
entry	O
into	O
a	O
loop	O
and	O
following	O
each	O
iteration	O
,	O
so	O
that	O
on	O
exit	O
from	O
the	O
loop	O
both	O
the	O
loop	B-Application
invariants	I-Application
and	O
the	O
loop	O
termination	O
condition	O
can	O
be	O
guaranteed	O
.	O
</s>
<s>
From	O
a	O
programming	O
methodology	O
viewpoint	O
,	O
the	O
loop	B-Application
invariant	I-Application
can	O
be	O
viewed	O
as	O
a	O
more	O
abstract	O
specification	O
of	O
the	O
loop	O
,	O
which	O
characterizes	O
the	O
deeper	O
purpose	O
of	O
the	O
loop	O
beyond	O
the	O
details	O
of	O
this	O
implementation	O
.	O
</s>
<s>
A	O
survey	O
article	O
covers	O
fundamental	O
algorithms	O
from	O
many	O
areas	O
of	O
computer	B-General_Concept
science	I-General_Concept
(	O
searching	O
,	O
sorting	O
,	O
optimization	O
,	O
arithmetic	O
etc	O
.	O
</s>
<s>
)	O
,	O
characterizing	O
each	O
of	O
them	O
from	O
the	O
viewpoint	O
of	O
its	O
invariant	B-Application
.	O
</s>
<s>
Because	O
of	O
the	O
similarity	O
of	O
loops	O
and	O
recursive	O
programs	O
,	O
proving	O
partial	O
correctness	O
of	O
loops	O
with	O
invariants	B-Application
is	O
very	O
similar	O
to	O
proving	O
correctness	O
of	O
recursive	O
programs	O
via	O
induction	B-Algorithm
.	O
</s>
<s>
In	O
fact	O
,	O
the	O
loop	B-Application
invariant	I-Application
is	O
often	O
the	O
same	O
as	O
the	O
inductive	O
hypothesis	O
to	O
be	O
proved	O
for	O
a	O
recursive	O
program	B-Application
equivalent	O
to	O
a	O
given	O
loop	O
.	O
</s>
<s>
The	O
following	O
C	B-Language
subroutine	O
max( )	O
returns	O
the	O
maximum	O
value	O
in	O
its	O
argument	O
array	O
a[],	O
provided	O
its	O
length	O
n	O
is	O
at	O
least	O
1	O
.	O
</s>
<s>
They	O
thus	O
describe	O
an	O
invariant	B-Application
property	O
of	O
the	O
loop	O
.	O
</s>
<s>
When	O
line	O
13	O
is	O
reached	O
,	O
this	O
invariant	B-Application
still	O
holds	O
,	O
and	O
it	O
is	O
known	O
that	O
the	O
loop	O
condition	O
i	O
!=	O
n	O
from	O
line	O
5	O
has	O
become	O
false	O
.	O
</s>
<s>
Following	O
a	O
defensive	B-Application
programming	I-Application
paradigm	O
,	O
the	O
loop	O
condition	O
i	O
!=	O
n	O
in	O
line	O
5	O
should	O
better	O
be	O
modified	O
to	O
ixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
=	O
n	O
is	O
known	O
in	O
line	O
13	O
.	O
</s>
<s>
In	O
order	O
to	O
obtain	O
that	O
also	O
i	O
<=	O
n	O
holds	O
,	O
that	O
condition	O
has	O
to	O
be	O
included	O
into	O
the	O
loop	B-Application
invariant	I-Application
.	O
</s>
<s>
It	O
is	O
easy	O
to	O
see	O
that	O
i	O
<=	O
n	O
,	O
too	O
,	O
is	O
an	O
invariant	B-Application
of	O
the	O
loop	O
,	O
since	O
i	O
<	O
n	O
in	O
line	O
6	O
can	O
be	O
obtained	O
from	O
the	O
(	O
modified	O
)	O
loop	O
condition	O
in	O
line	O
5	O
,	O
and	O
hence	O
i	O
<=	O
n	O
holds	O
in	O
line	O
11	O
after	O
i	O
has	O
been	O
incremented	O
in	O
line	O
10	O
.	O
</s>
<s>
However	O
,	O
when	O
loop	B-Application
invariants	I-Application
have	O
to	O
be	O
manually	O
provided	O
for	O
formal	O
program	B-Application
verification	O
,	O
such	O
intuitively	O
too	O
obvious	O
properties	O
like	O
i	O
<=	O
n	O
are	O
often	O
overlooked	O
.	O
</s>
<s>
This	O
triple	O
is	O
actually	O
a	O
relation	B-Algorithm
on	O
machine	O
states	O
.	O
</s>
<s>
If	O
this	O
relation	B-Algorithm
can	O
be	O
proven	O
,	O
the	O
rule	O
then	O
allows	O
us	O
to	O
conclude	O
that	O
successful	O
execution	O
of	O
the	O
program	B-Application
will	O
lead	O
from	O
a	O
state	O
in	O
which	O
is	O
true	O
to	O
a	O
state	O
in	O
which	O
holds	O
.	O
</s>
<s>
The	O
boolean	O
formula	O
in	O
this	O
rule	O
is	O
called	O
a	O
loop	B-Application
invariant	I-Application
.	O
</s>
<s>
With	O
some	O
variations	O
in	O
the	O
notation	O
used	O
,	O
and	O
with	O
the	O
premise	O
that	O
the	O
loop	O
halts	O
,	O
this	O
rule	O
is	O
also	O
known	O
as	O
the	O
Invariant	B-Application
Relation	B-Algorithm
Theorem	O
.	O
</s>
<s>
The	O
condition	O
C	B-Language
of	O
the	O
while	O
loop	O
is	O
.	O
</s>
<s>
A	O
useful	O
loop	B-Application
invariant	I-Application
has	O
to	O
be	O
guessed	O
;	O
it	O
will	O
turn	O
out	O
that	O
is	O
appropriate	O
.	O
</s>
<s>
The	O
property	O
is	O
another	O
invariant	B-Application
of	O
the	O
example	O
loop	O
,	O
and	O
the	O
trivial	O
property	O
is	O
another	O
one	O
.	O
</s>
<s>
Applying	O
the	O
above	O
inference	O
rule	O
to	O
the	O
former	O
invariant	B-Application
yields	O
.	O
</s>
<s>
Applying	O
it	O
to	O
invariant	B-Application
yields	O
,	O
which	O
is	O
slightly	O
more	O
expressive	O
.	O
</s>
<s>
The	O
Eiffel	B-Language
programming	I-Language
language	I-Language
provides	O
native	O
support	O
for	O
loop	B-Application
invariants	I-Application
.	O
</s>
<s>
A	O
loop	B-Application
invariant	I-Application
is	O
expressed	O
with	O
the	O
same	O
syntax	O
used	O
for	O
a	O
class	B-Application
invariant	I-Application
.	O
</s>
<s>
In	O
the	O
sample	O
below	O
,	O
the	O
loop	B-Application
invariant	I-Application
expression	O
x	O
<=	O
10	O
must	O
be	O
true	O
following	O
the	O
loop	O
initialization	O
,	O
and	O
after	O
each	O
execution	O
of	O
the	O
loop	O
body	O
;	O
this	O
is	O
checked	O
at	O
runtime	O
.	O
</s>
<s>
The	O
Whiley	B-Language
programming	O
language	O
also	O
provides	O
first-class	O
support	O
for	O
loop	B-Application
invariants	I-Application
.	O
</s>
<s>
Loop	B-Application
invariants	I-Application
are	O
expressed	O
using	O
one	O
or	O
more	O
where	O
clauses	O
,	O
as	O
the	O
following	O
illustrates	O
:	O
</s>
<s>
The	O
loop	B-Application
invariant	I-Application
is	O
defined	O
inductively	O
through	O
two	O
where	O
clauses	O
,	O
each	O
of	O
which	O
corresponds	O
to	O
a	O
clause	O
in	O
the	O
postcondition	O
.	O
</s>
<s>
The	O
fundamental	O
difference	O
is	O
that	O
each	O
clause	O
of	O
the	O
loop	B-Application
invariant	I-Application
identifies	O
the	O
result	O
as	O
being	O
correct	O
up	O
to	O
the	O
current	O
element	O
i	O
,	O
whilst	O
the	O
postconditions	O
identify	O
the	O
result	O
as	O
being	O
correct	O
for	O
all	O
elements	O
.	O
</s>
<s>
A	O
loop	B-Application
invariant	I-Application
can	O
serve	O
one	O
of	O
the	O
following	O
purposes	O
:	O
</s>
<s>
For	O
2.	O
,	O
programming	O
language	O
support	O
is	O
required	O
,	O
such	O
as	O
the	O
C	B-Language
library	O
assert.h	B-Language
,	O
or	O
the	O
above-shown	O
invariant	B-Application
clause	O
in	O
Eiffel	B-Language
.	O
</s>
<s>
The	O
technique	O
of	O
abstract	O
interpretation	O
can	O
be	O
used	O
to	O
detect	O
loop	B-Application
invariant	I-Application
of	O
given	O
code	O
automatically	O
.	O
</s>
<s>
However	O
,	O
this	O
approach	O
is	O
limited	O
to	O
very	O
simple	O
invariants	B-Application
(	O
such	O
as	O
0	O
<=	O
i	O
&&	O
i	O
<=	O
n	O
&&	O
i%2	O
==	O
0	O
)	O
.	O
</s>
<s>
Loop-invariant	O
code	O
consists	O
of	O
statements	O
or	O
expressions	O
that	O
can	O
be	O
moved	O
outside	O
a	O
loop	O
body	O
without	O
affecting	O
the	O
program	B-Application
semantics	O
.	O
</s>
<s>
Such	O
transformations	O
,	O
called	O
loop-invariant	O
code	O
motion	O
,	O
are	O
performed	O
by	O
some	O
compilers	O
to	O
optimize	B-Application
programs	O
.	O
</s>
<s>
where	O
the	O
calculations	O
x	O
=	O
y+z	O
and	O
x*x	O
can	O
be	O
moved	O
before	O
the	O
loop	O
,	O
resulting	O
in	O
an	O
equivalent	O
,	O
but	O
faster	O
,	O
program	B-Application
:	O
</s>
<s>
the	O
property	O
0	O
<=	O
i	O
&&	O
i	O
<=	O
n	O
is	O
a	O
loop	B-Application
invariant	I-Application
for	O
both	O
the	O
original	O
and	O
the	O
optimized	O
program	B-Application
,	O
but	O
is	O
not	O
part	O
of	O
the	O
code	O
,	O
hence	O
it	O
does	O
n't	O
make	O
sense	O
to	O
speak	O
of	O
"	O
moving	O
it	O
out	O
of	O
the	O
loop	O
"	O
.	O
</s>
<s>
Loop-invariant	O
code	O
may	O
induce	O
a	O
corresponding	O
loop-invariant	O
property	O
.	O
</s>
<s>
For	O
the	O
above	O
example	O
,	O
the	O
easiest	O
way	O
to	O
see	O
it	O
is	O
to	O
consider	O
a	O
program	B-Application
where	O
the	O
loop	B-Application
invariant	I-Application
code	O
is	O
computed	O
both	O
before	O
and	O
within	O
the	O
loop	O
:	O
</s>
<s>
A	O
loop-invariant	O
property	O
of	O
this	O
code	O
is	O
(	O
x1	O
==	O
x2	O
&&	O
t1	O
==	O
x2*x2	O
)	O
||	O
i	O
==	O
0	O
,	O
indicating	O
that	O
the	O
values	O
computed	O
before	O
the	O
loop	O
agree	O
with	O
those	O
computed	O
within	O
(	O
except	O
before	O
the	O
first	O
iteration	O
)	O
.	O
</s>
