Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Understanding "de Morgan's laws"

Post

Understanding "de Morgan's laws"

+2
−0

While trying to understand logical 'or'/'and', I encountered another problem (I'm writing Python code here, but my question is about the logic, not about any given programming language). I have some code like:

if x or y:
    pass
else:
    do_something()

(Of course, following the other Q&A, I made sure that the real x and y make sense independently as conditions.)

I wanted to focus on the else case, because it's actually interesting. I figured out that I could negate the condition to flip the cases, and then the else: pass part would be unnecessary:

if not(x or y):
    do_something()

However, other obvious attempts did not work:

if not x or y:
    do_something()

if not x or not y:
    do_something()

if (not x) or (not y): # not even with more parentheses!
    do_something()

I asked about this elsewhere and was told it had something to do with "de Morgan's laws". What does this mean? Can I use these "laws" to understand how to write the code correctly? How exactly do they apply?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Useful references (1 comment)
Useful references
Karl Knechtel‭ wrote about 1 month ago · edited about 1 month ago

Again, I'm asking so that there's a canonical available when someone encounters the problem and the question ought to be closed as a duplicate.

Related Stack Overflow questions: