else { if {} } and else if {}

huangapple go评论61阅读模式
英文:

else { if {} } and else if {}

问题

两段代码有什么不同吗:

else { if {} }

else if {}

例如,


if (!canWrite) {
    statusValue = "NotOwner";
}
else {
    if (syncJob.Status == "Idle") {
        statusValue = "InProgress";
    }
}


if (!canWrite) {
    statusValue = "NotOwner";
}
else if (syncJob.Status == "Idle") {
    statusValue = "InProgress";
}

上述两段代码是否以相同的方式工作?

英文:

Is there any difference between:

            else
            {
                if {}
            }

and

else if {}

For example,

            var statusValue = "Error";

            if (!canWrite)
            {
                statusValue = "NotOwner";
            }
            else
            {
                if (syncJob.Status == "Idle")
                {
                    statusValue = "InProgress";
                }
            }

and

            var statusValue = "Error";

            if (!canWrite)
            {
                statusValue = "NotOwner";
            }
            else if (syncJob.Status == "Idle")
            {
                    statusValue = "InProgress";
            }

Do the above both codes work in the same way?

答案1

得分: 1

两个你展示的代码版本执行相同的操作,但它们在语法上不同。

当你使用 else if 这个语言元素时,你可以级联这些子句,类似这样:

if (a == 1) {
  // 一些代码
} else if (b == 2) {
  // 一些其他代码
} else if (c == 3) {
  // 更多的代码
} else {
  // 仍然更多
}

在这个示例中,只有一个复合语句(由 {} 包围的代码)会被执行,这是有保证的。

当你在 else 内嵌套 if 时,你得不到完全相同的结果。如果你这样编写代码,当你需要 else if 时,会让你的代码更难阅读。这是一种让将来的自己讨厌现在的自己的好方法,因为你必须更改或调试你的代码。

英文:

The two versions of code you showed us do the same thing as each other, but they are not syntactically the same.

When you use the else if language element, you can cascade the clauses, something like this:

if (a == 1) {
  // some code
} else if (b == 2) {
  // some other code
} else if (c == 3) {
  // even more code
} else {
  // still more
}

Only one of the compound ( code surrounded by { } ) statements inside that example gets executed, guaranteed.

When you nest if within else, you don't get exactly the same thing. And if you write it that way when you want else if you'll make your code much harder to read. It's a good way to make your future self hate your present self when you must change or debug your code.

答案2

得分: 0

考虑以下代码:

if (condition1)
    DoA();
else
    DoB();

与以下代码完全等价:

if (condition1)
{
    DoA();
}
else
{
    DoB();
}

我强烈建议使用后者,因为在维护代码时,当大括号存在时,你更不容易在代码中引入错误。

所以:

if (condition1)
    DoA();
else if (condition2)
    DoB();
else
    DoC();

与带有大括号版本的代码相同。

只要你限制自己使用 单个语句,你就不需要大括号。大括号标记了语句的 ifelse 都可以同样适用于语句或块。

所有这些都表明:

if (condition1)
    DoA();
else 
{
    if (condition2)
        DoB();
}
else
    DoC();

if (condition1)
    DoA();
else 
{
    if (condition2)
    {
        DoB();
    }
}
else
    DoC();

与之前没有大括号的示例完全等效。

英文:

Consider

if (condition1)
    DoA();
else
    DoB();

Is exactly equivalent to

if (condition1)
{
    DoA();
}
else
{
    DoB();
}

I strongly recommend the latter because you are less likely to slip errors into your code doing maintenance when the brackets are there.

So...

if (condition1)
    DoA();
else if (condition2)
    DoB();
else
    DoC();

Is going to be the same as a version with brackets.

As long as you restrict yourself to single statements, you don't need brackets. The brackets demarcate blocks of statements, and both if and else can work equally well with either statements or blocks.

One thing that shakes out of all this is that

if (condition1)
    DoA();
else 
{
    if (condition2)
        DoB();
}
else
    DoC();

And

if (condition1)
    DoA();
else 
{
    if (condition2)
    {
        DoB();
    }
}
else
    DoC();

Are exactly equivalent to the previous bracket-free example

答案3

得分: -1

if (!canWrite)
{
statusValue = "NotOwner";
}
// else is ran if the above is false and the condition is true
else if (syncJob.Status == "Idle")
{
statusValue = "InProgress";
}

if (!canWrite)
{
statusValue = "NotOwner";
}
// Runs if the above is false
else
{
// Runs any code up to this point no matter the code condition below
// Runs if the first is false and this is true
if (syncJob.Status == "Idle")
{
statusValue = "InProgress";
}
// Runs any code up to this point no matter the code condition above
}

Syntax wise no, but when it is recompiled you will end up with different code... more jumps

英文:
if (!canWrite)
{
    statusValue = "NotOwner";
}
// else is ran if the above is false and the condition is true
else if (syncJob.Status == "Idle")
{
    statusValue = "InProgress";
}


if (!canWrite)
{
    statusValue = "NotOwner";
}
// Runs if the above is false
else
{
    // Runs any code up to this point no matter the code condition below
    // Runs if the first is false and this is true
    if (syncJob.Status == "Idle")
    {
         statusValue = "InProgress";
    }
    // Runs any code up to this point no matter the code condition above
}

Syntax wise no, but when it is recompiled you will end up with different code... more jumps

huangapple
  • 本文由 发表于 2023年2月14日 08:34:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定