Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about optimizing code

Author: Russell Reagan

Date: 13:41:03 07/07/03

Go up one level in this thread


On July 07, 2003 at 16:21:45, Uri Blass wrote:

>I almost do not use goto in my program and I learned that I should not do it.

goto is not bad always. A long time ago people didn't know the correct way to
use goto, and they used it in a lot of wrong ways, so everyone started saying,
"don't use goto! goto is bad!" and that made people think that it was bad. It is
only bad if you don't know when to use it. It is ok to use it to jump out of a
nested loop, for instance.

for (x = 0; x < 8; x++) {
    for (y = 0; y < 8; y++) {
        if (some_condition())
            goto done;
    }
}
done:

In this case, you want to get out of BOTH loops, so you can't just use a
'break'. You could create extra variables and add more if statements to
accomplish the same thing, but a goto works best in this situation, and it's
okay to use it here.



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.