如果inner为true,则C#跳过if语句的其余部分

时间:2012-03-03 18:44:29

标签: c# if-statement conditional

我的代码如下:

public void Method()
{
    if (flagIsUp)
    {
         if (x = 1)
            code here;

         if (y = 2)
            code here;

         if (z = 3)
            code here;
    }

    if (buttonPressed)
    {
         code here;
    }

}

我在if(y = 2)下放置什么,如果它为TRUE,它将跳到“if(flagIsUp)”块的其余部分(即它将跳过“if(z = 3)” “语句”,并继续使用该方法(即转到“if(buttonPressed)”?我试过break;,但这需要一个循环。使用return;只需结束Method(),跳过剩下的代码,不是我的意图。

9 个答案:

答案 0 :(得分:1)

使用您所拥有的构造,您无法使用简单的关键字。您应该修改它以使用else

if (x = 1)
    code here;
else if (y = 2)
    code here;
else if (z = 3)
    code here;

答案 1 :(得分:1)

首先,=分配,==测试是否相等。

那就是说,大部分时间,包括else,都应该足够了:

if (y == 2)
    code here;
else if (z == 3)
    code here;

答案 2 :(得分:1)

public void Method()
{
    if (flagIsUp)
    {
         if (x = 1)
         {
            code here;
         }  
         else if (y = 2)
         {
            code here;
         }
         else if (z = 3)
         {
            code here;
         }
    }

    if (buttonPressed)
    {
         code here;
    }

}

答案 3 :(得分:0)

使用else来适应此

if (y == 2) {
  code1; 
} else{
  if (z == 3) {
    code2;
  } 
  code3;
}

原始代码与我的标记清晰

if (y = 2)
  code1;

if (z = 3)
  code2;

code3; 

答案 4 :(得分:0)

听起来像你想要别人

if ( y == 2 ) { /* do stuff */ }
else if ( z == 3) { /* do other stuff */ }

答案 5 :(得分:0)

您可以在else前面使用if,但如果您这样做,我建议您使用额外的括号。

public void Method()
{
    if (flagIsUp)
    {
         if (x == 1)
         {
            code here;
         }

         if (y == 2)
         {
            code here;
         }
         else if (z == 3)
         {
            code here;
         }
    }

    if (buttonPressed)
    {
         code here;
    }

}

答案 6 :(得分:0)

将代码移动到函数:

if (flagIsUp)
{
   doStuff(x, y, z);
}

...

void doStuff(int x, int y, int z)
{
   if (x == 1)
   {
      // do stuff
      return;
   }
   if (y == 2)
   {
      // do stuff
      return;
   }
   if (z == 3)
   {
      // do stuff
      return;
   }
 }

答案 7 :(得分:0)

以下是关于它们如何在后端运行的一些答案和解释(因为从长远来看理解为什么会对你有所帮助)

提出的一个简单答案是在每个后​​续else语句之前使用if

public void Method() 
{ 
    if (flagIsUp) 
    { 
         if (x == 1) 
            //code here; 

         else if (y == 2) 
            //code here; 

         else if (z == 3) 
            //code here; 
    } 

    if (buttonPressed) 
    { 
         //code here; 
    } 
} 

这最终像编译后的运行更像是

public void Method() 
{ 
    if (flagIsUp) 
    { 
         if (x == 1)
         {
            //code here; 
            // Excecution skips to 'x==1 fastforward'
         }
         else
         {
             if (y == 2) // intentional indentation
             {
                 //code here;
                 // Excecution skips to 'y==1 fastforward'
             } 
             else
             {
                 if (z == 3) // intentional indentation
                 {
                     //code here;
                 }
             } // y==2 fast forward
         } // x==1 fast forward
    } 

    if (buttonPressed) 
    { 
         code here; 
    } 
} 

这种安排显示if条件中if if if“indents”子程序。如果A为真,那么它将跳过B和C的代码。如果A为假,且B为真,则跳过C.如果A和B为假,则检查C.

实现此目的的另一种方法是将此检查放入其自己的私有方法中,并在满足真实条件时从中返回

public void Method() 
{ 
    if (flagIsUp)
        DoFlagUpCheck();

    if (buttonPressed) 
    { 
         //code here; 
    } 
} 

private void DoFlagUpCheck()
{
     if (x == 1) 
     {
        //code here; 
        return; // Stop and return to Method()
     }

     if (y == 2) 
     {
        //code here; 
        return; // Stop and return to Method()
     }

     if (z == 3) 
     {
        //code here; 
        return; // Stop and return to Method()
     }
}

这样,代码执行会在第一个真实条件下停止并从方法返回。如果A为真,请执行代码并返回,否则继续执行B ...

值得考虑的一件事是,程序是否可以重构,以便X,Y和Z可以在单个枚举中表示?由于x==1y==2z==3是互斥的,因此可能有一种方法可以使这组条件更具可读性并最终得到更像

的内容
public Method()
{
    if (flagIsUp)
    {
        switch(flagCondition)
        {
            case FlagX:
                //code here;
                break;
            case FlagY:
                //code here;
                break;
            case FlagZ:
                //Code here;
                break;
        }
    }

    if (buttonPressed)
    {
        //code here;
    }
}

最后,对于表现来说,首先放置最可能的真实条件往往是有帮助的。

答案 8 :(得分:-1)

你可以使用goto语句,即使它不利于代码可读性,也可以做你想做的事情......

public void Method()
{
    if (flagIsUp)
    {
         if (x = 1)
            code here;

         if (y = 2){
            code here;
            goto Outer;
         }
         if (z = 3)
            code here;
    }

    Outer:
    if (buttonPressed)
    {
         code here;
    }

}
相关问题