If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

Introducing logical equivalence

Logic is both an essential part of computer science and of our everyday interactions, and logical expressions help us make decisions both in our programs and our lives.
Take, for example, this statement:
You cannot go to the park if your sister is awake
This is a logical expression that declares the criteria for going to the park. It's fairly simple since it only requires a single condition (your sister being awake) to be true.
To make more informed decisions and handle more possibilities, we can combine multiple logical expressions: Consider this compound statement:
You cannot go to the park if your sister is awake OR your room is dirty
That tells us when we cannot go to the park, but we might prefer to think about it in terms of when we can go to the park:
You can go to the park if your sister is NOT awake AND your room is NOT dirty
Or even simpler:
You can go to the park if your sister is asleep AND your room is clean
All of the above three statements are considered logically equivalent. If one of them is true, the others are true. If one of them is false, the other is false.
Logical equivalence is the idea that more than one expression can have the same meaning, but have a different form (often a form that helps make the meaning more clear).
Imagine that your parent is a computer scientist and wants to both test your responsibility and your understanding of logical equivalence. Such a parent might respond to your question, “Can I go to the park?”, with the following statement:
You cannot go to the park if there is NOT more than one parent home AND if your sister is NOT asleep OR NOT at least one parent is home
Even with just two conditions (the number of parents home and your sister's sleep status) and a few logical operators (NOT, AND, OR), it's become very difficult to figure out when you can go to the park. Here's a statement that's logically equivalent but much clearer:
You can go to the park if both parents are home OR if your sister is asleep AND at least one parent is home
Now we have the freedom to go to the park and understand an otherwise cryptic rule. This idea of logical equivalence is very important not just in rules for life, but in computer science where logic and rules are at the heart of creating useful tools.
Next we'll explore the many ways of making logically equivalent expressions in code, using:
  • Simple boolean expressions
  • Compound boolean expressions
  • If/else conditions
  • Everything combined!

Want to join the conversation?

  • male robot hal style avatar for user JI YONG Ahn
    Correct me if I am wrong but the two expressions exampled are wrong:
    "You cannot go to the park if there is NOT more than one parent home AND if your sister is NOT asleep OR NOT at least one parent is home" --->
    "You can go to the park if both parents are home OR if your sister is asleep AND at least one parent is home"

    Shouldn't it be:
    "You can go to the park if both parents are home AND if your sister is asleep OR at least one parent is home"?
    essentially the author changed AND and OR sign, which shouldn't have?
    (9 votes)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user PrasharJaagat1
    Hey! I had one question:

    "That tells us when we cannot go to the park, but we might prefer to think about it in terms of when we can go to the park:
    You can go to the park if your sister is NOT awake AND your room is NOT dirty"

    Shouldn't it be -- "You can go to the park if your sister is NOT awake OR your room is NOT dirty"?

    Thanks!
    (4 votes)
    Default Khan Academy avatar avatar for user
    • starky ultimate style avatar for user KLaudano
      The article is correct. If the article said "You can go to the park if your sister is NOT awake OR your room is NOT dirty", this would imply that we could go to the when when "your sister is NOT awake AND your room is dirty" or "your sister is NOT awake and your room is dirty". However, if your sister is awake or your room is dirty, you cannot go to the park. So, we can only go to the park when both your sister is NOT awake AND your room is NOT dirty.
      (1 vote)
  • starky tree style avatar for user Youjia He
    I still have trouble wrapping my head around the boolean expressions. Previous articles have said the order they are executed are exactly like in normal math with the mathematical operations (especially by adding parenthesis). Is there a better way to think about them so that I can understand the code faster?
    (2 votes)
    Default Khan Academy avatar avatar for user
    • area 52 green style avatar for user bigfatbvr
      Consider this statement:

      If you pass math, and writing or history, you can graduate.

      Pay attention to the location of the commas in this statement. They will come into play later.

      Breaking this statement apart, it shows that you must pass math and either pass writing or history to graduate. Assuming 65 is a passing grade, that statement would translate to this in pseudocode:

      IF (mathGrade > 65 AND (writingGrade > 65 OR historyGrade > 65))
      {
      DISPLAY ("graduation requirements satisfied")
      }

      The parentheses are important in determining the order of the logical operators (AND/OR). If the parentheses disappeared, then the new statement would become:

      If you pass math and writing, or history, you can graduate.

      This is different from the original statement because breaking this into parts shows that you must pass math and writing, or history to graduate. In the original, the commas come after “math” and after “history”. Now the commas come after “writing” and after “history”. See what changes in the meaning?

      In conclusion, while there may be an order of operations when it comes to logical operations, you should always use parentheses when coding to make your logic clearer. When looking at code, it always helps to break things down and simplify the problem you are looking at to understand it.
      (3 votes)
  • blobby green style avatar for user PrasharJaagat1
    I also had another question:

    What defines a statement as a logical expression? Is it simply an expression that evaluates to true or false? With that being said, would two logical expressions make up one entire logical expression?

    For example: dog == 3 AND cat == 5

    Two smaller logical expressions: dog == 3, cat == 5

    Entire thing combined -- Another logical expression?

    Thus, we could infer 3 distinct logical expressions?
    (1 vote)
    Default Khan Academy avatar avatar for user
    • aqualine ultimate style avatar for user Constellation
      A logical expression is an expression that returns a boolean value. dog==3 AND cat==5 as a whole is a logical expression connected by AND, and the parts on both sides of AND are also two logical expressions. The two together form a logical expression, so dog==3 AND cat==5 is just a logical expression.
      (1 vote)