2012-03-03 120 views
-4

我的代码如下:C#跳过if语句的其余部分,如果内,如果是真的

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

     if (y = 2) 
      code here; 

     if (z = 3) 
      code here; 
    } 

    if (buttonPressed) 
    { 
     code here; 
    } 

} 

我怎么把下如果(Y = 2),这样,如果其真正的,它会跳到其余的“if(flagIsUp)”块(即它将跳过“if(z = 3)”语句),并继续该方法(即继续“if(buttonPressed)”?但是这需要一个循环,使用return;简单地结束Method(),跳过代码的其余部分,这不是我的意图

+1

'x','y'和'z'是否真的将变量分开? – ChrisF 2012-03-03 18:46:42

+0

这不会编译,你需要==不= =(比较vs assignemnt) – 2012-03-03 18:49:59

+2

它也不会编译,因为“代码在这里”是无效的代码 - 我认为这是伪代码。 – Krumelur 2012-03-03 18:51:35

回答

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

} 
+3

这不应该downvoted。答案中已经提到了这是一种不好的风格,但它是对问题提出的有效答案。 – hvd 2012-03-03 18:49:29

+3

你可以使用goto,但你真的真的不应该。 – 2012-03-03 18:51:03

+4

我认为GOTO应该从.NET中移除 - 总有办法让代码变得难看,但GOTO是最简单的。那么新手首先不会被教导不好的风格。只是我2美分。 – Krumelur 2012-03-03 18:52:49

0

使用else到accomlpish这个

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

与我的标记原始代码为清楚起见

if (y = 2) 
    code1; 

if (z = 3) 
    code2; 

code3; 
1

有了你有结构,有没有简单的关键字,您可以使用。您应该修改它以使用else

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

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

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

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

听起来像你想的其他

if (y == 2) { /* do stuff */ } 
else if (z == 3) { /* do other stuff */ } 
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; 
    } 

} 
0

可以在前面使用elseif,但是如果你这样做,我建议你加入大括号。

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

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

这里有一些答案和explenations他们在后端如何运行的(如理解为什么会帮助你在长期运行)

一个简单的回答提出的每一个后续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; 
    } 
} 

这样的安排显示了如何如果条件里面否则,如果“缩进”子程序。如果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; 
    } 
} 

最后,它往往是有益的,对性能,放置最有可能的情况是第一。