Finkelson
Finkelson

Reputation: 3023

How to simplify if statement?

How can I simplify if statement like this

!(!a || b)?

And why it is actually possible?

UPD:

Sorry. Not optimize, but simplify :)

Upvotes: 1

Views: 1438

Answers (3)

Rahul
Rahul

Reputation: 77934

Based on De Morgan's Law your condition !(!a || b) can be simplified like (a && !b)

cause negataion mark as !

!(!a) will become a

!(||) will become &&

!(b) will become !b

Upvotes: 6

Óscar López
Óscar López

Reputation: 236170

You can simplify it a little more by applying De Morgan's laws:

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.

Upvotes: 5

Totò
Totò

Reputation: 1854

According to the De Morgan theorem it could be (a && !b). Not sure if it makes any difference as you haven't specified what optimization you are looking for.

Upvotes: 1

Related Questions