Author: Uri Blass
Date: 14:18:20 07/07/03
Go up one level in this thread
On July 07, 2003 at 16:41:03, Russell Reagan wrote:
>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
I think that my code do it without goto
In your example there are also simple alternatives without goto.
for (x=0;x<8;x++){
for (y=0;y<8;y++){
if (some_condition())
{
x=8;
y=8;
}
}
}
Another possibility is to define xy=8*x+y and to do
xy=0;
while (xy<64)
{
x=xy>>3;
y=xy&7;
if somecondition()
xy=64;
else
xy++;
}
Note that I added
x=xy>>3; and y=xy&7 only for simplicty and it may be faster not to have these
varaibles and to change the condition to calculate them based on x and y when
you need it.
Uri
This page took 0.01 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.