Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

c#

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")) {       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:

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... } 
like image 211
Epaga Avatar asked Dec 11 '08 14:12

Epaga


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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.

like image 197
Jon Skeet Avatar answered Sep 20 '22 07:09

Jon Skeet


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; } 
like image 25
GX Cristian Avatar answered Sep 20 '22 07:09

GX Cristian