📚

All Subjects

 > 

⌨️ 

AP Comp Sci P

 > 

📱

Big Idea 3: Algorithms and Programming

Boolean Expressions

2 min read•november 16, 2020

minnachow

Minna Chow


AP Computer Science Principles ⌨️

Bookmarked 2.3k • 69 resources
See Units

Boolean Variables

Boolean variables can only represent the values true or false. Relational operators are used with Boolean values to test the relationship between two values.
Here are the relational operators you'll see on the test:
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-LvcvKXcYXJCG.jpg?alt=media&token=e2cffc14-37dc-4aeb-b193-45a382d65e2e
When you compare two values using a relational operator, the computer will return a Boolean value.
Take this Python example below:
y = 5 x = 55 print (x > y)
The computer will print the word True because 55 is greater than 5.

Not, And, Or

Boolean values are also used with logical operators known as NOT, AND and OR. These can be used to combine multiple conditions, whereas relational operators can only evaluate one condition.
The NOT operator is used to reverse what the condition evaluates to. If a condition is true, the operator will evaluate to false, and vice versa.
y = 5 x = 55 print (not y >= 5)
In this case, the computer will print the word "False." y is equal to 5. This makes the condition true—it's the not operator that reverses this so the computer returns false.
Here's the NOT operator in Pseudocode:
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-kmHKnWodlmyy.png?alt=media&token=6b37d241-543f-49d2-b7d6-478b107c795d
The AND operator is used to combine two conditions. The operator will only evaluate to true if both conditions are met.
y = 5 x = 55 print (y >= 5 and x <= 44)
The computer will print the word "False" because x is not less than or equal to 44, even though y equals 5.
Here's the AND operator in Pseudocode:
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-5SPTHnKxwfTf.png?alt=media&token=66425aea-d94c-4399-81c0-291466baf03b
The OR operator also involves two conditions. In this case, the operator will evaluate to true if one condition or the other is met.
y = 5 x = 55 print (y >= 5 or x <= 44)
The computer will print the word "True" because, although the second condition isn't met, the first one is.
Here's the OR operator in Pseudocode:
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-CriyzcA4aWR4.png?alt=media&token=6016d976-14d4-4d84-ac66-57786209fafd

Image source: College Board CED

Logical operators evaluate either Boolean expressions or a single Boolean value.

Was this guide helpful?

Join us on Discord
Thousands of students are studying with us for the AP Computer Science Principles exam.
join now
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
🔍 Are you ready for college apps?
Take this quiz and find out!
Start Quiz
Browse Study Guides By Unit
🕹Big Idea 1: Creative Development
⚙️Big Idea 2: Data
📱Big Idea 3: Algorithms and Programming
🖥Big Idea 4: Computer Systems and Networks
⌨️Big Idea 5: Impact of Computing
✏️Blogs
📝Exam Prep
Join us on Discord
Thousands of students are studying with us for the AP Computer Science Principles exam.
join now
💪🏽 Are you ready for the AP CSP exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
START QUIZ