我怎样才能在没有 goto 的情况下(干净地)重写这个?

发布于 2024-08-27 17:31:51 字数 393 浏览 16 评论 0原文

我怎样才能干净地做到这一点而不需要 goto?

loop:
  if(condition1){
    something();
  } else if (condition2) {
    somethingDifferent();
  } else {
    mostOfTheWork();
    goto loop;
  }

我也不想使用休息时间。此外,在执行其他操作之前,预计会循环几次(最多 40 次),因此 mostOfTheWork 部分很可能会尽可能高,即使只是为了可读性。提前致谢。

编辑:这是在编译器优化器在中断时表现不佳的误解下发布的,虽然一开始通常很愚蠢,但我已经证明对自己来说是错误的 通过(性能)实验。另一方面,感谢您的回答;它们是不同风格的有趣读物。

How can I do this cleanly without gotos?

loop:
  if(condition1){
    something();
  } else if (condition2) {
    somethingDifferent();
  } else {
    mostOfTheWork();
    goto loop;
  }

I'd prefer not to use breaks as well. Furthermore, it is expected to loop several (adv 40) times before doing something else, so the mostOfTheWork part would most likely be as high up as possible, even if just for readability. Thanks in advance.

EDIT: This was posted under the misconception that the compiler optimizer worked poorly with breaks, which, while generally stupid to begin with, I have proven incorrect to myself
through experimentation (of performance). On the other hand, thank you for your answers; they have been interesting reads on varying styles.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

彡翼 2024-09-03 17:31:51

显然,如果任一条件触发,您都会跳出循环。

    while ((!condition1) && (!condition2)) {
      MostOfTheWork();
    }
    if (condition1) {
      something();
    } else if (condition2) {
      somethingDifferent();
    }

因此,在两个条件都没有触发时循环,然后看看哪一个条件满足了你的要求。

现在,有人会尖叫说我对条件的评估超出了我的需要
到。为了他们的利益,修改版本:

{
  bool t1, t2;
  while ((!(t1 = condition1)) && (!(t2 =condition2))) {
    MostOfTheWork();
  }
  if (t1) {
    something();
  } else if (t2) {
    somethingDifferent();
  }
}

Clearly, you will break out of the loop if either condition fires.

    while ((!condition1) && (!condition2)) {
      MostOfTheWork();
    }
    if (condition1) {
      something();
    } else if (condition2) {
      somethingDifferent();
    }

So, loop while neither condition has fired, then see which one got you.

Now, someone will scream that I evaluated the conditions more than I needed
to. For their benefit, a modified version:

{
  bool t1, t2;
  while ((!(t1 = condition1)) && (!(t2 =condition2))) {
    MostOfTheWork();
  }
  if (t1) {
    something();
  } else if (t2) {
    somethingDifferent();
  }
}
梦纸 2024-09-03 17:31:51
bool done = false ;

while( !done ) {
  if(condition1){
    something();
    done = true ;
  } else if (condition2) {
    somethingDifferent();
    done = true ;
  } else {
    mostOfTheWork();
  }
} 
bool done = false ;

while( !done ) {
  if(condition1){
    something();
    done = true ;
  } else if (condition2) {
    somethingDifferent();
    done = true ;
  } else {
    mostOfTheWork();
  }
} 
离不开的别离 2024-09-03 17:31:51

没有休息?

function foo(){
  if(condition1){
    something();
    return;
  } else if (condition2) {
    somethingDifferent();
    return;
  }
  mostOfTheWork();
  foo(); //(Tail recursive for those worried about stack overflows)
}

然而,中断是用于流量控制的,它们的作用比 goto 更清楚,所以我建议使用它们。但是在这种情况下,我会推荐@John的答案 作为正确的方法。

Without breaks?

function foo(){
  if(condition1){
    something();
    return;
  } else if (condition2) {
    somethingDifferent();
    return;
  }
  mostOfTheWork();
  foo(); //(Tail recursive for those worried about stack overflows)
}

However, breaks are there for flow control, they are much clearer as to what they do than goto, so I would recommend using them. However in this case I would recommend @John's answer as the correct way to do it.

旧街凉风 2024-09-03 17:31:51

没有 goto 或喙。一如既往的不干净,主观

 do {
   if ( condition1 )
     something();
   else if( condition2 )
     somethingElse();
   else
     mostOfTheWork();
 } while( !(condition1 || condition2) );

