Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8181131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:25:08+00:00 2026-06-07T00:25:08+00:00

Consider the following C#: // C# .net switch(x) { case 1: for(int i =

  • 0

Consider the following C#:

// C# .net
switch(x)
   {
    case 1:
        for(int i = 0; i < 10; i++)
        {
            int val = getValue(i);
            if (val == 0)
                goto endswitch;
        }
        doMoreStuff();
        break;
    case 2:
        doSomeThingElse();
        break;
    default: throw new ArgumentOutOfRangeException();
}
endswitch: ;

I’ve written code similar to the above code sample. The problem is that I need to break the switch statement from inside the inner for loop. If I put a break statement there, it will only break the inner for loop and then proceed to doMoreStuff(), which is not what I need.

The alternative that seems to work best here is a goto statement, but I know this is frowned upon.

Another alternative is to keep track of a separate variable inside the for loop, but this adds lines of code and is less elegent.

What is the best way to do this?

Update: I have read that there is a way to do this in JavaScript. It works like this: (http://www.devguru.com/technologies/ecmascript/quickref/break.html)

// JavaScript
outer_loop:
for(i=0; i<3; i++)
{
   document.write("<BR>" + "outer " + i + ":   ");
   for(j=0; j<5; j++)
   {
      document.write("inner " + j + " ");
      if(j==x)
         break outer_loop;
   }
}

Is something like this possible in C#?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:25:10+00:00Added an answer on June 7, 2026 at 12:25 am

    You can abstract the check to a method with a return flag:

    switch(x)
    {
        case 1:
            if (ShouldDoMoreStuff())
                doMoreStuff();
            break;
        case 2:
            doSomeThingElse();
            break;
        default: throw new ArgumentOutOfRangeException();
    }
    
    
    private bool ShouldDoMoreStuff()
    {
        for(int i = 0; i < 10; i++)
        {
            var val = getValue(i);
            if (val == 0)
                return false;
        }
    
        return true;
    }
    

    Just to expand, you can use the goto as you had, but it’s generally frowned upon, especially in such a trivial case. Sometimes it’s useful when you have many, many nested loops or switches but that’s usually a sign that maybe you should refactor/redesign a bit. As you pointed out, you can store a local variable and do a check but that’s a bit obtuse/smelly as you realized. I prefer this method I posted above as it becomes pretty readable to work with.

    EDIT: Regarding your comment and edited question, I do not believe a analogous language feature to your JavaScript code exists in C#. From http://msdn.microsoft.com/en-us/library/37zc9d2w%28VS.80%29.aspx (emphasis added):

    Within nested statements, the break statement terminates only the do,
    for, switch, or while statement that immediately encloses it. You can
    use a return or goto statement to transfer control from within more
    deeply nested structures.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following case: A web server is running a .NET app with <sessionState
Consider the following .Net ASMX web service with two web methods. using System; using
Please consider the following three .NET types: I have an interface, an abstract class,
Consider the following situation: WidgetCompany produced a .NET DLL in 2006 called Widget.dll, version
consider the following: http://jsfiddle.net/MgZ7n/10/ I want the entire hd to be highlighted in blue.
Please consider the following: http://jsfiddle.net/CASM6/9/ I have two rows of tabs. there are two
Consider the following code (written with Visual Studio 2010 and .NET 4.0) using System;
I have a question about .net generics. Consider the following code: public abstract class
Ok this looks like a major fundamental bug in .NET: Consider the following simple
Consider the following code ( http://jsfiddle.net/FW36F/1/ ): <input type=checkbox onchange=alert(this.checked)> <button onclick=document.getElementsByTagName('input')[0].checked=!document.getElementsByTagName('input')[0].checked;>toggle</button> If you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.