[英]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
。
从 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.