Skip to content

Course navigation

For teachersTopic 2.5 · Lesson kit

Compound Boolean Expressions

  • 3 class periods
  • 45 minutes each
  • Objective 2.5.A
  • 6 downloads with keys

Downloads

At a glance

Pacing
3 class periods of 45 minutes
Learning objectives
2.5.A
Essential knowledge
2.5.A.12.5.A.2
Materials
Projector and the And, Or, Not slide deckWhiteboard and markersMini whiteboards or scrap paperIDE on every computer (optional demonstrations)Printed And, Or, Not puzzle worksheet (answer key for you) and exit ticket

45 minutes a day

Agenda

Day 145 minutes

  • Warm-up
  • Direct instruction
  • Guided practice
  • Closure
  1. 0:00–0:055 minWarm-upwho gets into the skate park (slide 2)
  2. 0:05–0:1510 minDirect instructionthe NOT operator (slides 3–6)
  3. 0:15–0:3015 minDirect instructionAND and OR (slides 7–11)
  4. 0:30–0:4212 minGuided practicetrue or false (slide 12)
  5. 0:42–0:453 minClosure"and" or "or"?

Day 245 minutes

  • Warm-up
  • Direct instruction
  • Guided practice
  • Independent practice
  • Closure
  1. 0:00–0:055 minWarm-upread it like Java
  2. 0:05–0:2217 minDirect instructionthe order of !, &&, and || (slides 13–15)
  3. 0:22–0:3412 minGuided practicewhat prints (slide 16)
  4. 0:34–0:428 minIndependent practiceworksheet questions 1 and 2 in class
  5. 0:42–0:453 minClosureadd the parentheses

Day 345 minutes

  • Warm-up
  • Direct instruction
  • Guided practice
  • Independent practice
  • Closure
  • Exit ticket
  1. 0:00–0:055 minWarm-upwhich side runs?
  2. 0:05–0:2015 minDirect instructionshort-circuit evaluation and the guard (slides 17–19)
  3. 0:20–0:3010 minGuided practicecrash or no crash (slide 20)
  4. 0:30–0:377 minIndependent practiceworksheet question 5 in pairs
  5. 0:37–0:403 minClosurevocabulary and sentence starter (slides 21–22)
  6. 0:40–0:455 minExit ticket

Overview

Day 1 is the three logical operators: ! (not) flips one boolean value (2.5.A.1), and && (and) and || (or) join two boolean expressions into one value (2.5.A.1). Day 2 is the order they run in, ! first, then &&, then ||, and how parentheses change it (2.5.A.1). Day 3 is short-circuit evaluation: Java skips the right side of && or || when the left side already decides the answer, and a guard on the left keeps a risky right side from running (2.5.A.2).

Students know the relational operators (Topic 2.2), if statements (Topic 2.3), and nested if and else if chains (Topic 2.4). Stay inside the topic boundary: no truth-table equivalence, De Morgan’s laws, or comparing objects with == (Topic 2.6), and no loops. Say that rewriting a condition into an equivalent one is the next topic.

Teacher note

The slides are the And, Or, Not deck (22 slides, with speaker notes); slide numbers below refer to it. Day 1 uses slides 1–12, Day 2 uses slides 13–16, and Day 3 uses slides 17–22. The And, Or, Not puzzle worksheet (10 questions, about 35 minutes) is split across the three nights: questions 2 and 7 after Day 1; questions 1, 4, and 8 after Day 2; questions 3, 5, 6, 9, and 10 after Day 3.

Files whose names end in .snippet.java hold only the statements that go inside main. All files are in the topic’s examples/ folder. Everything here is optional to run; the slides carry the examples.

  • Day 1 demonstrations: NotBasics, NotBattery, NotWithoutParens, AndTeen, AndDrive, OrWeekend, OrFree, OrBoth, ChainedCompare, OrShorthand, OrIntOperand, AndWhenOr.
  • Day 2 demonstrations: PrecedenceAndFirst, PrecedenceNot, PrecedenceTrace, PopcornBug, PopcornFixed, MonthBug, MonthFixed.
  • Day 3 demonstrations: GuardAnd, GuardOr, WordGuard, WordGuardSwapped, SafeGuards, QuotientCrash, NestedGuard, MixedGuard.
  • Printed only (exit ticket): ExitLogic.

Day 1 · Warm-up: Who gets into the skate park

Show slide 2. Students fill in the table for the rule “you need a helmet and you must be signed in”: Ana gets in, and Ben, Cy, and Dee do not. Then change the rule to “helmet or signed in”: Ana, Ben, and Cy get in, and Dee does not. Ask which word is stricter. Name it: this is Boolean logic, and today’s operators are Java’s versions of “and”, “or”, and “not”.

Day 1 · Direct instruction: The NOT operator

  1. Goals (slide 3). Goals 1 and 2 are Day 1, goal 3 is Day 2, and goals 4 and 5 are Day 3.
  2. ! flips a value (slide 5; 2.5.A.1). !a is true when a is false, and false when a is true. Show !muted with muted true, giving false. To flip a comparison, wrap the whole comparison in parentheses: !(volume < 20) is !(false), which is true for volume 35.
  3. ! grabs only the value next to it (slide 6). !volume < 20 is read as (!volume) < 20, and ! cannot flip an int, so it does not compile (bad operand type int for unary operator '!'). The fix is the parentheses.

Misconception: ! can go in front of a comparison without parentheses

! has higher precedence than every comparison, so it grabs only the value right next to it. Whenever students want to negate a comparison, they write !( first and close it after the comparison. Show the compiler message once (NotWithoutParens).

Day 1 · Direct instruction: AND and OR

  1. Both, or at least one (slide 8; 2.5.A.1). a && b is true only when both sides are true. a || b is true when at least one is true, including both. Walk the four rows of the table on the slide, and point out that the whole expression is one boolean value.
  2. A range and a choice (slide 9). minutes >= 30 && minutes <= 60 displays Just right for 45. day.equals("Sat") || day.equals("Sun") displays Sleep in for "Sun". A range uses &&; a choice among values uses ||.
  3. Each side must be a whole boolean (slide 10). 30 <= minutes <= 60 does not compile, because 30 <= minutes is a boolean that cannot then be compared with 60; write 30 <= minutes && minutes <= 60. day.equals("Sat") || "Sun" does not compile either, because "Sun" is not a boolean.
  4. When English “and” means || (slide 11). “Students and teachers get 10% off” means a student or a teacher; isStudent && isTeacher is true only for someone who is both. Ask students which question to ask: must one person pass both tests, or is either test enough?

Misconception: Leaving out the second comparison

Students write x > 5 || 10, month == 12 || 1, or the chained 1 <= x <= 12, the way they would say it in English or write it in math. Java never fills in the missing half. Each side of && and || must be a complete boolean expression, which means repeating the variable. This is worksheet question 6, snippets 1 and 2.

Misconception: English “and” always means &&

Sentences like “kids under 5 and seniors ride free” use “and” for two groups, which is || in Java. Nobody is both under 5 and over 64, so && lets nobody in. Teach the question from slide 11 and use it again on worksheet question 7.

Day 1 · Guided practice: True or false

Show slide 12. Students work out each value on mini whiteboards, using hasKey true, locked true, code 1234: hasKey && !locked is false; !hasKey || locked is true; code == 1234 && hasKey is true; !(code == 1234) || !hasKey is false. Ask for the value of each side before the whole expression.

Teacher note

Insist that students write the value of each side underneath the expression, as in true && false, before the answer. It builds the habit they need for precedence tomorrow.

Day 1 independent practice and closure

Puzzle worksheet questions 2 and 7 are homework: the pencil check on !, &&, and ||, and the one-token fix (change && to ||) for the weekend sale. For closure, ask for one rule from school that needs && and one that needs ||.

Day 2 · Warm-up: Read it like Java

Write a || b && c on the board with a true, b false, c false. Students decide the value and write which part they did first, then compare. Some will go left to right and get false. Do not resolve it yet; the first slide of the lesson does.

Day 2 · Direct instruction: The order of !, &amp;&amp;, and ||

  1. The order (slide 14; 2.5.A.1). Parentheses first, then !, then the comparisons, then &&, then ||. Compare it with 2 + 3 * 4: && is the “times” and || is the “plus”. a || b && c is read as a || (b && c), which is true for the warm-up values.
  2. Say it with parentheses (slide 15). member || total > 50 && thisWeek with member true, total 20.0, and thisWeek false displays true, because && goes first and a member is true no matter what. (member || total > 50) && thisWeek displays false. The two lines mean different rules; the parentheses say which one is meant.
  3. Add parentheses even where they are not needed when it makes the meaning clear.

Misconception: Java reads && and || left to right

Students apply the left-to-right rule they learned for + and * of the same precedence, but && and || have different precedence. Write a || b && c and ask where the invisible parentheses go. This is worksheet question 1 (answer A, not B) and the lie in question 8 (claim 2).

Day 2 · Guided practice: What prints

Show slide 16 with x 4. Students write the three lines and underline the operator that runs first on each. Line 1, x > 0 || x > 10 && x < 2, displays true: && goes first, giving false, and true || false is true. Line 2, !(x > 3) && x < 10, displays false: the parentheses make x > 3 go first, ! flips true to false, and false && true is false. Line 3, !(x > 3 && x < 10), displays false: the && inside the parentheses is true, and ! flips it. Contrast lines 2 and 3, which differ only in where the parentheses sit.

Day 2 independent practice and closure

Students begin worksheet questions 1 and 2 in class (about eight minutes) while you circulate. Finish questions 1, 4, and 8 tonight: the JVM trace, the code tiles for the Lily Pad Plunge (a variable must exist before it is used, and the decoy uses ||), and two truths and a lie about hasPass || weekend && !rainy. For closure, write a && b || c and ask a student to add the parentheses that show how Java reads it.

Day 3 · Warm-up: Which side runs?

Ask: for false && something, do we need to know something to know the answer? And for true || something? Take a few answers, and say that today’s lesson is about what Java does with that fact.

Day 3 · Direct instruction: Short-circuit evaluation and the guard

  1. Skipping the right side (slide 18; 2.5.A.2). Java evaluates the left side first. false && … is always false and true || … is always true, so Java skips the right side. That is short-circuit evaluation. Trace word as the empty string: word.length() > 0 is false, so word.substring(0, 1) is never evaluated, and No h at the start is displayed.
  2. The guard goes on the left (slide 19). With the two comparisons swapped, word.substring(0, 1) runs first on the empty string and throws a StringIndexOutOfBoundsException. It compiles, so the compiler cannot warn you; it crashes only when it runs. Connect it to the guard in Topic 2.4: there the guard was an outer if, here it is the left side of &&.

Misconception: Java evaluates both sides every time

Students believe the right side always runs, so they see no reason to care about the order of the two sides. Show SafeGuards: with n 0, n != 0 && 10 / n > 1 and n == 0 || 10 / n > 1 run without crashing, while QuotientCrash (division on the left) crashes. This is what worksheet question 3 (face-off) and question 5 (snippet pool) settle.

Misconception: Short-circuiting makes the guard order unimportant

Short-circuiting only skips the right side. A risky expression on the left runs before the guard is ever checked, so the guard must be the left operand of && (or, with ||, a left side that is true exactly when the right side is unsafe). Ask students every time: “Which side runs first, and can it crash?”

Day 3 · Guided practice: Crash or no crash

Show slide 20 with boxes 0 and cookies 24. Students write each line’s output or crash. A, boxes == 0 || cookies / boxes > 5, displays true: the left side is true, so the division is skipped. B, boxes != 0 && cookies / boxes > 5, displays false: the left side is false, so the division is skipped. C, cookies / boxes > 5 || boxes == 0, crashes with an ArithmeticException, because the division is on the left and runs first. Ask why A and C use the same two tests and behave differently.

Day 3 · Independent practice: Independent practice

In pairs, students do worksheet question 5, the grade-book snippet pool: count > 0 && total / count >= 80. They must say what happens when count is 0 and why. The rest of the worksheet is homework: questions 3, 6, 9, and 10 (the face-off, you’re the compiler, who am I, and the wrap-up). Question 5 settles the Think First prediction on question 4 (done the night before; students can check it then).

Day 3 · Closure: Closure and exit ticket

Show slide 21 (vocabulary), then slide 22 and have students finish the sentence starter, “I use && when __, and || when __.” Give the exit ticket (about five minutes). Question 1 checks values and the order of operators (2.5.A.1), question 2 checks short-circuit evaluation and the guard (2.5.A.2), and question 3 checks building a compound condition correctly (2.5.A.1). Re-teach the weakest one at the start of Topic 2.6.

Exit ticket

Question 1 · Tracing

Write the exact output.

int n = 6;
boolean open = false;
System.out.println(n > 5 || open && n < 3);
System.out.println((n > 5 || open) && n < 3);
System.out.println(!(n > 5) || open);
Check your reasoning
true
false
false

Line 1: && goes before ||, so Java reads n > 5 || (open && n < 3). open && n < 3 is false, but n > 5 is true, so the result is true. Line 2: the parentheses make || go first, giving true && n < 3, which is true && false, so false. Line 3: !(n > 5) is !true, which is false, and false || open is false || false, so false.

A complete answer has all three lines and shows that && runs first on line 1 and the parentheses change the order on line 2.

Question 2 · Multiple choice

int d = 0; and int total = 20;. Which expression crashes with an ArithmeticException when it is evaluated?

  1. d != 0 && total / d > 4
  2. total / d > 4 && d != 0
  3. d == 0 || total / d > 4
  4. d > 0 && total / d > 4
Check your reasoning

B. Java evaluates the left side first. In B the left side is total / d, which divides by zero before the guard d != 0 is ever checked. In A and D the left side is false, so && skips the division. In C the left side d == 0 is true, so || skips the division.

Question 3 · Short answer

Write one condition that is true when the int variable age is a teen (13 through 19, inclusive). Why does 13 <= age <= 19 not work?

Check your reasoning

Model answer: age >= 13 && age <= 19. Both tests must pass, so the two comparisons are joined with &&. 13 <= age <= 19 does not compile: 13 <= age is a boolean, and a boolean cannot be compared with 19 using <=. Each side of && must be a complete boolean expression.

A complete answer uses && (not ||, which would be true for every age), includes both boundaries with >= and <=, and repeats age in the second comparison.