On this concept: Formula

Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Computational Thinking · Grade 6-8

Logical Operators

Logical Operators combine or modify boolean expressions: AND is true only when both operands are true, OR is true when at least one is true, and NOT reverses a boolean value.

AND: T∧T=T; OR: F∨T=T; NOT: ¬T=F

Quick Answer

Logical Operators combine or modify boolean expressions: AND is true only when both operands are true, OR is true when at least one is true, and NOT reverses a boolean value. You use them whenever a decision depends on more than one fact: a game door that opens only with a key and the switch on, a discount for students or seniors, a message shown when a user is not logged in. Each operator takes true/false values and returns one true/false answer, so several tests become a single condition for an if statement or a search filter.

Why This Matters

Most real rules have more than one part: 'age 13 or older and has parent permission', 'free shipping for members or orders over $50'. Logical operators let a program state such rules exactly. Choose the wrong one and the program still runs but quietly does the wrong thing: AND where you meant OR lets almost nobody through, and OR where you meant AND lets almost everyone in.

Intuitive Explanation

A science-fair rule says a project can enter if it has a poster and a working demo. As a condition that is hasPoster AND hasDemo. Each piece is a test that is true or false on its own, and AND joins them into one answer that is true only when both pieces are. A project with a poster but a broken demo gives true AND false, which is false. Change the rule to OR and the same project gets in, because one true piece is enough. NOT flips a single test: NOT late is true for every project handed in on time.

Numbers show how much one word matters. Take x > 0 AND x < 10. For x = 7 both parts are true, so the condition is true. For x = 12 the first part is true and the second is false, so AND gives false. Swap in OR and x = 12 passes, and so does every other number, because any number is either greater than 0 or less than 10. One word turned a range check into a condition that is always true.

When operators are mixed, NOT is applied first, then AND, then OR, so a OR b AND c means a OR (b AND c); add parentheses when you mean something else. Also watch for a condition that looks like two tests but is not: x == 5 OR 10 reads like English, yet the right side is just the number 10, which many languages treat as true. Each side of an operator must be a complete true/false test: x == 5 OR x == 10.

Think of it like this. Two light switches wired one after the other light the bulb only when both are on: that is AND. Wired side by side, either switch alone lights it: that is OR. NOT is a switch mounted upside down. The picture predicts every two-input case, but it says nothing about the order a program follows in a long mix of operators.

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


When to Use

Use logical operators when one decision depends on two or more yes/no facts, or on the opposite of one. Listen for and, or, not, either, between: 'between 1 and 9' becomes x >= 1 AND x <= 9, and 'unless it rains' becomes NOT raining. A single comparison needs no operator. If you must list the result for every possible combination of inputs, build a truth table; if the question is which block of code runs, that is selection.

Tip. Ask: Is one condition built by joining true/false tests with AND or OR, or by flipping one with NOT?

How to Recognize It

Look inside the condition: count how many true/false tests it contains and what joins them.

  1. Does the condition contain two or more separate tests, like age >= 13 and age <= 19?

    Yes means a logical operator must join them. A single test such as age >= 13 is just one Boolean value, with no operator needed.

  2. Is the question what the combined condition gives for given values, not which code runs next?

    Yes is about the operators. If it asks which branch of an IF-ELSE runs, the focus is Selection, even when the condition uses AND or OR.

  3. Do you need the result for one set of values, rather than for every possible combination?

    Yes is evaluating with operators. If you must list all combinations of true and false inputs, you need a truth table instead, the topic of Truth Tables.

  4. Would the answer change if you swapped AND for OR?

    Almost always yes: AND needs every test true, OR needs at least one. If the question is about the general laws these operators obey, such as De Morgan's laws, it is Boolean Logic.

Logical Operators vs Boolean Logic vs Selection vs Truth Tables

All four live wherever programs make yes/no decisions. Ask whether you are joining tests into one condition, reasoning about true/false rules in general, choosing which code runs, or listing every case.

Logical Operators

Meaning
Logical operators join true/false tests with AND or OR, or flip one with NOT, to make a single true/false result.
Key test
Several tests become one condition, evaluated for the values you have.
Formula
A∧B, A∨B, ¬A
Example
hasTicket AND NOT isBanned is true for a fan with a ticket who is not banned.

