简体   繁体   English

C#相当于Java的继续<label>?

[英]C# equivalent to Java's continue <label>?

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 ;)). 编辑 :好吧,似乎没有这样的等价物(嘿 - Jon Skeet自己说没有,这就解决了;))。 So the "solution" for me (in its Java equivalent) is: 所以我的“解决方案”(在其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...
}

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 strongly recommend 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. 我不能认为有很多次没有更可读的编码方式。

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;
}

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. 这可能会突破内循环并允许外循环继续前进。

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. 第4行的continue语句是继续循环下一个值的指令。 The output here would be 1,3,5 and 7. 这里的输出是1,3,5和7。

Replacing "continue" with "break", as follows, 用“break”替换“continue”,如下所示,

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. 我希望这能为您提供一些您想要的东西,但如果没有,请随时再询问。

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*/
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM