Boolean Expressions & Operators (College Board AP® Computer Science Principles): Revision Note
Boolean values & comparisons
What is a Boolean value?
A Boolean value is a data type that can only be
trueorfalseBooleans are fundamental to decision-making in programs: conditions are evaluated to produce a Boolean result
Relational operators compare two values and return a Boolean result
Operator | Meaning | Example | Result |
|---|---|---|---|
| Equal to |
|
|
| Not equal to |
|
|
| Less than |
|
|
| Greater than |
|
|
| Less than or equal to |
|
|
| Greater than or equal to |
|
|
Boolean operands
Boolean operands are the values passed into a Boolean operation and can be:
Literal Boolean values (
trueorfalse)Variables that hold Boolean values (
isLoggedIn,isPassing)Relational expressions that evaluate to a Boolean (
score ≥ 50)
Comparison expressions evaluate to
trueorfalsebefore being used in a conditional or logic statement
Boolean logic operators
How are Boolean expressions combined?
Logic operators allow Boolean values to be combined to form more complex conditions
AP CSP pseudocode uses three logic operators:
NOT,AND, andOR
Operator | Description | Example | Result |
|---|---|---|---|
| Reverses a Boolean value |
|
|
| Returns |
|
|
| Returns |
|
|
Logic operators in practice
score ← 72
isPassing ← score ≥ 50
isHonors ← score ≥ 90
DISPLAY(isPassing AND NOT isHonors)
isPassingistrue(72 ≥ 50),isHonorsisfalse(72 < 90),NOT isHonorsistruetrue AND trueevaluates totrue, so the program displaystrue
Truth tables
A truth table shows every possible combination of Boolean inputs and the corresponding output for each logic operator
A | B | A AND B | A OR B |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
A | NOT A |
|---|---|
|
|
|
|
Examiner Tips and Tricks
ANDrequires both conditions to betrue— if either isfalse, the whole expression isfalse.ORonly returnsfalsewhen both conditions arefalse. When evaluating complex Boolean expressions, simplify one operator at a time from the inside out.For the AP Create Performance Task, Boolean expressions drive the conditionals and loops your program must include — be prepared to explain how a condition in your code determines program behavior.
Worked Example
What is the value of the following expression?
NOT (3 > 5) AND (4 = 4)
(A) true
(B) false
(C) 3
(D) 4
[1]
Answer:
(A) true [1 mark]
NOT (3 > 5)evaluates toNOT false=true, and4 = 4istrue, sotrue AND trueevaluates totrue
Unlock more, it's free!
Was this revision note helpful?