Boolean Logic

Meaning
Boolean logic is the system of reasoning with only two values, true and false, and the laws they follow.
Key test
The question is about general rules, like De Morgan's laws, not one condition.
Formula
¬(A∧B)≡¬A∨¬B
Example
Showing that 'not (raining and cold)' means the same as 'not raining or not cold'.

Selection

Meaning
Selection uses a condition to decide which block of code runs, as in IF-ELSE.
Key test
The question is which path the program takes, not how the condition is built.
Example
IF lives = 0 THEN show 'Game over' ELSE keep playing.

Truth Tables

Meaning
A truth table shows the output of an expression for all possible true/false inputs, one row per combination.
Key test
Every combination is listed, 2n rows for n inputs.
Formula
rows=2n
Example
A OR B needs 4 rows, and only the row with both false gives false.

Formula & Notation

AND: T∧T=T; OR: F∨T=T; NOT: ¬T=F
Logical operators on boolean values: conjunction A∧B (AND), disjunction A∨B (OR), negation ¬A (NOT). They satisfy De Morgan's laws: ¬(A∧B)=¬A∨¬B and ¬(A∨B)=¬A∧¬B.

Worked Examples

Example 1 — Evaluate a discount rule

Easy

A cinema gives a discount when age < 13 OR isStudent is true. Evaluate the condition for three customers: (a) age 10, not a student; (b) age 16, a student; (c) age 16, not a student.
Show the steps and the answer
  1. (a) 10 < 13 is true and isStudent is false: true OR false = true.

    OR needs at least one true part, and the age test supplies it.

  2. (b) 16 < 13 is false and isStudent is true: false OR true = true.

    One true part is again enough, this time from the student test.

  3. (c) 16 < 13 is false and isStudent is false: false OR false = false.

    OR is false only when every part is false, so this customer pays full price.

  4. Swap OR for AND and redo (a): true AND false = false.

    With AND, a 10-year-old who is not a student would lose the discount, which is not the rule the cinema meant.

Answer: (a) discount, (b) discount, (c) full price.

Takeaway: OR passes when any one test is true; AND demands all of them, so match the operator to the wording of the rule.

Example 2 — Same words, different grouping

Standard

A game opens a gate when hasKey OR hasMap AND isDaytime is true. The player has a key, has no map, and it is night. Does the gate open? Would it open if the condition were (hasKey OR hasMap) AND isDaytime?
Show the steps and the answer
  1. Substitute the values: hasKey = true, hasMap = false, isDaytime = false.

    Grouping questions are easiest to settle with actual values in front of you.

  2. Apply AND first: hasMap AND isDaytime = false AND false = false.

    AND has higher precedence than OR, so the code really means hasKey OR (hasMap AND isDaytime).

  3. Then OR: true OR false = true, so the gate opens.

    Under the default grouping, a key on its own is enough at any time of day.

  4. With the parentheses: (true OR false) AND false = true AND false = false, so the gate stays shut.

    Parentheses force the OR first, which makes daylight a requirement for everyone, key holders included.

Answer: As written, the gate opens (true). With (hasKey OR hasMap) AND isDaytime it stays shut (false).

Takeaway: When AND and OR appear together, AND is applied first; write parentheses whenever you want a different grouping.

Example 3 — Find the mistake: 'is 5 or 10'

Application

A quiz gives a bonus when the number of correct answers is 5 or 10. Mia writes the condition score == 5 OR 10 and tests it with score = 7. The bonus appears. Find the mistake and correct it.
Show the steps and the answer
  1. Split Mia's condition at the OR: the left side is score == 5, the right side is just 10.

    OR combines two separate values, and each side is evaluated on its own.

  2. Evaluate with score = 7: 7 == 5 is false, and 10 by itself counts as true.

    In languages such as Python and JavaScript any nonzero number counts as true, so false OR true gives true and the bonus appears.

  3. Rewrite with two complete tests: score == 5 OR score == 10.

    Now each side compares score with a value, which is what 'is 5 or 10' means in English.

  4. Test it: score = 7 gives false OR false = false; score = 10 gives false OR true = true.

    One failing case and one passing case confirm the bonus now appears only for 5 or 10.

Answer: Write score == 5 OR score == 10. Mia's version is true for every score, because 10 on its own always counts as true.

