void
f( int n)
{
for (int i = 0; n != 0; ++i)
{
printf( "%d\n", n);
}
}
Note that n is tested in the termination condition, but is not changed in the for loop body.
Here is cppcheck producing a warning for this code:
$ ~/llvm/results/bin/clang++ -c -Wall apr13a.cc
apr13a.cc:7:18: warning: variable 'n' used in loop condition not modified in
loop body [-Wfor-loop-analysis]
for (int i = 0; n != 0; ++i)
^
1 warning generated.
$
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Consider the following broken code:
include <stdio.h>
void
f( int n)
{
for (int i = 0; n != 0; ++i)
{
printf( "%d\n", n);
}
}
Note that n is tested in the termination condition, but is not changed in the for loop body.
Here is cppcheck producing a warning for this code:
$ ~/llvm/results/bin/clang++ -c -Wall apr13a.cc
apr13a.cc:7:18: warning: variable 'n' used in loop condition not modified in
loop body [-Wfor-loop-analysis]
for (int i = 0; n != 0; ++i)
^
1 warning generated.
$