Is using goto a legitimate way to break out of two loops?

风格不统一 提交于 2019-11-30 11:08:37

return is a "structured" goto which many programmers find more acceptable! So:

static int findit(int sum, int* pa, int* pb, int* pc)
{
    for (int a = 1; a<sum; a++) {
        for (int b = 1; b < sum; b++) {
            int c = sum-a-b;
            if (a*a+b*b == c*c) {
                *pa = a; *pb = b; *pc = c;
                return a*b*c;
        }
    }
    return -1;    
}

int main() {
    int a, b, c;
    const int sum = 1000;
    int result = findit(sum, &a, &b, &c);
    if (result == -1) {
        std::cout << "No result!" << std::endl;
        return 1;
    }
    std::cout << "a:" << a << std::endl;
    std::cout << "b:" << b << std::endl;
    std::cout << "c:" << c << std::endl;
    std::cout <<"Result:" << result << std::endl;
    return 0;
}

In my opinion it's fine to use goto in a situation like this.

Btw, the condescending preaching against goto usually comes from people who just parrot what they heard others say or read somewhere..

Matthew Vines

See this question about breaking out of 2 loops. There are much better answers provided than using a goto.

The best answer provided is to place your second loop into a function, and call that function from inside your first loop.

code copied from mquander's response

public bool CheckWhatever(int whateverIndex)
{
    for(int j = 0; j < height; j++)
    {
        if(whatever[whateverIndex][j]) return false;
    }

    return true;
}

public void DoubleLoop()
{
    for(int i = 0; i < width; i++)
    {
        if(!CheckWhatever(i)) break;
    }
}

Though I do feel that using a goto in this case isn't quite as bad as killing kittens. But it's close.

I can't think of a better alternative. But one alternative not using goto would be modifying the first for-loop:

for (a = 1; a<sum && result == -1; a++){

Then break out of the second for-loop. That will work assuming the result will never be -1 after the second for-loop has been broken by break.

You could declare a bool found = false at the top and then add && !found to your for loop conditionals (after a < sum and b < sum) and then set found to true where your current goto is. Then make your output conditional on found being true.

int a,b,c,sum = 1000;
for (a = 1; a<sum; ++a)
 for (b = 1; b<sum; ++b){
  c = sum-a-b;
  if (a*a+b*b == c*c) sum = -a*b*c;
 }
printf("a: %d\n",a-1);
printf("b: %d\n",b-1);
printf("c: %d\n",c);
printf("Result: %d\n",-sum);

Also optimized result out.. :P

Anyway i love gotos!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!