如何突破IF语句



我有这样的代码:

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
}

如果s,是否有可能在不使用任何转到语句或将代码的其余部分提取到其他方法的情况下转到之后的代码?


是的,我知道Else;)
但这段代码很长,应该在第一个IF为false时运行,当第一个IF是true时,第二个IF是false。所以提取一种我认为最好的方法。

回答您的问题:

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
}

您可以使用goto删除某些代码。在本例中,如果thing1为true,则绕过对things2的检查。

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

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

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

考虑do/while循环:

public void Method()
{
    bool something = true, something2 = false;
    do
    {
        if (!something)
            break;
        if (something2)
            break;
    } while (false);
}

由于硬编码的false条件,do/while循环保证只像IF块一样运行一次。当您想提前退出时,只需break

public 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.
}

相同问题

长应答

从C#7.0开始的另一种方法是使用本地函数。您可以用一个有意义的名称命名本地函数,并在声明之前直接调用它(为了清楚起见)。这是您重写的示例:

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
}
public void Method()
{
    if(something)
    {
        // Some code
        if(!something2)
        {
            return;
        }
    }
    // The code I want to go if the second if is true
}

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

一般来说:if/else序列中没有break,只需在if / if else / else子句中正确排列代码即可。

public void Method()
{
    if(something)
    {
        // Some code
        if(something2)
        {
            // The code I want to go if the second if is true
        }
        return;
    }
}

只有在!something2或使用else return:时才能使用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
}

在您的代码示例中,您只需在第二个if中的if之后运行代码(或者根据上下文设置其他人提到的标志)。使用方法调用可以提高可读性并减少嵌套。

至于实际逃离ifs,我认为有一种方法比我在这里看到的答案更符合C#标准。只需将if语句的内容提取到一个单独的方法中。这也增加了可读性。因此:

public void Method()
{
    if(something)
    {
        //some code
        if (somethingelse)
        {
            //break if
        }
        //some other code running if the above if didn't trigger
    }
}

这可以这样完成:

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
}

您也可以使用lambda

if (new Func<bool>(() =>
{
    if (something1)
    {
        // Some code that calculates "something2"
        if (something2)
        {
            // Now I should break from ifs and go to the code outside ifs
            return true;
        }
    }
    return false;
})())
{
    // The code I want to go if the second if is true
}

附言:我想我知道为什么人们会想要这个"如果所有条件都成立,就运行东西,否则就运行其他东西"。并且这些条件太复杂了,无法放入一个CCD_ 12中,或者它们相互依赖。

尝试添加一个控制变量:

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
    }
}

在更复杂的情况下可能真正有用的另一个变体:

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
  if (contREsponse == "yes")
                {
                    cont = true;
                }
                else if (contREsponse == "no")
                {
                    Console.WriteLine("Come back, when you change your mind.");                    
                    //exit environment:  
                    Environment.Exit(0);
                }

最新更新