📖 Crammy · All study guides
AP Computer Science A · Unit 3

Boolean Expressions: every key term you need (+ practice quiz)

54 flashcard terms for AP Computer Science A Unit 3, written to match the course framework. Read them here, drill them as flashcards, or take the 42-question quiz. Free, no account needed.

Study this unit free →

More AP Computer Science A guides

Boolean Type
true or false; result of logical expressions. Used in conditionals (if), loops. Variables: boolean isPassed = true;
Comparison Operators
== (equal), != (not equal), < (less), > (greater), <= (less/equal), >= (greater/equal). Return boolean.
Logical AND (&&)
Both conditions true → true. Otherwise false. Short-circuit: if left false, right not evaluated.
Logical OR (||)
At least one true → true. Both false → false. Short-circuit: if left true, right not evaluated.
Logical NOT (!)
Inverts boolean: !true = false, !false = true. Applied to single boolean or expression.
De Morgan's Laws
!(A && B) = !A || !B. !(A || B) = !A && !B. Useful for simplifying complex conditions.
If Statement
if (condition) { statements if true } else { statements if false }. Condition must be boolean.
Nested If Statements
If inside if. Use braces carefully for clarity. Indentation shows nesting; braces required for multiple statements.
If-Else-If Chains
if (cond1) { } else if (cond2) { } else { }. Evaluates conditions in order; executes first true block only.
Ternary Operator
(condition) ? value_if_true : value_if_false. Compact if-else. Example: max = (a > b) ? a : b;
Switch Statement
switch (value) { case 1: ...; break; default: ... }. Cleaner than many if-else for discrete values.
Switch Break Statement
break exits switch; omitting it causes fall-through to next case. Use intentionally or always break.
Unit 3 Summary
Boolean logic: && (and), || (or), ! (not). Comparisons return booleans. If-else structures decision flow. Ternary operator = compact if-else.
De Morgan's Law (AND)
!(a && b) is equivalent to !a || !b. Distribute the not and flip the operator. Useful for simplifying nested negations on the exam.
De Morgan's Law (OR)
!(a || b) is equivalent to !a && !b. Negating a disjunction produces a conjunction of negations.
Negating Comparisons
!(x < 5) is x >= 5, !(x == y) is x != y. Each relational operator has an exact opposite; do not forget the equals case.
Short-circuit Evaluation Guard
if (s != null && s.length() > 0) is safe: when s is null the right side never runs. Reversing the order would throw NullPointerException.
Dangling else
An else pairs with the nearest preceding unmatched if, regardless of indentation. Use braces to make intent explicit.
Drill these as interactive flashcards →
if / else if Chain
Only the first true branch executes; subsequent conditions are skipped. Order matters when ranges overlap (test the narrowest or highest threshold first).
Sequential ifs vs else-if
Separate if statements each get tested and several may run; an else-if ladder runs at most one branch. Choosing the wrong form causes double counting.
Boolean Identity Simplification
if (flag == true) simplifies to if (flag); if (flag == false) to if (!flag). Comparing booleans to literals is redundant.
Return Boolean Directly
Instead of if (x > 0) return true; else return false; write return x > 0;. Boolean expressions are values.
Truth Table Reasoning
To prove two boolean expressions equivalent, evaluate both for every combination of inputs (4 rows for two variables, 8 for three). Any differing row disproves equivalence.
Range Test
lo <= x && x <= hi checks membership in a closed interval. Java has no chained comparison; lo <= x <= hi does not compile.
Exclusive Or Pattern
a != b on two booleans is true when exactly one is true. Equivalent to (a || b) && !(a && b).
Equality of Object References
Using == on two objects tests identity, so two equal-content objects can fail the test. Classes define equals to compare state.
Testing for null First
Always test obj != null before calling methods on obj in a compound condition; place it leftmost so short-circuiting protects the call.
Ternary-free Subset
The AP CED does not include the ?: conditional operator; write an explicit if/else instead. You may still see it in code you read outside the exam.
Nested if Simplification
if (a) { if (b) doIt(); } equals if (a && b) doIt(); provided there is no else attached to either if.
Off-by-one in Thresholds
score >= 90 versus score > 90 differ exactly at 90. Read boundary conditions in the problem statement carefully; the exam loves boundary values.
Boolean Variables as Flags
boolean found = false; set to true when a condition is met, then test after a loop. Avoid resetting it to false inside the loop unless intended.
Operator Precedence for Booleans
! binds tightest, then relational (<, >), then equality (==, !=), then &&, then ||. a || b && c means a || (b && c).
Equivalence Testing of Conditions
Two boolean expressions are equivalent when they agree on every row of the truth table; testing a few sample values can only disprove equivalence, never confirm it.
Number of Truth Table Rows
An expression with n distinct boolean variables has 2^n rows, so three variables require eight cases for a complete check.
Absorption Law
a || (a && b) simplifies to a, and a && (a || b) simplifies to a, because the extra clause can never change the outcome.
Distribution Over Boolean Operators
a && (b || c) equals (a && b) || (a && c), which is how nested conditions get flattened into a single test.
Test yourself on this unit →
Short-circuit as Null Protection
if (s != null && s.length() > 0) is safe because the right operand is skipped whenever the guard fails; reversing the order would throw.
Short-circuit Hides Side Effects
If the right operand contains a method call that changes state, that change simply does not happen when the left operand decides the result.
else Binds to the Nearest if
Without braces an else attaches to the closest unmatched if, which is why a dangling else can silently change program meaning.
Mutually Exclusive vs Independent Tests
An if / else if chain stops after the first true branch, while consecutive separate if statements can all execute.
Ordering Overlapping Ranges
When ranges overlap, the most restrictive condition must be tested first or a broader earlier branch will capture the case.
Boundary Analysis
Most conditional bugs live at the endpoints, so a test plan should include the value just below, exactly at, and just above each threshold.
Returning a Boolean Directly
Replace if (x > 0) return true; else return false; with return x > 0; — the condition already has the required value.
Negating a Compound Condition
The negation of a >= 1 && a <= 9 is a < 1 || a > 9; both the operators and the connective flip.
Exclusive Or in the Subset
Exactly one of two conditions holds when (a && !b) || (!a && b), which is the standard way to write XOR without a dedicated operator.
Guard Clause Style
An early return for invalid input flattens deeply nested conditionals and makes the main logic the least indented code in the method.
Comparing Objects Requires equals
Using == on two String or object references compares identity, so a content test needs equals and a null check on the receiver.
Boolean Flag Accumulated in a Loop
Initialize found to false and set it to true on a match; never reset it inside the loop or later non-matching items will erase the result.
All vs Any Logic
An all-elements check starts true and is falsified by one counterexample; an any-element check starts false and is confirmed by one witness.
Unreachable Branch Detection
In an if / else if chain, a later condition implied by an earlier one can never execute, which is a common source of dead code.
Nested if Equals a Conjunction
An if inside an if with no else is exactly the same as a single if joined by &&, and flattening usually improves readability.
Testing Divisibility Correctly
Use n % k == 0 rather than n % k == 1 for a remainder test, since a negative n makes the remainder negative.
Compound Condition Efficiency
Placing the cheapest or most frequently false condition first in an && chain reduces the work done on average.
Complete Case Coverage
A conditional structure is complete when every possible input reaches exactly one branch; a trailing else guarantees no case falls through unhandled.
Turn these into flashcards & quizzes →