Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Yet another MSVC weird behaviour

Author: Dan Newman

Date: 10:31:55 12/28/01

Go up one level in this thread


On December 28, 2001 at 09:33:30, David Rasmussen wrote:

>On December 27, 2001 at 16:56:21, Michel Langeveld wrote:
>
>>
>>Although I think the problem you posted before is already solved I still answer
>>your question. It's good to write:
>>
>>int i;
>>
>>for (i=0; i < 8; i++)
>>{
>>   /* i = 0,1,2,3,4,5,6,7 here */
>>}
>>
>>/* i = 8 here */
>>
>>But you have to consider that if you use this construction that after this for
>>i=8 in the end.
>
>I'm not sure what you're saying here, but as for good style, this is recommended
>(in C++ notation):
>
>
>There are several points here:
>
>1. The variable is declared close to where it is used. Enhances readability and
>maintainability. Can't be done in C though.

Actually, it can be done in C, but it's pretty ugly.  Just use
a block:

  {
    int i;
    for (i = 0; i != 8; ++i)
    {
      ...
    }
  }

-Dan.

>2. The increment is written "++i" to emphasize that it is an incrementation and
>nothing else. You only write it for it's side effect, you dont use the value of
>the expression.
>3. Most importantly to this discussion: The continuation condition "i != 8" is
>more clear, since the invariant that holds after the loop (the post condition)
>will always by definition be !(i != 8) which is i == 8. If you had "i < 8", your
>post condition is !(i < 8) which is i >= 8. So you dont really know that i == 8
>when you're done. If you do something to i in the loop (maybe as a bug), i == 8
>might not be true at the end.
>
>/David



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.