简体   繁体   English

如何简化if语句?

[英]How to simplify if statement?

How can I simplify if statement like this 我该如何简化if语句

!(!a || b) ? !(!a || b)

And why it is actually possible? 以及为什么实际上有可能?

UPD: UPD:

Sorry. 抱歉。 Not optimize, but simplify :) 不是优化,而是简化:)

Based on De Morgan's Law your condition !(!a || b) can be simplified like (a && !b) 根据De Morgan's Law您的条件!(!a || b)可以简化为(a && !b)

cause negataion mark as ! 造成negataion标记为!

!(!a) will become a !(!a)将成为

!(||) will become && !(||)将变成&&

!(b) will become !b !(b)将成为!b

You can simplify it a little more by applying De Morgan's laws : 您可以通过应用De Morgan的定律进一步简化它:

a && !b

… But I wouldn't call it "optimizing"! …但是我不会称其为“优化”! Really, it's such a tiny expression that you won't get any performance improvements by "optimizing" it (assuming that you're interested in performance improvements) - profile first and find the performance hotspots elsewhere. 确实,这是一个很小的表达式,您无法通过对其进行“优化”来获得任何性能改进(假设您对性能改进感兴趣)-首先进行概要分析,然后在其他位置查找性能热点。 Or better yet, clarify what do you understand by "optimizing" , what's your definition for it. 或者更好的是,通过“优化”来澄清您的理解,您的定义是什么。

UPDATE 更新

OK, it was simplifying the expression what you intended all along. 好的,这一直是简化表达的意图。 Then stick to the first paragraph of my answer. 然后坚持我的答案的第一段。

According to the De Morgan theorem it could be (a && !b) . 根据De Morgan定理,它可以是(a && !b) Not sure if it makes any difference as you haven't specified what optimization you are looking for. 由于尚未指定要寻找的优化,因此不确定是否会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM