c code question

Discussion of chess software programming and technical issues.

Moderator: Ras

benstoker
Posts: 342
Joined: Tue Jan 19, 2010 2:05 am

c code question

Post by benstoker »

Is this:

Code: Select all

          B = FALSE;

            for ( i = 0; i < cnt; i++ )
                if ( HashStack[i] == Position->StateInfo->HASH )
                    B = TRUE;

            if ( B )
                break;
equivalent to this:

Code: Select all

            for ( i = 0; i < cnt; i++ )
                if ( HashStack[i] == Position->StateInfo->HASH )
                    break;
rjgibert
Posts: 317
Joined: Mon Jun 26, 2006 9:44 am

Re: c code question

Post by rjgibert »

No. In the 1st snippet, the break statement will exit from whatever loop or case statement the code snippet is embedded in. In the 2nd snippet, the break statement exits from the for loop itself only. I hope this helps.

Edit: I should add that in first snippet, most of (entire?) the array is examined, where as in the 2nd snippet, the for loop is exited from earlier.
Last edited by rjgibert on Thu Aug 12, 2010 3:22 am, edited 2 times in total.
benstoker
Posts: 342
Joined: Tue Jan 19, 2010 2:05 am

Re: c code question

Post by benstoker »

rjgibert wrote:No. In the 1st snippet, the break statement will exit from whatever loop or case statement the code snippet is embedded in. In the 2nd snippet, the break statement exits from the for loop itself only. I hope this helps.
It's quite obvious now that you say it.
rjgibert
Posts: 317
Joined: Mon Jun 26, 2006 9:44 am

Re: c code question

Post by rjgibert »

It's ok. We all get nailed by such blind spots at one time or another. It helps to have a 2nd pair of eyes.
benstoker
Posts: 342
Joined: Tue Jan 19, 2010 2:05 am

Re: c code question

Post by benstoker »

rjgibert wrote:No. In the 1st snippet, the break statement will exit from whatever loop or case statement the code snippet is embedded in. In the 2nd snippet, the break statement exits from the for loop itself only. I hope this helps.
But ............ let's revisit this.

If cnt is 20, and at cnt=8, the condition is true and B = TRUE, then why iterate through 9-20? It seems on the first instance the condition is true, the break exits without a need to see if the if-condition is true from cnt=9 to cnt=20.
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: c code question

Post by AlvaroBegue »

benstoker wrote: If cnt is 20, and at cnt=8, the condition is true and B = TRUE, then why iterate through 9-20? It seems on the first instance the condition is true, the break exits without a need to see if the if-condition is true from cnt=9 to cnt=20.
No, in the first snippet of code, the loop will go through all 20 iterations, and then it will break out of whatever loop or switch statement the snippet is in.

In the second snippet the `break' applies to the loop on `i'.
rjgibert
Posts: 317
Joined: Mon Jun 26, 2006 9:44 am

Re: c code question

Post by rjgibert »

I'm guessing this is what u are after:

i = 0;
while((i < cnt) && (HashStack[i++] != blah));
if (i < cnt) break;

I'm doing this on my iPhone, so hopefully it's ok.
Aleks Peshkov
Posts: 911
Joined: Sun Nov 19, 2006 9:16 pm
Location: Russia
Full name: Aleks Peshkov

Re: c code question

Post by Aleks Peshkov »

Good C-style is always use {} braces after if, else, for, while, switch, etc. statements.

Code: Select all

          B = FALSE;

            for ( i = 0; i < cnt; i++ )
            {
                if ( HashStack[i] == Position->StateInfo->HASH )
                {
                    B = TRUE;
                }
            }

            if ( B )
            {
                break;
            }
Last edited by Aleks Peshkov on Thu Aug 12, 2010 4:34 pm, edited 1 time in total.
wgarvin
Posts: 838
Joined: Thu Jul 05, 2007 5:03 pm
Location: British Columbia, Canada

Re: c code question

Post by wgarvin »

If you don't want to add an extra test to the condition of the inner loop, another way to break out of both loops at the same time is to use goto. Using goto as a multi-level break statement like this is a completely safe and reasonable use of goto.

Code: Select all

    /* some outer loop.. */
    while (whatever)
    {
        for ( i = 0; i < cnt; i++ )
        {
            if ( HashStack[i] == Position->StateInfo->HASH )
                goto Label1;
        }
    }
    Label1:;

I usually prefer to write this rather than adding extra conditions to my loops; it seems clearer to say exactly what I mean even if the compiler is capable of rearranging them both into the same thing during its optimization passes.
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: c code question

Post by AlvaroBegue »

Aleks Peshkov wrote:Good C-style is always use {} braces after if, else, for, while, switch, etc. statements.
A broad consensus on this matter doesn't exist. I use {} braces around anything that spans more than one line.

Code: Select all

  B = FALSE;

  for (i = 0; i < cnt; i++) {
    if (HashStack[i] == Position->StateInfo->HASH)
      B = TRUE;
  }

  if (B)
    break;
Like Wylie Garvin, I also recommend using `goto' to break out of nested loops. Alternatively, if the loop can be given a clear name and a clean interface, I might move it to its own function, and then you just need to use `return'. As a general rule, do whatever makes the code most clear.