这当然是非常愚蠢的。只需休息一下即可。

No gotos or beaks. Cleaniless as always, subjective

 do {
   if ( condition1 )
     something();
   else if( condition2 )
     somethingElse();
   else
     mostOfTheWork();
 } while( !(condition1 || condition2) );

This is of course, incredibly silly. Just use a break.

撧情箌佬 2024-09-03 17:31:51

计算机科学教授可能已经在你的脑海中灌输了这样的观念:这是不好的,并且会对你的代码造成严重破坏。当正确且有目的地使用时,Goto 实际上非常有效。这就是为什么大多数编程语言都没有放弃它。

在您的特定情况下,goto 很好,因为代码块很小,您可以轻松地看到程序流程。您也可以使用其他控制结构编写意大利面条式代码,尽管这样做有点困难。

You've probably had it drilled into your head by CS professors that it's bad and will wreak havoc in your code. Gotos are actually quite efficient when used properly and purposely. This is why most programming languages haven't dropped it.

In your particular case, the goto is fine because the block of code is small and you can easily see the program flow. You can write spaghetti code with other control structures too, although it's a little harder to do.

盗琴音 2024-09-03 17:31:51

由于您没有具体了解某种语言,因此某些语言具有 continuenextskip,您可以使用它们来代替该 转到

Since you weren't specific about a language, some languages have continue, next or skip that you can use in place of that goto.

隔纱相望 2024-09-03 17:31:51
do
{
  if(condition1){
    something();
  } else if (condition2) {
    somethingDifferent();
  } else {
    mostOfTheWork();
  }
} while (!condition1 && !condition2)
do
{
  if(condition1){
    something();
  } else if (condition2) {
    somethingDifferent();
  } else {
    mostOfTheWork();
  }
} while (!condition1 && !condition2)
你丑哭了我 2024-09-03 17:31:51

在我看来,这个版本的代码最清楚地向后代传达了程序流程,并且最容易扩展。是的,我正在使用 break。我想不出有什么真正的理由不这样做。

while(true)
{
    if (condition1)
    {
        something();
        break;
    }
    if (condition2)
    {
        somethingDifferent();
        break;
    }
    mostOfTheWork();
}

如果你确实不想使用 break,可以使用 goto 跳出循环,或者使用 return 跳出循环。函数(如果此循环是较大函数的一部分,则您必须重构)。

while(true)
{
    if (condition1)
    {
        something();
        goto exit;
    }
    if (condition2)
    {
        somethingDifferent();
        goto exit;
    }
    mostOfTheWork();
}
exit:

或者

while(true)
{
    if (condition1)
    {
        something();
        return;
    }
    if (condition2)
    {
        somethingDifferent();
        return;
    }
    mostOfTheWork();
}

,如果您拒绝使用除 if 和 while 之外的任何流量控制,那么这个怎么样:

bool ok = true;
while(ok)
{
    if (condition1)
    {
        something();
        ok = false;
    }
    if (ok && condition2)
    {
        somethingDifferent();
        ok = false;
    }
    if (ok)
    {
        mostOfTheWork();
    }
}

另外,请参阅 我对此类问题的规范答案(并投票!)

In my opinion this version of the code most clearly communicates the program flow to posterity, and is the most easily extended. Yes, I'm using break. I can't think of any real reason not to.

while(true)
{
    if (condition1)
    {
        something();
        break;
    }
    if (condition2)
    {
        somethingDifferent();
        break;
    }
    mostOfTheWork();
}

If you really don't want to use break, you can use goto to get out of the loop, or use return to bail out of the function (if this loop is part of a larger function you'd have to refactor).

while(true)
{
    if (condition1)
    {
        something();
        goto exit;
    }
    if (condition2)
    {
        somethingDifferent();
        goto exit;
    }
    mostOfTheWork();
}
exit:

Or

while(true)
{
    if (condition1)
    {
        something();
        return;
    }
    if (condition2)
    {
        somethingDifferent();
        return;
    }
    mostOfTheWork();
}

And if you refuse to use any flow control other than if and while, how about this one:

bool ok = true;
while(ok)
{
    if (condition1)
    {
        something();
        ok = false;
    }
    if (ok && condition2)
    {
        somethingDifferent();
        ok = false;
    }
    if (ok)
    {
        mostOfTheWork();
    }
}

Also, please see my canonical answer for this sort of question (and vote for it!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文