如何打破IF声明

问题描述 投票:16回答:11

我有这样的代码:

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
}

我想知道是否有可能在ifs之后使用ifs而不使用任何goto语句或将其余代码提取到另一个方法。

更新:

是的我知道Else;)但是这段代码很长,如果第一个IF是假的,第一个IF是真的,第二个是假的,应该运行。所以提取方法我认为是最好的想法

c# .net
11个回答
3
投票

使用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

0
投票

尝试添加控制变量:

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
投票

只是想添加另一个变种来更新这个精彩的“如何”列表。虽然,它可能在更复杂的情况下非常有用:

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

0
投票

我想我知道为什么人们会想要这个。 “如果所有条件都是真的,运行东西,否则运行其他东西”。条件太复杂,不能放入一个if

只需使用一种方法!

private bool TheCheck()
{
    if (something1)
    {
        //calculate something2
        if (something2)
        {
            //calculate something3
            if (something3)
            {
                return true;
            }
        }
    }
    return false;
}

//and then somewhere in your code
if (TheCheck())
{
    DoStuff();
}

现在你甚至可以将TheCheck包装为内联委托,这甚至更酷

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

15
投票

回答你的问题:

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
}

8
投票

这是几年前我学到的东西的变化。显然,这在C ++开发人员中很受欢迎。

首先,我想我知道你为什么要打破IF块。对我来说,我不喜欢一堆嵌套的块,因为1)它使代码看起来很混乱2)如果你必须移动逻辑,它可以是一个pia来维护。

请考虑使用do/while循环:

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

    do
    {
        if (!something) break;

        if (something2) break;

    } while (false);
}

由于硬编码的do/while条件,false循环保证仅像IF块一样运行一次。如果你想早点退出,只需要break


7
投票

您可以使用goto删除一些代码。在示例中,事物1是真的,然后绕过事物2的检查。

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

4
投票
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.
}

same question

long answer


2
投票

在这种情况下,插入一个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
}

一般来说:break序列中没有if/else,只需在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
}

1
投票
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
}
© www.soinside.com 2019 - 2024. All rights reserved.