C# 等价于 Java 的 continue <label>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/359436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# equivalent to Java's continue <label>?
提问by Epaga
Should be simple and quick: I want a C# equivalent to the following Java code:
应该简单快捷:我想要一个等效于以下 Java 代码的 C#:
orig: for(String a : foo) {
for (String b : bar) {
if (b.equals("buzz")) {
continue orig;
}
}
// other code comes here...
}
Edit: OK it seems there is no such equivalent (hey - Jon Skeet himself said there isn't, that settles it ;)). So the "solution" for me (in its Java equivalent) is:
编辑:好吧,似乎没有这样的等价物(嘿 - Jon Skeet 自己说没有,这就解决了;))。所以我的“解决方案”(在它的 Java 中)是:
for(String a : foo) {
bool foundBuzz = false;
for (String b : bar) {
if (b.equals("buzz")) {
foundBuzz = true;
break;
}
}
if (foundBuzz) {
continue;
}
// other code comes here...
}
采纳答案by Jon Skeet
I don't believe there's an equivalent, I'm afraid. You'll have to either use a boolean, or just "goto" the end of the inside of the outer loop. It's even messier than it sounds, as a label has to be applied to a statement - but we don't want to do anything here. However, I think this does what you want it to:
我不相信有等价物,恐怕。您必须使用布尔值,或者只是“转到”外部循环内部的末尾。它甚至比听起来更混乱,因为必须将标签应用于语句 - 但我们不想在这里做任何事情。但是,我认为这可以满足您的要求:
using System;
public class Test
{
static void Main()
{
for (int i=0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Console.WriteLine("i={0} j={1}", i, j);
if (j == i + 2)
{
goto end_of_loop;
}
}
Console.WriteLine("After inner loop");
end_of_loop: {}
}
}
}
I would stronglyrecommend a different way of expressing this, however. I can't think that there are many times where there isn't a more readable way of coding it.
然而,我强烈推荐一种不同的表达方式。我想不出很多时候没有更易读的编码方式。
回答by Joel Coehoorn
In VB.Net, you could just have one while
loop and one for
loop and then exit
the desired scope level.
在 VB.Net 中,您可以只有一个while
循环和一个for
循环,然后exit
是所需的范围级别。
In C#, maybe break;
?
在 C# 中,也许break;
?
That might break out of the inner loop and allow the outer loop to keep going.
这可能会跳出内循环并允许外循环继续运行。
回答by ZombieSheep
I think you're looking for the simple "continue" keyword... However, not being a Java guy I don't really get what the code snippet is trying to achieve.
我认为您正在寻找简单的“继续”关键字......但是,作为一个 Java 人,我并没有真正理解代码片段试图实现的目标。
Consider the following, though.
不过,请考虑以下内容。
foreach(int i in new int[] {1,2,3,5,6,7})
{
if(i % 2 == 0)
continue;
else
Console.WriteLine(i.ToString());
}
The continue statement on line 4 is an instruction to continue with the loop at the next value. The output here would be 1,3,5 and 7.
第 4 行的 continue 语句是在下一个值处继续循环的指令。这里的输出将是 1、3、5 和 7。
Replacing "continue" with "break", as follows,
将“继续”替换为“中断”,如下所示,
foreach(int i in new int[] {1,2,3,5,6,7})
{
if(i % 2 == 0)
break;
else
Console.WriteLine(i.ToString());
}
will give the output 1. Break instructs the loop to terminate, most commonly used when you want to stop processing when a condition is met.
将给出输出 1。 Break 指示循环终止,最常用于在满足条件时要停止处理的情况。
I hope this gives you some of what you were looking for, but if not, feel free to ask again.
我希望这能给你一些你正在寻找的东西,但如果没有,请随时再次询问。
回答by Sergio
You could do something like:
你可以这样做:
for(int i=0; i< foo.Length -1 ; i++) {
for (int j=0; j< bar.Length -1; j++) {
if (condition) {
break;
}
if(j != bar.Length -1)
continue;
/*The rest of the code that will not run if the previous loop doesn't go all the way*/
}
}
回答by GX Cristian
Other posibility is to make a function with the inner loop:
其他可能性是使用内部循环创建一个函数:
void mainFunc(string[] foo, string[] bar)
{
foreach (string a in foo)
if (hasBuzz(bar))
continue;
// other code comes here...
}
bool hasBuzz(string[] bar)
{
foreach (string b in bar)
if (b.equals("buzz"))
return true;
return false;
}