If you need 10 fence panels then you need 11 fenceposts. Similarly, 5 to 7 is 3 iterations, not (7-5)=2.
Mike.
Yup. I used the term loosely--if you Google it* you'll find a more strict definition than what I meant, but essentially it's an off-by-one error. Like Mike said, in the case of the loop you posted, it will iterate 11 times instead of 10 because of a logic error.
(* When you encounter a new term like "fencepost error", a good idea is to Google something like "wiki fencepost error" to see the Wikipedia article on it. It's usually faster and more accurate than waiting for a reply to a forum post, I find.)
The loop checks the value of k at the end of the loop instead of at the beginning. Because of this (and because k starts at 0, not 1), it must exit as soon as k = 10. Otherwise, when it sees that k is 10, and tests whether k < 10, it sees that k is not greater than 10, and does an extra iteration.
So you either need to use k = 10 (or k >= 10, which is safer but perhaps costlier in terms of an extra cycle or two), or else check if k > 10 at the beginning of the loop:
PHP:
while ($k < 10) {
$sum = $sum + 1;
$cnt = $sum;
$k = $k + 1;
printf("%10d %10d %10d\n", $k, $sum, $cnt);
}
I don't know if your language has the while() loop, but it probably has something like it. The above code is in PHP but should be similar in many languages.
Good luck,
Torben