6 ms·
Running from 0 to -6 is 7 iterations. Running from 0 to INT_MIN is |INT_MIN| + 1 iterations.
by tomsmeding 6y ago
Running from 0 to -6 is 7 iterations. Running from 0 to INT_MIN is |INT_MIN| + 1 iterations.
- Thorrez 6y ago>Running from 0 to -6 is 7 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/for https://en.cppreference.com/w/cpp/language/for Also 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".
- susam 6y ago>> Running from 0 to -6 is 7 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 iterated
- jonsen 6y ago7 times through the loop is 6 iterations. It’s the first time that is iterated, repeated.
- susam 6y agoIsn't the first pass through the body of the loop also often colloquially called "the first iteration"?
- jonsen 6y agoYes, but look at your example here. The value is decremented to -6 after the 6. iteration, right? That’s what Thorrez is saying at the start of this thread.
- susam 6y agoThank you for elaborating the terminology issue in your comments. I do see Thorrez's point now. If 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!
- deleted 6y ago[deleted]
- user-the-name 6y agoThe body of the loop will run |INT_MIN| + 1 times. I don't think there is a more well-accepted definition of "iteration" than that.
- jonsen 6y ago> ...well-accepted definition of "iteration"... 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.
- user-the-name 6y agoI don't think I have ever heard a programmer say that running a piece of code two times is "one iteration".
- jonsen 6y agoBut they can say both of: - From 1 to 10 there is 9 - From 1 to 10 there is 10 How far from 0 to INT_MIN - it depends.
- user-the-name 6y agoNone of that affects the fact that if a loop runs 10 times, all programmers will say that it ran for 10 iterations.
- jonsen 6y agoYes. My point here is that Thorrez at the start of this thread may have a point: Start 0 1. iteration -1 2. iteration -2 3. iteration -3 ... at the end of the |INT_MIN|’th iteration INT_MIN
- wruza 6y agoYou may define or argue that it is, but it's not a common sense for anyone.