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

C#如果内部if为true,则跳过if语句的其余部分,c#,if-statement,conditional,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; } }

我的代码如下:

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)下加什么,这样如果它为真,它将跳到“if(flagIsUp)”块的其余部分(即,它将跳过“if(z=3)”语句),并继续使用该方法(即转到“if(buttonPressed)”?我尝试了break;,但这需要一个循环。使用return;简单地结束方法(),跳过代码的其余部分,这不是我的本意。

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

}

使用
else
来适应此情况

if (y == 2) {
  code1; 
} else{
  if (z == 3) {
    code2;
  } 
  code3;
}
原始代码与我的标记清晰

if (y = 2)
  code1;

if (z = 3)
  code2;

code3; 

根据您得到的构造,没有简单的关键字可以使用。您应该将其修改为使用
else

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

首先,=赋值,==相等性测试

也就是说,大部分时间,包括这里,
else
应该足够了:

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

听起来你还想要别的

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

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

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

}

将代码移动到函数:

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

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

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

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; 
    } 
} 
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;
    }
}
此排列显示if-else-if如何在条件中“缩进”子例程。如果A为true,则它将跳过B和C的代码。如果A为false,且B为true,则跳过C。如果A和B为false,则选中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()
     }
}
这样,代码执行在第一个true条件下停止并从方法返回。如果A为true,则执行代码并返回,否则继续执行B

值得考虑的一件事是,是否可以对程序进行重构,使X、Y和Z可以在单个枚举中表示?因为
X==1
Y==2
Z==3
是相互排斥的,所以可能有一种方法可以使这组条件更具可读性,并最终得到类似的结果

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

最后,对于性能来说,将最可能的真实条件放在第一位通常是有帮助的。

x
y
z
真正独立的变量吗?这不会编译,你需要==not=(比较vs赋值)它也不会编译,因为“这里的代码”是无效代码-我认为这是伪代码。这不应该被否决。正如答案中已经指出的那样,这是一种糟糕的风格,但它是对所问问题的有效答案。你可以使用goto,但你真的不应该。我认为goto应该从.NET中删除-总有办法让代码难看,但goto是最简单的。那么新手一开始就不会被教坏的风格。只要我的2美分。答案很简单,如果,为什么要使用坏的东西作为goto?@Krumelur它有它的用途。我完全同意你不应该在这种情况下使用它,但是
goto
在那些通过避免深度嵌套的条件和un必要的额外辅助变量。