Logical Operators Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Logical Operators.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

Operators that combine or modify boolean expressions: AND (true only when both operands are true), OR (true when at least one operand is true), and NOT (reverses a boolean value from true to false or vice versa).

AND is strict (both must be true), OR is flexible (either works), NOT flips the result.

Read the full concept explanation →

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Logical operators let you build complex conditions from simple boolean tests.

Common stuck point: AND requires ALL conditions true; OR only requires ONE — easy to confuse.

Sense of Study hint: When combining conditions with AND/OR, evaluate each condition separately first, then combine. Remember: AND narrows results (both must be true), OR broadens results (either suffices). Use parentheses to make the order of evaluation explicit.

Worked Examples

Example 1

medium
Evaluate A AND NOT B OR CA \text{ AND NOT } B \text{ OR } C for A=A=True, B=B=True, C=C=False, using standard precedence.

Answer

False\text{False}

First step

1
NOT B = NOT True = False.

See the full worked solution + why-it-works coaching

SetupKey insightWhy it worksCommon pitfallConnection

Unlock answer keys One Family plan — every worked solution, all subjects

Example 2

medium
Use De Morgan to rewrite: NOT(x>0 OR y>0)\text{NOT}(x > 0 \text{ OR } y > 0).

Example 3

medium
Identify the bug: `if (x = 0 OR 1 OR 2)` is meant to test whether x is 0, 1, or 2.

Example 4

hard
Simplify A OR (A AND B)A \text{ OR }(A \text{ AND } B) using boolean algebra.

Example 5

hard
Simplify the condition: `NOT (age < 13 OR age > 65)`.

Example 6

challenge
A condition `(NOT a) OR (NOT b)` is being optimized. Show two equivalent simpler forms.

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

easy
Evaluate: True AND False\text{True AND False}.

Example 2

easy
Evaluate: True OR False\text{True OR False}.

Example 3

easy
Evaluate: NOT True\text{NOT True}.

Example 4

easy
Evaluate: True XOR True\text{True XOR True} (XOR is true when operands differ).

Example 5

easy
Evaluate: False OR False\text{False OR False}.

Example 6

easy
Evaluate: True AND True\text{True AND True}.

Example 7

easy
Evaluate: NOT (False)\text{NOT (False)}.

Example 8

easy
In `x == 5 OR 10`, why is this a logic bug when checking if x is 5 or 10?

Example 9

medium
Evaluate NOT True AND False\text{NOT True AND False} using precedence (NOT before AND).

Example 10

medium
Evaluate True OR False AND False\text{True OR False AND False} using precedence (AND before OR).

Example 11

medium
Using De Morgan's law, rewrite NOT (A AND B)\text{NOT (A AND B)} without the outer NOT.

Example 12

medium
Using De Morgan's law, rewrite NOT (A OR B)\text{NOT (A OR B)} without the outer NOT.

Example 13

medium
A short-circuit AND `A AND B` skips evaluating B when A is false. If A is `False`, is B evaluated?

Example 14

medium
Evaluate (True AND False) OR (NOT False)(\text{True AND False}) \text{ OR } (\text{NOT False}).

Example 15

medium
For a discount, a customer needs (age over 65) OR (student AND member). If age=70, student=False, member=False, does the customer qualify?

Example 16

challenge
Simplify NOT(NOT A OR NOT B)\text{NOT(NOT A OR NOT B)} using De Morgan and double negation.

Example 17

challenge
XOR can be built from AND, OR, NOT. Verify that A XOR B=(A OR B) AND NOT(A AND B)A \text{ XOR } B = (A \text{ OR } B) \text{ AND NOT}(A \text{ AND } B) for A=True,B=TrueA=\text{True}, B=\text{True}.

Example 18

challenge
A condition `NOT (x > 0 AND x < 10)` should reject values inside (0,10). Rewrite it with De Morgan into an OR of comparisons.

Example 19

medium
Evaluate NOT (True OR False) AND True\text{NOT (True OR False) AND True} using precedence.

Example 20

medium
Access is granted if `(isAdmin OR isOwner) AND NOT isBanned`. For isAdmin=False, isOwner=True, isBanned=False, is access granted?

Example 21

easy
Evaluate: False AND True\text{False AND True}.

Example 22

easy
Evaluate: False OR True\text{False OR True}.

Example 23

easy
Evaluate: True XOR False\text{True XOR False} (XOR is true when operands differ).

Example 24

easy
How many rows does the truth table for AA AND BB have?

Example 25

easy
Which operator binds TIGHTEST (highest precedence): AND, OR, or NOT?

Example 26

easy
Evaluate: True AND True AND False\text{True AND True AND False}.

Example 27

medium
Evaluate NOT(True AND False) OR False\text{NOT(True AND False) OR False} using precedence.

Example 28

medium
Rewrite NOT(x=5)\text{NOT}(x = 5) without NOT, using a comparison.

Example 29

medium
A discount is given if (member AND age >= 18) OR (employee). Are member=False, age=25, employee=True eligible?

Example 30

medium
Short-circuit OR: in `A OR B`, B is skipped if A is True. If A=True and B is `1/0`, does the expression crash?

Example 31

medium
Evaluate (A AND B) OR (A AND NOT B)(A \text{ AND } B) \text{ OR } (A \text{ AND NOT } B) for A=A=True, B=B=False.

Example 32

medium
The expression A AND NOT AA \text{ AND NOT } A is what kind of formula?

Example 33

medium
Access if `loggedIn AND (isAdmin OR isOwner)`. For loggedIn=True, isAdmin=False, isOwner=False, is access granted?

Example 34

medium
If short-circuit AND is used and `A AND B` evaluates only A when A is False, what design rule does this enable?

Example 35

hard
Simplify A AND (A OR B)A \text{ AND }(A \text{ OR } B) using boolean algebra.

Example 36

hard
Show that A XOR B(A AND NOT B) OR (NOT A AND B)A \text{ XOR } B \equiv (A \text{ AND NOT } B) \text{ OR }(\text{NOT } A \text{ AND } B) by testing (A,B)=(T,F)(A,B) = (\text{T},\text{F}).

Example 37

hard
Three variables A,B,CA, B, C. Build a truth-table expression that is True only when EXACTLY one of them is true.

Example 38

hard
Why is `{NAND}` (NOT AND) considered functionally complete on its own?

Example 39

hard
Simplify: (A AND B) OR (A AND NOT B)(A \text{ AND } B) \text{ OR }(A \text{ AND NOT } B).

Example 40

challenge
A short-circuit AND in `expensiveCheck() AND cheapCheck()` runs the expensive one first. What is the smell, and the fix?

Example 41

challenge
Write the negation of 'every student passed' using logical operators (formally).

Background Knowledge

These ideas may be useful before you work through the harder examples.

boolean logicselection