How to break out of IF statement

I have a code like this:

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

I want to know if it is possible to go to this code after ifs without using the goto statement or retrieving the rest of the code with another method.

Update:

Yes, I know Else;) But this code is long and should be run if the first IF is false and when the first IF is true and the second is false. so I think the extraction method is the best

+24
source share
13 answers

To answer your question:

public void Method()
{
    while(true){
        if(something)
        {
            //some code
            if(something2)
            {
                break;
            }
        return;
        }
        break;
    }
    // The code i want to go if the second if is true
}
+22
source

Use else:

if(something)
{
     //some code
     if(something2)
     {
           // now I should break from ifs and go to te code outside ifs
     }
     else return;
 }
 // The code i want to go if the second if is true
+2
source

goto, - . , thing1 true, 2 .

if (something) {
    do_stuff();
    if (thing1) { 
        do_thing1();
        goto SkipToEnd;
    }
    if (thing2) {
        do_thing2();
    }
SkipToEnd:
    do_thing3();
}
+11

, . -, C++.

, , , IF. , , 1) 2) , .

do/while while:

public void Method()
{
    bool something = true, something2 = false;

    do
    {
        if (!something) break;

        if (something2) break;

    } while (false);
}

do/while while , IF, false . , break.

+8
public void Method()
{
    if(something)
    {
    //some code
        if(something2)
        {
           // now I should break from ifs and go to te code outside ifs
           goto done;
        }
        return;
    }
    // The code i want to go if the second if is true
    done: // etc.
}

+5

else:

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

: if/else break, if / if else / else.

+2
public void Method()
{
    if(something)
    {
        //some code
        if(!something2)
        {
            return;
        }
    }
    // The code i want to go if the second if is true
}
+2
public void Method()
{
    if(something)
    {
        //some code
        if(something2)
        {
            // The code i want to go if the second if is true
        }
    return;
    }
}
+1

return, !something2 else return:

public void Method()
{
    if(something)
    {
        //some code
        if(something2)
        {
            //now I should break from ifs and go to te code outside ifs
        }
        if(!something2) // or else
            return;
    }
    // The code i want to go if the second if is true
}
+1

, , . Msgstr " , , ". , , if.

!

if (new Func<bool>(() =>
{
    if (something1)
    {
        if (something2)
        {
            return true;
        }
    }
    return false;
})())
{
    //do stuff
}
+1

:

public void Method()
{
    bool doSomethingElse = true;
    if(something)
    {
        //some code
        if(!something2)
        {
            doSomethingElse = false;
        }
    }
    if(doSomethingElse)
    {
        // The code i want to go if the second if is true
    }
}
0

Another way, starting with c # 7.0, is to use local functions. You can call a local function a meaningful name and call it just before the declaration (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 te code outside ifs
                return;
            }
            return;
        }
    }

    // The code i want to go if the second if is true
    // More code here
}
0
source

just want to add one more option to update this wonderful how-to list. However, this can be really useful in more complex cases:

try {
    if (something)
    {
        //some code
        if (something2)
        {
            throw new Exception("Weird-01.");
            // now You will go to the catch statement
        }
        if (something3)
        {
            throw new Exception("Weird-02.");
            // now You will go to the catch statement
        }
        //some code
        return;
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex); // you will get your Weird-01 or Weird-02 here
}
// The code i want to go if the second or third if is true
-1
source

All Articles