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 6745989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:16:12+00:00 2026-05-26T12:16:12+00:00

JavaScript supports a goto like syntax for breaking out of nested loops. It’s not

  • 0

JavaScript supports a goto like syntax for breaking out of nested loops. It’s not a great idea in general, but it’s considered acceptable practice. C# does not directly support the break labelName syntax…but it does support the infamous goto.

I believe the equivalent can be achieved in C#:

    int i = 0;            
    while(i <= 10)
    {
        Debug.WriteLine(i);
        i++;
        for(int j = 0; j < 3; j++)
            if (i > 5)
            {
                goto Break;//break out of all loops
            }
    }

    Break:

By the same logic of JavaScript, is nested loop scenario an acceptable usage of goto? Otherwise, the only way I am aware to achieve this functionality is by setting a bool with appropriate scope.

  • 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-05-26T12:16:12+00:00Added an answer on May 26, 2026 at 12:16 pm

    My opinion: complex code flows with nested loops are hard to reason about; branching around, whether it is with goto or break, just makes it harder. Rather than writing the goto, I would first think really hard about whether there is a way to eliminate the nested loops.

    A couple of useful techniques:

    First technique: Refactor the inner loop to a method. Have the method return whether or not to break out of the outer loop. So:

    for(outer blah blah blah)
    {
        for(inner blah blah blah)
        {
            if (whatever)
            {
                 goto leaveloop;      
            }
        }
    }
    leaveloop:    
    ...
    

    becomes

    for(outer blah blah blah)
    {
        if (Inner(blah blah blah))
            break;
    }
    
    ...
    
    bool Inner(blah blah blah)
    {
        for(inner blah blah blah)
        {
            if (whatever)
            {
                 return true;      
            }
        }
        return false;
    }
    

    Second technique: if the loops do not have side effects, use LINQ.

    // fulfill the first unfulfilled order over $100
    foreach(var customer in customers)
    {
        foreach(var order in customer.Orders)
        {
            if (!order.Filled && order.Total >= 100.00m)
            {
                 Fill(order);
                 goto leaveloop;      
            }
        }
    }
    leaveloop:    
    

    instead, write:

    var orders = from customer in customers
                 from order in customer.Orders;
                 where !order.Filled
                 where order.Total >= 100.00m
                 select order;
    var orderToFill = orders.FirstOrDefault();
    if (orderToFill != null) Fill(orderToFill);
    

    No loops, so no breaking out required.

    Alternatively, as configurator points out in a comment, you could write the code in this form:

    var orderToFill = customers
        .SelectMany(customer=>customer.Orders)
        .Where(order=>!order.Filled)
        .Where(order=>order.Total >= 100.00m)
        .FirstOrDefault();
    if (orderToFill != null) Fill(orderToFill);
    

    The moral of the story: loops emphasize control flow at the expense of business logic. Rather than trying to pile more and more complex control flow on top of each other, try refactoring the code so that the business logic is clear.

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

Sidebar

Related Questions

How do I find out if my browser supports Javascript 1.5 or not? using
I know that javascript, for example supports functions inside of functions, like so: function
I am wondering if JavaScript supports writing a function within another function, or nested
I want to test with JavaScript whether the browser supports cookies or not. The
I need a JavaScript library that supports Ajax as well as help me in
Is there a jQuery plugin or javascript library that supports a genie animation effect
According to Wikipedia, IE8 only supports Javascript 1.5. So they are saying IE8 completely
In regards to Can a PHP Proxy call Javascript functions like a click I'm
I need a text editor that supports cross platform JavaScript languages such as jQuery
Is there any way to check within JavaScript if XMLHttpRequest object supports W3C Progress

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.