簡體   English   中英

如何跳出 IF 語句

[英]How to break out of the IF statement

我有這樣的代碼:

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
}

我想知道在不使用任何 goto 語句或將其余代碼提取到其他方法的情況下,是否有可能在 ifs 之后轉到該代碼。

更新:

是的,我知道其他 ;) 但是這段代碼很長,如果第一個 IF 為假,並且第一個 IF 為真,第二個為假,則應該運行。 所以提取方法我認為是最好的主意

回答你的問題:

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 為真,則繞過對things2 的檢查。

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

這是我幾年前學到的東西的變體。 顯然,這在 C++ 開發人員中很受歡迎。

首先,我想我知道你為什么要突破 IF 塊。 對我來說,我不喜歡一堆嵌套的塊,因為 1) 它使代碼看起來很混亂 2) 如果您必須移動邏輯,它可能是一個需要維護的 pia。

考慮使用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 te 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 te 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 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子句中正確排列代碼即可。

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

您可以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
}

我想我知道為什么人們會想要這個。 “如果所有條件都為真,則運行東西,否則運行其他東西”。 而且條件太復雜,無法將if

只需使用 lambda!

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

在您的代碼示例中,您應該簡單地在第二個 if 內的 ifs 之后運行代碼(或設置一個像其他人提到的標志,具體取決於上下文)。 使用方法調用來提高可讀性並減少嵌套。

至於實際轉義 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
}

嘗試添加一個控制變量:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM