Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break out of the IF statement

Tags:

c#

.net

I have code like this:

public void Method() {     if(something)     {         // Some code         if(something2)         {             // Now I should break from ifs and go to the code outside ifs         }         return;     }     // The code I want to go if the second if is true } 

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.

like image 271
szpic Avatar asked Mar 27 '15 09:03

szpic


People also ask

How do you break out of an IF statement in a for loop?

In Python, the break statement provides you with the opportunity to exit out of a loop when an external condition is triggered. You'll put the break statement within the block of code under your loop statement, usually after a conditional if statement.

Can you use break to break out of an if statement?

breaks don't break if statements. But break s don't break out of if s. Instead, the program skipped an entire section of code and introduced a bug that interrupted 70 million phone calls over nine hours. You can't break out of if statement until the if is inside a loop.


1 Answers

To 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 } 
like image 58
cshu Avatar answered Sep 22 '22 16:09

cshu