Is there a possibility to go to that code after ifs without using any go to statement or extracting rest of the code to the other method? Yes, I know Else ;)
But this code is farly long and should be run IF the first IF is false and when the first IF is true and the second is false. So extracting a method I think is the best idea.
In the given code an else might be enough. Bu tin general you should structure your code so that you don't need this.
Commented Mar 27, 2015 at 9:32 Commented Mar 27, 2015 at 9:35I'm looking for clever answers to this question, other than else. You should change title to like "break out deeply nested ifs" or something. Otherwise, you'll just get unwanted downvotes by people who don't read.
Commented Mar 27, 2015 at 9:38The bit of code you've given is too simple and can simply be changed with else or !something2 etc. So your code really won't help get a good answer. I suggest to give a better example or post your actual code.
Commented Mar 27, 2015 at 9:42It sounds more as though you need to look into restructuring your logic, you haven't really explained why else return; isn't a viable option
Commented Mar 27, 2015 at 9:43To answer your question:
public void Method() < do < if (something) < // some code if (something2) < break; >return; > break; > while( false ); // The code I want to go if the second `if` is true >
153k 30 30 gold badges 287 287 silver badges 412 412 bronze badges
answered Mar 27, 2015 at 9:37
5,894 30 30 silver badges 46 46 bronze badges
yet it's the only one that truly answered OP's question.
Commented Oct 27, 2017 at 6:31
Wrong, in C# break only breaks you out of an enclosing loop or switch statement. It will not break you out of an if block.
Commented Jan 24, 2018 at 21:18@user2163234 I think you're misunderstanding his solution. He wrapped the if in an otherwise-meaningless while loop. So break breaks out of the while loop, which is functionally equivalent to breaking out of the if (since the if is the only child of the while .
Commented Aug 6, 2018 at 22:45That said, if you find yourself wanting to do something like this, please reconsider and refactor your code instead.
Commented Aug 6, 2018 at 22:46IMO better to replace 'while(true) Commented Jul 8, 2020 at 16:05
You can use a goto to drop past some code. In the example, if thing1 is true then the check for things2 is bypassed.
if (something) < do_stuff(); if (thing1) < do_thing1(); goto SkipToEnd; >if (thing2) < do_thing2(); >SkipToEnd: do_thing3(); >
3,038 1 1 gold badge 18 18 silver badges 31 31 bronze badges
answered Mar 21, 2017 at 1:51
894 1 1 gold badge 14 14 silver badges 28 28 bronze badges
The concept of "without using gotos" is nonsense or a homework problem. You use what is available to do the job, that makes your code clear to the next reader. Artificial constructs to avoid an obvious solution help no one. Gotos should ideally only be used to go forward in code, never into another method, and never for looping.
Commented Nov 7, 2017 at 18:46This is a variation of something I learned several years back. Apparently, this is popular with C++ developers.
First off, I think I know why you want to break out of IF blocks. For me, I don't like a bunch of nested blocks because 1) it makes the code look messy and 2) it can be a PITA to maintain if you have to move logic around.
Consider a do/while loop instead:
public void Method() < bool something = true, something2 = false; do < if (!something) break; if (something2) break; >while (false); >
The do/while loop is guaranteed to run only once just like an IF block thanks to the hardcoded false condition. When you want to exit early, just break .
31.6k 22 22 gold badges 109 109 silver badges 132 132 bronze badges answered May 15, 2018 at 23:44 oscilatingcretin oscilatingcretin 10.8k 39 39 gold badges 126 126 silver badges 213 213 bronze badgesSince this is still getting upvotes, I may as well mention my love/hate relationship with this method. Normally, if you have an IF statement within a loop, you can use break within the IF block to break out of the parent loop. However, if you use the technique in my answer here, the aforementioned inner IF would be replaced by a loop, so using break within that would just break you out of your "IF loop", leaving you still within the main loop.
Commented May 29, 2019 at 14:10public void Method() < if(something) < // Some code if(something2) < // Now I should break from ifs and go to the code outside ifs goto done; >return; > // The code I want to go if the second if is true done: // etc. >
31.6k 22 22 gold badges 109 109 silver badges 132 132 bronze badges
answered Sep 16, 2015 at 9:51
1,935 21 21 silver badges 35 35 bronze badges
I gave this answer a +1 because of the supporting evidence from Microsoft, specifically: "The goto statement is also useful to get out of deeply nested loops."
Commented May 4, 2018 at 12:40Another way starting from C# 7.0 would be using local functions. You could name the local function with a meaningful name, and call it directly before declaring it (for clarity). Here is your example rewritten:
public void Method() < // Some code here bool something = true, something2 = true; DoSomething(); void DoSomething() < if (something) < // Some code if (something2) < // Now I should break from ifs and go to the code outside ifs return; >return; > > // The code I want to go if the second if is true // More code here >
31.6k 22 22 gold badges 109 109 silver badges 132 132 bronze badges
answered Sep 28, 2019 at 18:09
562 5 5 silver badges 6 6 bronze badges
public void Method() < if(something) < // Some code if(!something2) < return; >> // The code I want to go if the second if is true >
31.6k 22 22 gold badges 109 109 silver badges 132 132 bronze badges
answered Mar 27, 2015 at 10:09
bunnyshell bunnyshell
253 5 5 silver badges 12 12 bronze badges
Old answer but for future viewers thinking this is a simple solution, return breaks out of Method() too, so the code OP wanted to run (as it is outside of the if statements, but inside the same method).
Commented Jan 31, 2020 at 13:22In this case, insert a single else :
public void Method() < if(something) < // Some code if(something2) < // Now I should break from ifs and go to the code outside ifs >else return; > // The code I want to go if the second if is true >
Generally: There is no break in an if/else sequence, simply arrange your code correctly in if / if else / else clauses.
31.6k 22 22 gold badges 109 109 silver badges 132 132 bronze badges answered Mar 27, 2015 at 9:33 9,752 2 2 gold badges 35 35 silver badges 44 44 bronze badgespublic void Method() < if(something) < // Some code if(something2) < // The code I want to go if the second if is true >return; > >
31.6k 22 22 gold badges 109 109 silver badges 132 132 bronze badges
answered Mar 27, 2015 at 9:33
1,086 10 10 silver badges 30 30 bronze badges
You can return only if !something2 or use else return :
public void Method() < if(something) < //some code if(something2) < //now I should break from ifs and go to the code outside ifs >if(!something2) // or else return; > // The code I want to go if the second if is true >
31.6k 22 22 gold badges 109 109 silver badges 132 132 bronze badges
answered Mar 27, 2015 at 9:34
Tim Schmelter Tim Schmelter
458k 77 77 gold badges 704 704 silver badges 963 963 bronze badges
The only reason to do that instead of simply using an else is if the body of the inner if could change something2 .
Commented Mar 27, 2015 at 9:35 @CompuChip: good point, so either use !something2 or else . Commented Mar 27, 2015 at 9:36You don't even need the first if if it returns when false. Just check if (!something2) and return . The example code is too simple and obviously not his real problem. Unless he's a complete noob.
Commented Mar 27, 2015 at 9:45In your code example, you should simply run the code after the ifs inside the second if instead (or set a flag like someone else mentioned, depending on the context). Use method calls to improve readability and reduce nesting.
As for actually escaping ifs, I think there is a way that conforms much more to C# standards than the answers I've seen here. Simply extract the contents of the if statement to a separate method. This also increases readability. So:
public void Method() < if(something) < //some code if (somethingelse) < //break if >//some other code running if the above if didn't trigger > >
This can be accomplished as such:
public void Method() < if(something) < DoSomething(); >> public void DoSomething() < //some code if (somethingelse) < return; >//some other code running if the above if didn't trigger >