这两个“if”有什么区别?和“if-else”声明?

发布于 2024-12-02 21:27:56 字数 296 浏览 2 评论 0原文

2 个 if 语句和 1 个 if-else 语句有什么区别?

 int x;

 cin >> x;

 if (x==10) 
   cout << "Hello";

 if (x!=10) 
   cout << "Hey";

int x;

cin >> x;

 if (x==10) 
   cout << "Hello";

 else 
   cout << "Hey";

What is the difference between 2 if statements and 1 if-else statement?

 int x;

 cin >> x;

 if (x==10) 
   cout << "Hello";

 if (x!=10) 
   cout << "Hey";

int x;

cin >> x;

 if (x==10) 
   cout << "Hello";

 else 
   cout << "Hey";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

只是偏爱你 2024-12-09 21:27:56

实际上,优化器可能会使它们完全相同。在这些情况下,最好的办法就是尝试一下 - 查看编译器的汇编输出,您就会确切地看到其中的区别。

In practice, the optimizer will probably make them exactly the same. The best thing to do in these cases is to try it - look at the assembly output of your compiler, and you'll see exactly what the difference is.

和影子一齐双人舞 2024-12-09 21:27:56

不同之处在于,在第二种情况下,仅检查和计算一次条件。

The difference is that in the second case the condition is checked and computed only once.

浅听莫相离 2024-12-09 21:27:56

在第一个示例中,始终都会对两者进行评估。

在第二个示例中,如果第一个为真,则永远不会到达第二个。

In the first example both are evaluated, always.

In the second example if first is true, it never gets to second.

2024-12-09 21:27:56

最重要的区别(在我看来)是第一种形式更难阅读并且更容易出错。

第二种形式读起来更像英语:“If x is 10 then do this, else do that”,而第一种形式本质上使两个子句无关。这很容易出错,因为如果您决定需要更改阈值 10,那么您需要在两个位置而不是仅一个位置更新它。

就执行速度而言,如果有任何差异,我会感到非常惊讶。第一种形式将进行两次评估,但这是最不存在的问题。这当然不是您应该浪费时间进行优化的事情。

The most important difference (to my mind) is that the first form is harder to read and is more error-prone.

The second form reads more like English: "If x is 10 then do this, else do that" whereas the first form essentially makes the two clauses unrelated. It's error prone because if you decide that the threshold 10 needs to change then you need to update it in two places rather than just one.

In terms of execution speed, I'd be very surprised if there is any difference at all. There will be two evaluations with the first form but that's the least of the problems. It's certainly not the sort of thing you should waste time optimising.

清晰传感 2024-12-09 21:27:56

没有明显的输出差异。但是,如果您使用梯子一,它确实会使您的代码更易于阅读

There is no visible output difference. However, it does make your code easier to read if you use the ladder one

夏雨凉 2024-12-09 21:27:56

if (x==10) //仅当 x 是数字 10 时才匹配,然后处理器跳转到下一行,即

if (x!=10) // 仅当 x 不是数字 10

时才 匹配
other if only check ,如果数字是 10 或其他任何值,则为 10。

在某种程度上,两者的结果相同,但这只是语句的问题。

因此

  • ,在第一个示例中, if 的两行都将在第二个示例中执行,
  • 其中任何一行都会被执行

因此最好使用第二行来提高性能

if (x==10) //matches only if x is number 10 , then processor jump to next line i.e.

if (x!=10) // matches only if x is not number 10

where as
other if checked only , if the number is either 10 or anything else then 10.

In a way both will result same, but its just matter of statements.

so

  • in first example, both lines of if will be executed
  • in second example either of one is executed

So its better to use second one for performance

云胡 2024-12-09 21:27:56

从可维护性的角度来看,第一个

  • 违反了 DRY 原则。
  • 更难理解和修改。不是像这里这样的简单条件,而是一个很好的长条件,您要么必须剪切并粘贴条件,然后拍打!在前面,或者尝试记住德摩根定律是如何制定的......有一天它将失败,并且反转的 if 将无法与第一个完全相反......

因此, else 是要走的路。

From a maintainability point of view the first one

  • violates the DRY principle.
  • is a lot harder to understand and modify. Not with a trivial condition, like here, but with a nice long condition you'll either have to just cut 'n paste the condition and slap a ! in front, or try to remember how De Morgan's laws were formulated... And some day that will fail, and the inverted if will fail to be the exact opposite of the first....

So, else is the way to go.

给妤﹃绝世温柔 2024-12-09 21:27:56

在第一个块中,编译器将运行两个 if 语句...
但在第二个语句中,只有 1 个语句将运行,因为两者都与单个条件链接。要么 if 为真,要么 else 为真

您可以将其理解为将第一个视为“与”类型

,将第二个视为“或”类型

In the first block both if statement will run by the compiler...
But int the second one only 1 statement will run as both are linked with a single condition . Either if can be true or else can be true

You can understand this as considering 1st one as 'and' type

And the 2nd one as 'or' type

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文