# Is !(p && q) exactly the same as !p || !q?

**URL:** <https://boards.straightdope.com/t/is-p-q-exactly-the-same-as-p-q/804388>\
**Category:** Factual Questions\
**Created:** [December 13, 2017, 1:18pm UTC](https://boards.straightdope.com/t/is-p-q-exactly-the-same-as-p-q/804388 "2017-12-13T13:18:23Z")\
**Posts on this page:** 4\
**Page:** 10

<div class="post-metadata">

**Author:** ![Francis\_Vaughan](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/francis_vaughan/32/3093_2.png) [@Francis\_Vaughan](https://boards.straightdope.com/u/Francis_Vaughan)\
**Post date:** [January 22, 2018, 1:00am UTC](https://boards.straightdope.com/t/is-p-q-exactly-the-same-as-p-q/804388/181 "2018-01-22T01:00:39Z")

</div>

Argh, to add some more. You don’t actually need three states for your example, since you are only keeping a running pair of max and min, and once you have one input everything is valid. So the switch can reduce to initial, and running - and thus can be an if. But overall the idea remains a core part of how any code processing an input stream should be written.  
Any input processing code that turns into a massive set of cascading if statements is a prime candidate for revision.

---

<div class="post-metadata">

**Author:** ![Mr.Nylock](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@Mr.Nylock](https://boards.straightdope.com/u/Mr.Nylock)\
**Post date:** [January 22, 2018, 1:01am UTC](https://boards.straightdope.com/t/is-p-q-exactly-the-same-as-p-q/804388/182 "2018-01-22T01:01:50Z")

</div>

> [@Francis\_Vaughan](#):
>
> Sorry, I accidentally hit the post button before I was done, and didn’t have time in the edit window to properly write the example. So the indenting is messed up and it could do with some more cleaning up generally, but there should be enough to get the overall idea. I can hack it some more if needed to get the idea across.

Interesting, I think I get the idea, I’ll look at it again in a couple days. Is barf a highly technical term? 😛

---

<div class="post-metadata">

**Author:** ![septimus](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/septimus/32/410_2.png) [@septimus](https://boards.straightdope.com/u/septimus)\
**Post date:** [January 22, 2018, 6:19am UTC](https://boards.straightdope.com/t/is-p-q-exactly-the-same-as-p-q/804388/183 "2018-01-22T06:19:09Z")

</div>

> [@Mr.Nylock](#):
>
> I remember toggling between the two a few weeks ago and wondering if there was any practical difference for me posting. Well, now I guess I have the correct answer. 🙂

Unfortunately it’s the _reader_, not the _poster_ whose skin setting matters. Even if you revert to the old v3.7.3, you still have to use PHP instead of CODE if you want Sultan skinheads to be able to read your post.

~ ~ ~ ~  
As a general comment about coding, I like to avoid duplication where possible. For example, you invoke **readInt()** three places; I’d reduce that to two or even one(\*). Although that will result in a _shorter_ program, it may take you _longer_ to write it! But the extra effort may pay for itself if the code ends up easier to read or modify. … Or … what **Francis Vaughan** said. 🙂

- 
  - Initializing **min** and **max** may become a problem if you reduce to a single **readInt()**. I solve this by pre-initializing min = 99999999, max = -99999999, or, more correctly, min = INT\_MAX, max = INT\_MIN.

---

<div class="post-metadata">

**Author:** ![tim314](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tim314/32/3468_2.png) [@tim314](https://boards.straightdope.com/u/tim314)\
**Post date:** [January 22, 2018, 10:47am UTC](https://boards.straightdope.com/t/is-p-q-exactly-the-same-as-p-q/804388/184 "2018-01-22T10:47:23Z")

</div>

> [@Mr.Nylock](#):
>
> I get that they’re equivalent - if something is not p and also not q it is saying it is neither p nor q, like it it’s not both things at once.

I don’t think that’s a very accurate translation of what you wrote symbolically. First of all, p and q are statements that can be either true or false (i.e., Boolean expressions), not necessarily predicates applied to the same subject. In otherwords, there need not be a _thing_ which we’re suggesting is p or q. They can just as easily be unrelated statements like “bananas are yellow” and “elephants are large”.

But even putting that aside, the symbols you wrote don’t say “neither p nor q” – in other words, they don’t say both p and q are false… Rather, they describe a case where _at least one of p or q is false_.

Let me try to give you a better translation:

!p || !q = “Either p is not true, or q is not true\*”

!(p && q) = “It is not the case that both p and q are true”

See why these are equivalent? If at least one of p and q is false, then it is not the case that both are true. And, in the other direction, if it isn’t the case that both are true, then it must be the case that at least one of them is false.

- Or both – contrary to casual English where we sometimes use “or” to mean “exclusive or”.

[Previous page](https://boards.straightdope.com/t/is-p-q-exactly-the-same-as-p-q/804388.md?page=9)
