# 提前返回或让代码完成更快吗？Is it faster to return early or to let the code finish?

``````public void myMethod()
{
if(..)
else if(..)
else if(...)
else if(...)
else if(...)

//and so on
}
``````

## 3 个回复3

### ===============>>#2 票数：12

SLaks的回答当然是正确的。 但我认为你问这个问题的事实表明你对if-else意味着什么有误解。 当你说

``````if(x) X();
else if(y) Y();
else if(z) Z();
return;
``````

``````if(x)
{
X();
}
else
{
if (y)
{
Y();
}
else
{
if (z)
{
Z();
}
}
}
return;
``````

``````if (x) X();
else while(y) Y();
``````

## 更新：

``````if (condition)
Consequence()
else
Alternative();
Continuation();
``````

``````if (!condition)
goto ALTERNATIVE;
Consequence();
goto CONTINUATION;
ALTERNATIVE:  Alternative();
CONTINUATION: Continuation();
``````

``````if (x) X();
else if (y) Y();
else if (z) Z();
return;
``````

``````if (x)
X();
else
{
if (y)
Y();
else
{
if (z)
Z();
}
}
``````

``````if (!x) goto XALTERNATIVE;
X();
goto CONTINUATION;
XALTERNATIVE: if (!y) goto YALTERNATIVE;
Y();
goto CONTINUATION;
YALTERNATIVE: if (!z) goto CONTINUATION;
Z();
CONTINUATION: return;
``````

（*）除当地声明外; C＃禁止这样做。

### ===============>>#3 票数：0

``````if(...){return;}
if(...){return;}
if(...){return;}
if(...){return;}
if(...){return;}
``````