Loopy C Puzzle (2011)
susam.in
susam.in
Wouldn't it be just after |INT_MIN| iterations?
I guess it depends on your definition of an iteration. I view an iteration as including the iteration expression (i-- in this case) at the end. So during 7 iterations, i-- has executed 7 times, so the value of i is -7. You seem to be saying the iteration expression belongs to the next iteration as opposed to belonging to the currently ending iteration.
I think my view is the widely accepted view. Check out how cppreference rewrites a for loop:
{
init_statement
while ( condition ) {
statement
iteration_expression ;
}
}
In our specific case: {
i = 0;
while (i < 6) {
printf(".");
i--;
}
}
https://en.cppreference.com/w/cpp/language/forAlso the article doesn't say "running |INT_MIN| + 1 iterations" it says "after |INT_MIN| + 1 iterations". I think in that case, even if you don't consider the final i-- to be part of the "n + 1 iterations", you would still consider the final i-- to be done "after |INT_MIN| + 1 iterations".
> I view an iteration as including the iteration expression (i-- in this case) at the end.
Then you should agree that running from 0 to -6 (inclusive) is 7 iterations.
Example:
#include <stdio.h>
int main()
{
int i;
for (i = 0; i >= -6; i--, printf("iterated\n")) {
printf("i = %d\n", i);
}
return 0;
}
Output: i = 0
iterated
i = -1
iterated
i = -2
iterated
i = -3
iterated
i = -4
iterated
i = -5
iterated
i = -6
iteratedIf we consider an iteration to be complete when i-- (the iteration expression) has been completed execution, then yes, the value of the variable i is decremented to -6 when the 6th iteration completes.
In fact, the value of the variable i is decremented to -7 when the 7th iteration completes. The language used in this blog post regarding the iterations (which is also quoted by Thorrez) is indeed sloppy. I will update it soon and fix it. Thank you, jonsen and Thorrez, for paying attention to the details!
To say or perform again; repeat. synonym: repeat. To utter or do again; repeat: as, to iterate an advice or a demand. Repeated.
So x+1 times is x iterations.
- From 1 to 10 there is 9
- From 1 to 10 there is 10
How far from 0 to INT_MIN - it depends.
Start 0 1. iteration -1 2. iteration -2 3. iteration -3 ... at the end of the |INT_MIN|’th iteration INT_MIN
Start 0 1. iteration -1 2. iteration -2 3. iteration -3 ... at the end of the |INT_MIN|’th iteration INT_MIN
But even if you include it, it is ran exactly as many times as the body of the loop, so not sure why you bring it up?
The article isn't just talking about what happens in the body. It's also talking about what the value of i is after an iteration is done.
>But even if you include it, it is ran exactly as many times as the body of the loop, so not sure why you bring it up?
That's exactly why I bring it up:
After 0 iterations, the iteration statement ran 0 times, so i is 0.
After 1 iteration, the iteration statement ran 1 time, so i is -1.
After 2 iterations, the iteration statement ran 2 times, so i is -2.
...
After |INT_MIN| iterations, the iteration statement ran |INT_MIN| times, so i is INT_MIN.
This directly contradicts the article. My point is that the article has an error.
If on the other hand you say the iteration statement isn't part of the iteration you get:
After 0 iterations, the iteration statement ran 0 times, so i is 0.
After 1 iteration, the iteration statement ran 0 times, so i is 0.
After 2 iterations, the iteration statement ran 1 time, so i is -1.
...
After |INT_MIN| + 1 iterations, the iteration statement ran |INT_MIN| times, so i is INT_MIN.
I don't agree with that point of view. But it does agree with the article.
Don't know if OP is the author, but the code under the statement is not the correct one
* https://blog.llvm.org/posts/2011-05-13-what-every-c-programm...
* https://blog.regehr.org/archives/213
* https://devblogs.microsoft.com/oldnewthing/20140627-00/?p=63...