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"
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?
1 comment thread