Main content

## AP®︎/College Computer Science Principles

### Course: AP®︎/College Computer Science Principles > Unit 3

Lesson 6: Logical equivalence# Introducing logical equivalence

AP.CSP:

AAP‑2 (EU)

, AAP‑2.L (LO)

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?

- 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?(5 votes) - 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!(3 votes)- 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)

- 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)- 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)

- 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?(1 vote)