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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:10:26+00:00 2026-05-14T03:10:26+00:00

How can I do this cleanly without gotos? loop: if(condition1){ something(); } else if

  • 0

How can I do this cleanly without gotos?

loop:
  if(condition1){
    something();
  } else if (condition2) {
    somethingDifferent();
  } else {
    mostOfTheWork();
    goto loop;
  }

I’d prefer not to use breaks as well. Furthermore, it is expected to loop several (adv 40) times before doing something else, so the mostOfTheWork part would most likely be as high up as possible, even if just for readability. Thanks in advance.

EDIT: This was posted under the misconception that the compiler optimizer worked poorly with breaks, which, while generally stupid to begin with, I have proven incorrect to myself
through experimentation (of performance). On the other hand, thank you for your answers; they have been interesting reads on varying styles.

  • 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-14T03:10:27+00:00Added an answer on May 14, 2026 at 3:10 am

    Clearly, you will break out of the loop if either condition fires.

        while ((!condition1) && (!condition2)) {
          MostOfTheWork();
        }
        if (condition1) {
          something();
        } else if (condition2) {
          somethingDifferent();
        }
    

    So, loop while neither condition has fired, then see which one got you.

    Now, someone will scream that I evaluated the conditions more than I needed
    to. For their benefit, a modified version:

    {
      bool t1, t2;
      while ((!(t1 = condition1)) && (!(t2 =condition2))) {
        MostOfTheWork();
      }
      if (t1) {
        something();
      } else if (t2) {
        somethingDifferent();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I write this code more cleanly/concisely? /// <summary> /// Creates a set
I don't think this can be done cleanly, but I'll ask anyway. I have
This is clearly not appears like it wouldn't be a best practice. Can someone
Can this be done by setting a property? I'd prefer that approach then to
I hope I can explain this clearly enough. I have my main form (A)
This is a very strange problem, and I just hope that I can clearly
Can this be done w/ linqtosql? SELECT City, SUM(DATEDIFF(minute,StartDate,Completed)) AS Downtime FROM Incidents GROUP
Can this be done? It seems like this should be possible. In general, I
Can this be done in JavaScript? type == 1 ? function1() : function2();
I can't seem to find any examples of this having been done anywhere on

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.