5

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.

erikH
  • 2,286
  • 1
  • 17
  • 19
iefpw
  • 6,816
  • 15
  • 55
  • 79

6 Answers6

29

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.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • Is it considered bad form to just put it on one line, (e.g., `out1: ;`)? – Brian Dec 05 '11 at 22:39
  • 9
    @Brian: It's considered bad form by many people to have a goto at all; a goto to an empty statement is worse, and a goto to an empty statement that puts two almost-identical punctuation characters next to each other is worse still. I would avoid this idiom if possible. – Eric Lippert Dec 05 '11 at 23:21
18

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.

Tigran
  • 61,654
  • 8
  • 86
  • 123
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    The natural next question is what to do if you are dealing with nested loops; `break` and `continue` will only work on the innermost loop. For discussion of this issue, see [How to break out of 2 loops without a flag variable in C#?](http://stackoverflow.com/questions/982595/how-to-break-out-of-2-loops-without-a-flag-variable-in-c) – Brian Dec 05 '11 at 22:44
10

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
   }
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
5

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.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
4

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);
    }
  }
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1
public void CountNumber() 
{
       for(int i = 0; i < 40; i++) {
          if(i > 20) {
              continue;
          }

          Console.WriteLine("hello " + 1);

       }
}
Tigran
  • 61,654
  • 8
  • 86
  • 123