循环最佳实践

发布于 2024-07-15 18:23:35 字数 143 浏览 11 评论 0原文

我有一个非常大的循环,循环 1000 行。 如果找到幻值 1,我将退出循环。 如果未找到幻值 1 但找到幻值 2,则循环需要跳到开头。 现在我正在使用 switch、一些 if 和 goto。 我读到 goto 不是最好的方法。 有没有更好的方法来完成这项工作?

I have a very large loop that loops a 1000 rows. I exit the loop if magic value 1 is found. If magic value 1 is not found but magic value 2 is found then the loop needs to skip to the beginning. Right now I am using a switch, some ifs and a goto. I have read that goto is not the best way. Is there a better way to make this work?

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

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

发布评论

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

评论(7

野味少女 2024-07-22 18:23:35

要退出循环,您可以使用 break 语句,要转到下一条记录,您可以使用继续声明。

for(int i = 0; i < 1000; i++)
{
    if(magicValue1)
       break;
    if(magicValue2)
       continue;
}

我不赞成使用 GOTO 语句,我只是指出一个可能的用例

您可以使用 goto 跳转语句来启动/退出循环,但我会保留除非您使用嵌套循环,否则请远离此选项。 我认为 goto 语句仍然有其优化、干净退出等用途。但总的来说,最好相当谨慎使用它。

for(int i = 0; i < 100; i++)
{ 
  start:

  for(int i = 0; i < 10; i++)
  {
     if(magicValue1)
       goto end;
    if(magicValue2)
       goto start;
  }
}
end : 

To exit a loop you can use the break statement, to go onto the next record you can use the continue statement.

for(int i = 0; i < 1000; i++)
{
    if(magicValue1)
       break;
    if(magicValue2)
       continue;
}

I AM NOT CONDONING THE USE OF THE GOTO STATEMENT I AM SIMPLY POINTING OUT A POSSIBLE USE CASE

You can use goto jump statement to start/exit a loop, however I would stay away from this option unless you are using nested looping. I think the goto statement still has its uses for optimizing, exiting cleanly ect.. but in general it is best to use it quite sparingly.

for(int i = 0; i < 100; i++)
{ 
  start:

  for(int i = 0; i < 10; i++)
  {
     if(magicValue1)
       goto end;
    if(magicValue2)
       goto start;
  }
}
end : 
陪我终i 2024-07-22 18:23:35

怎么样:

for(int i = 0; i < 1000; i++) {
    if(values[i] == MAGIC_VALUE_1) {
        break;
    } else if(values[i] == MAGIC_VALUE_2) {
        i = 0;
    }
}

如果“跳到开头”的意思是“跳过此记录并处理下一条记录”,请将 i = 0 替换为 继续

How about this:

for(int i = 0; i < 1000; i++) {
    if(values[i] == MAGIC_VALUE_1) {
        break;
    } else if(values[i] == MAGIC_VALUE_2) {
        i = 0;
    }
}

If by "skip to the beginning" you mean "skip this record and process the next one," replace i = 0 with continue.

拧巴小姐 2024-07-22 18:23:35

没有 breakwhile 变体:

bool continue = true; int i = 0;
while (i < 1000 && continue){
    if(values[i] == MAGIC_VALUE_1) {
        continue=false;
    } else if(values[i] == MAGIC_VALUE_2) {
        i = 0;
    }
    i++;
}

A while variation with no break:

bool continue = true; int i = 0;
while (i < 1000 && continue){
    if(values[i] == MAGIC_VALUE_1) {
        continue=false;
    } else if(values[i] == MAGIC_VALUE_2) {
        i = 0;
    }
    i++;
}
笔落惊风雨 2024-07-22 18:23:35

我还不能发表评论(距离 1 个代表点),

但这不是更好吗:

for (int i = 0; i < 1000; i++)
{
    if (magicValue1)
    {
       break;
    }
    else if (magicValue2)
    {
       dosomething();
       i=0;
    }
}

而且我不确定“重新开始搜索”是什么意思。

I can't comment yet ( 1 rep point away)

but wouldn't this be better:

for (int i = 0; i < 1000; i++)
{
    if (magicValue1)
    {
       break;
    }
    else if (magicValue2)
    {
       dosomething();
       i=0;
    }
}

and I'm not sure by whats meant by "restart the search".

甜心小果奶 2024-07-22 18:23:35

我采用#2 情况意味着您不想执行(即跳过)#2 情况中的循环体,而不是您想要将循环重置为 0。(如果我知道,请参阅代码注释向后。)

这个建议可能会引起争议,因为 for 循环中不太传统的条件可以说是自记录规模较低,但如果这不打扰你,一种简洁的方式来写我认为你的想要的是:

        for (int i= 0; i<values.Length && values[i]!= MAGIC_1; i++)
        {
            if (values[i] == MAGIC_2)
            {
                // Don't do the loop body for this case but continue on looping
                continue;
                // If you want to reset the loop to zero instead of skip the 2 case,
                // comment-out the continue; and un-comment the line below:
                // i=0;
            }
            // Do long loop body here
        }

I'm taking #2 case to mean that you want to not perform (i.e. skip) the loop body in the #2 case and not that you want to reset the loop to 0. (See the code comments if I've got that backward.)

This suggestion may be controversial because of the less conventional condition in the for loop could be said to be low on the self-documenting scale, but if that doesn't bother you, a concise way of writing what I think you you want is:

        for (int i= 0; i<values.Length && values[i]!= MAGIC_1; i++)
        {
            if (values[i] == MAGIC_2)
            {
                // Don't do the loop body for this case but continue on looping
                continue;
                // If you want to reset the loop to zero instead of skip the 2 case,
                // comment-out the continue; and un-comment the line below:
                // i=0;
            }
            // Do long loop body here
        }
埖埖迣鎅 2024-07-22 18:23:35

请注意,如果您在 MagicValue 为 2 时将计数器设置回 0,并且您的代码从不更改这些值,则您可能会陷入无限循环。

Just note that if you set the counter back to 0 if MagicValue is 2, and your code never changes the values, you are probably going to be in an infinite loop.

路还长,别太狂 2024-07-22 18:23:35

更复杂的可能是:

我们定义 2 个扩展方法。

public static class Extensions
{
   public static bool isMagic_1(this int i)
   {
         return i == 1;
   }

   public static bool isMagic_2(this int i)
   {
         return i == 2;
   }
}

现在您可以这样做:

  for(int i = 0; i < 1000; i++)
  {
     if(i.isMagic_1())
       break;
     if(i.isMagic_2())
       continue;
  }

希望这会有所帮助!

A more complex could be:

We define 2 Extension Methods.

public static class Extensions
{
   public static bool isMagic_1(this int i)
   {
         return i == 1;
   }

   public static bool isMagic_2(this int i)
   {
         return i == 2;
   }
}

Now you can do this:

  for(int i = 0; i < 1000; i++)
  {
     if(i.isMagic_1())
       break;
     if(i.isMagic_2())
       continue;
  }

hope this helps!

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