C# equivalent to Java's continue

前端 未结 5 1743
南笙
南笙 2020-12-14 06:13

Should be simple and quick: I want a C# equivalent to the following Java code:

orig: for(String a : foo) {
  for (String b : bar) {
    if (b.equals(\"buzz\"         


        
相关标签:
5条回答
  • 2020-12-14 07:03

    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.

    0 讨论(0)
  • 2020-12-14 07:06

    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.

    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.

    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.

    I hope this gives you some of what you were looking for, but if not, feel free to ask again.

    0 讨论(0)
  • 2020-12-14 07:10

    In VB.Net, you could just have one while loop and one for loop and then exit the desired scope level.

    In C#, maybe break; ?

    That might break out of the inner loop and allow the outer loop to keep going.

    0 讨论(0)
  • 2020-12-14 07:10

    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*/
      }
    }
    
    0 讨论(0)
  • 2020-12-14 07:12

    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;
    }
    
    0 讨论(0)
提交回复
热议问题