Takeaway: Each side of AND or OR must be a full true/false test; repeat the variable instead of trusting the English shortcut.

Where You'll See It

  • A video game door that needs a key AND a switch

    The door opens only when hasKey AND switchOn are both true — AND is strict, so missing either condition keeps the door shut, exactly like the boolean operator.

  • A discount that applies to students OR seniors

    isStudent OR isSenior gives the discount if EITHER is true — OR is flexible, so a student who is not a senior still qualifies, matching how the OR operator works.

  • A 'not logged in' message

    The site shows the login button when NOT loggedIn is true — the NOT operator flips loggedIn, so the button appears precisely when the user is not signed in.

Common Mistakes

  1. The slip: You can write 'x == 5 OR 10' the way you say it in English.

    Why it's tempting: It reads naturally aloud, so it feels like valid code.

    Instead: Each comparison needs its own complete test: 'x == 5 OR x == 10'. Writing 'x == 5 OR 10' compares x to 5, then treats 10 as its own (usually truthy) value, breaking the logic.

  2. The slip: AND and OR can be swapped if the conditions feel similar.

    Why it's tempting: In casual speech 'and' and 'or' are used loosely, so the distinction blurs.

    Instead: AND requires ALL operands true; OR requires AT LEAST ONE true. Swapping them reverses the meaning — 'student AND senior' (almost no one) is very different from 'student OR senior' (many people).

  3. The slip: Logical operators evaluate strictly left to right, so parentheses do not matter.

    Why it's tempting: We read text left to right, so we assume code does the same.

    Instead: Operators have precedence: NOT binds first, then AND, then OR. 'a OR b AND c' means 'a OR (b AND c)'. Use parentheses when you want a different grouping.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. Evaluate x > 0 AND x < 10 for x = 5 and for x = 11.

    Hint: Work out each comparison on its own, then combine with AND.

  2. With a = true, b = false and c = true, evaluate: (1) a AND b, (2) a OR b, (3) NOT b AND c.

    Hint: NOT is applied before AND, so find NOT b first in part 3.

  3. A ride allows a rider if (height ≥ 120 cm OR with an adult) AND NOT holding food. Is a 110 cm rider who is with an adult and holding no food allowed?

    Hint: Evaluate the bracket first, then the NOT part.

  4. A program should run when x is 5 or 10. Which condition is right: (a) x == 5 OR 10, or (b) x == 5 OR x == 10? What does (a) do when x = 3 in Python?

    Hint: Each side of OR must be a full true/false test. Is the number 10 true or false on its own?

  5. Code: IF score >= 50 THEN print 'pass' ELSE print 'try again'. Does this use a logical operator, or only selection?

    Hint: Look for AND, OR or NOT joining or flipping conditions.

  6. A student evaluates true OR false AND false as (true OR false) AND false, which is false. What is the correct value?

    Hint: Which binds tighter, AND or OR?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

Why does a computer sometimes skip the second test in an AND?

Because the answer is already settled. If the first test in A AND B is false, the whole thing is false whatever B is, so most languages never check B. This is called short-circuiting. Programmers rely on it: in 'list is not empty AND first item > 0', the second test would crash on an empty list, but it never runs when the list is empty.

In English, 'or' sometimes means one but not both. Is OR like that?

No. In programming, OR is true when either test is true and also when both are. 'You can have cake or ice cream' in English often means just one, which is a different operator called XOR, exclusive or. XOR is true only when exactly one test is true. If a rule really means 'one but not both', you need (A OR B) AND NOT (A AND B), or a built-in XOR.

How do I write 'not between 1 and 10' correctly?

Flip each part and swap the operator. 'Between' is x >= 1 AND x <= 10, so 'not between' is x < 1 OR x > 10. Writing x < 1 AND x > 10 is a common slip, and it is never true, because no number is both below 1 and above 10. This flip-and-swap rule is De Morgan's law in action.

Learning Path

Logical Operators

You are here

You need Boolean Logic (true and false as values) and Selection (if statements that branch on a condition) first. Logical operators let one condition carry several tests at once, using AND, OR, NOT and the order they are applied in. Next, Truth Tables record a compound condition's result for every input combination, and Boolean as a data type shows how programs store and pass these values.

See Also