这两个“if”有什么区别?和“if-else”声明?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
实际上,优化器可能会使它们完全相同。在这些情况下,最好的办法就是尝试一下 - 查看编译器的汇编输出,您就会确切地看到其中的区别。
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.
不同之处在于,在第二种情况下,仅检查和计算一次条件。
The difference is that in the second case the condition is checked and computed only once.
在第一个示例中,始终都会对两者进行评估。
在第二个示例中,如果第一个为真,则永远不会到达第二个。
In the first example both are evaluated, always.
In the second example if first is true, it never gets to second.
最重要的区别(在我看来)是第一种形式更难阅读并且更容易出错。
第二种形式读起来更像英语:“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.
没有明显的输出差异。但是,如果您使用梯子一,它确实会使您的代码更易于阅读
There is no visible output difference. However, it does make your code easier to read if you use the ladder one
if (x==10) //仅当 x 是数字 10 时才匹配,然后处理器跳转到下一行,即
if (x!=10) // 仅当 x 不是数字 10
时才 匹配
other if only check ,如果数字是 10 或其他任何值,则为 10。
在某种程度上,两者的结果相同,但这只是语句的问题。
因此
因此最好使用第二行来提高性能
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
So its better to use second one for performance
从可维护性的角度来看,第一个
因此,
else
是要走的路。From a maintainability point of view the first one
So,
else
is the way to go.在第一个块中,编译器将运行两个 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