C# 中的 goto 语句

发布于 2024-12-19 11:01:04 字数 738 浏览 2 评论 0原文

我正在编写一个类似于 C# 的函数:

public void CountNumber() 
{
       for(int i = 0; i < 40; i++) {
          if(i > 20) {
               goto out1;
          }

          Console.WriteLine("hello " + 1);

          out1:
             string hello = "";
       }
}

这​​基本上会计算数字,如果 i 大于 20,则不应写入 console.writeline。它应该跳过并点击“out1”,但“out1”最终需要有一个函数才能编译。它需要有“string hello =”“”才能编译。我不需要“字符串 hello =”“”。我只是想让它什么都不做并结束循环。有没有办法在没有 out1: 语句需要的“string hello =”“”的情况下做到这一点?喜欢:

public void CountNumber() 
{
       for(int i = 0; i < 40; i++) {
          if(i > 20) {
               goto out1;
          }

          Console.WriteLine("hello " + 1);

          out1:
       }
}

谢谢。

I'm writing a function like in C#:

public void CountNumber() 
{
       for(int i = 0; i < 40; i++) {
          if(i > 20) {
               goto out1;
          }

          Console.WriteLine("hello " + 1);

          out1:
             string hello = "";
       }
}

This basically counts the number and if the i is greater than 20 it should not write to console.writeline. it should step over and hit "out1" but the "out1" needs to have a function in the end to compile. It needs to have "string hello = """ to compile. I don't need the "string hello = """. I just want it to do nothing and got the end of the loop. Is there a way to do this without the "string hello = """ that the out1: statement needs? Like:

public void CountNumber() 
{
       for(int i = 0; i < 40; i++) {
          if(i > 20) {
               goto out1;
          }

          Console.WriteLine("hello " + 1);

          out1:
       }
}

Thanks.

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

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

发布评论

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

评论(6

九歌凝 2024-12-26 11:01:04

虽然说有比使用 goto 更好的方法来解决这个问题是绝对正确的,但我注意到没有人真正回答你的问题。

标签必须标记一个语句。您想去一个没有与之关联的语句的位置。您可以使用单个分号或空块来创建空语句。

    out1:
    ;
} 

或者

    out1:
    {}
}

但就像他们说的,如果可以避免的话,首先不要去那里。

Though it is absolutely correct to say that there are better ways to solve this problem than to use goto, I note that no one has actually answered your question.

A label must label a statement. You want to go to a location that has no statement associated with it. You can either make an empty statement with a single semicolon, or an empty block.

    out1:
    ;
} 

or

    out1:
    {}
}

But like they say, don't go there in the first place if you can avoid it.

浮光之海 2024-12-26 11:01:04

这个循环可以很容易地用许多其他方式编写 - 您可以只循环 while i<=20 而不是 i<40 (最好),或者移动 Console。 WriteLine 调用 if 语句,并反转 if。

但是,我假设您正在尝试在“真实”案例中处理更复杂的场景。如果是这样的话,不用使用 goto,而是使用 continue 来跳过循环的其余部分:

public void CountNumber() 
{
   for(int i = 0; i < 40; i++) {
      if(i > 20) {
         continue; // Skips the rest of this loop iteration
      }

      Console.WriteLine("hello " + 1);
   }
}

同样,您可以使用 break 来完全跳过 循环。跳出循环并且不处理更多元素(如果这更适合您的实际情况)。

This loop could easily be written many other ways - you could just loop while i<=20 instead of i<40 (best), or move the Console.WriteLine call into the if statement with the if inverted.

However, I'm assuming you're trying to work with a more elaborate scenario in your "real" case. If that's the case, instead of using goto, just use continue to skip the rest of the loop:

public void CountNumber() 
{
   for(int i = 0; i < 40; i++) {
      if(i > 20) {
         continue; // Skips the rest of this loop iteration
      }

      Console.WriteLine("hello " + 1);
   }
}

Similarly, you can use break to completely break out of the loop and not process more elements, if that's more appropriate in your real case.

心的位置 2024-12-26 11:01:04

只需反转你的条件 - if...else 也可能是一种替代方案。我假设还有其他代码,否则您只需更改 for 循环本身即可计数到 20。

   for(int i = 0; i < 40; i++) 
   {
      if(i <= 20) 
      {
          Console.WriteLine("hello " + 1);
      }
      //other code
   }

Just invert your condition - also if...else might be an alternative. I assume there is other code otherwise you can just change the for loop itself to just count up to 20.

   for(int i = 0; i < 40; i++) 
   {
      if(i <= 20) 
      {
          Console.WriteLine("hello " + 1);
      }
      //other code
   }
記憶穿過時間隧道 2024-12-26 11:01:04

还有一些其他类似 goto 的语句,您应该考虑使用:

  • continue 转到当前循环的下一次迭代。
  • break 离开当前循环
  • return 退出当前方法

如果以上都没有达到您想要的效果,您应该只考虑 goto。根据我的经验,这种情况很少见。

看起来您想在此处使用继续

There are some other goto-like statements, you should consider using:

  • continue goes to the next iteration of the current loop.
  • break leaves the current loop
  • return exits the current method

You should only consider goto if none of the above does what you want. And in my experience that's very rarely the case.

It looks like you want to use continue here.

北城挽邺 2024-12-26 11:01:04

您可以使用 continue 关键字来实现:

public void CountNumber()  {
  for(int i = 0; i < 40; i++) {
    if(i > 20) {
      continue;
    }
    Console.WriteLine("hello " + 1);
  }
}

但是,请考虑使用 if 来代替:

public void CountNumber()  {
  for(int i = 0; i < 40; i++) {
    if(i <= 20) {
      Console.WriteLine("hello " + 1);
    }
  }
}

You can use the continue keyword for that:

public void CountNumber()  {
  for(int i = 0; i < 40; i++) {
    if(i > 20) {
      continue;
    }
    Console.WriteLine("hello " + 1);
  }
}

However, consider using the if instead:

public void CountNumber()  {
  for(int i = 0; i < 40; i++) {
    if(i <= 20) {
      Console.WriteLine("hello " + 1);
    }
  }
}
别念他 2024-12-26 11:01:04
public void CountNumber() 
{
       for(int i = 0; i < 40; i++) {
          if(i > 20) {
              continue;
          }

          Console.WriteLine("hello " + 1);

       }
}
public void CountNumber() 
{
       for(int i = 0; i < 40; i++) {
          if(i > 20) {
              continue;
          }

          Console.WriteLine("hello " + 1);

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