Icon for AP® Computer Science Principles

AP® Computer Science Principles

Boolean Expressions: AP® Computer Science Principles Review

boolean expressions

Boolean expressions are statements that can be evaluated as either true or false. They are at the heart of many decisions that computers make, such as whether a user is allowed to log in or if a game character has enough points to advance. As a result, these expressions become essential in programming logic and everyday tech processes.

Therefore, understanding what a Boolean expression is can greatly simplify the process of writing effective code. These expressions are built using relational operators (like “equals” or “greater than”) and logical operators (like “AND” or “OR”). In programming, the computer checks each expression and decides if the overall result is true or false, guiding the program’s next step. This knowledge is especially important when preparing for the AP® Computer Science Principles exam.

Understanding Boolean Values

Boolean values represent a simple but powerful concept in computer science. Specifically, a Boolean value can only be true or false. This may seem obvious, yet these values power most decision-making features in code.

Imagine a light switch. When flipped up, the light is on (true), and when flipped down, the light is off (false). Likewise, a Boolean expression in a program might check if a user’s password is correct. If it is correct, the result is true, and the user gains access. However, if it is incorrect, the result is false, and the user is denied access. Therefore, Boolean values act as the on/off signals for countless computer operations.

Relational Operators

Relational operators compare two values, variables, or expressions and result in a Boolean value. These operators are:

  • = (equals)
  • (not equals)
  • > (greater than)
  • < (less than)
  • (greater than or equal to)
  • (less than or equal to)

In a program, these symbols allow a computer to make decisions by checking how two pieces of data relate. For instance, consider the expression x > y. If the value of x is bigger than the value of y, the expression evaluates to true. Otherwise, it evaluates to false. Therefore, relational operators form the backbone of comparison logic in both math and programming.

Example: Evaluating a Relational Expression

Suppose there is a simple piece of code:

x  10
y  7
result  (x > y)
DISPLAY(result)

Step-by-step:

  1. x ← 10; declares a variable x and assigns it 10.
  2. y ← 7; declares a variable y and assigns it 7.
  3. result =←(x > y); checks if x is greater than y. Since 10 is greater than 7, the expression is true.
  4. DISPLAY(result) displays true on the screen.

As a result, the program concludes that x > y is indeed correct, so the expression evaluates to true.

Logical Operators

Logical operators combine or modify Boolean expressions to create more flexible conditions. The three main logical operators are:

  • NOT: Flips a Boolean value. If an expression is true, NOT changes it to false, and vice versa.
  • AND: Returns true if both expressions are true, and false otherwise.
  • OR: Returns true if at least one expression is true, and false if both are false.

These logic operators are widely used in programming for complex conditions. For example, in a soccer video game, a player might need to satisfy two conditions (like having at least 10 goals AND 5 assists) to unlock a new skill. Consequently, understanding these operators helps in building multi-part decisions.

Cecelia VetterCC BY-SA 4.0, via Wikimedia Commons

Example: Evaluating a Logical Expression

Consider this snippet:

hasLicense  true
hasInsurance  false
canDrive  (hasLicense AND hasInsurance)
DIPLAY(canDrive)

Step-by-step:

  1. hasLicense ← true; indicates the user has a driving license.
  2. hasInsurance ← false; indicates the user does not have valid insurance.
  3. canDrive ← (hasLicense AND hasInsurance); checks if both conditions are true. Because one condition (hasInsurance) is false, the entire expression is false.
  4. DISPLAY(canDrive) outputs false, showing that both conditions must be met to drive legally.

Therefore, AND requires both values to be true, which is not the case here.

Combining Relational and Logical Operators

Often, programs need more than a single comparison. Instead, they combine multiple relational operators with logical operators. This combination lets a computer evaluate more detailed conditions. For example, a student might only receive an award if their grade is over 90 AND their attendance is at least 95%.

Next, consider an expression that checks both a numerical range and a specific status. The combination might look like (age > 17) AND (age < 25) OR (studentStatus = true). In plain language, this states that a person must be between 18 and 24 years old OR be a student to receive a discount. Therefore, combining relational and logical operators provides efficient ways to handle real-world rules in code.

Example: Step-by-Step Combination

Here is a quick program demonstration:

age  20
studentStatus  false
discount  ((age >= 18) AND (age <= 24)) OR (studentStatus = true)
DISPLAY(discount)
  1. age ← 20 sets age to 20.
  2. studentStatus ← false indicates the person is not a student.
  3. discount ← ((age >= 18) AND (age <= 24)) OR (studentStatus = true) checks two conditions. First, it sees if age is between 18 and 24 (inclusive). Second, it checks whether the person is a student. Because age is between 18 and 24, the expression is true, even though studentStatus is false.
  4. DISPLAY(discount) prints true, confirming the discount applies.

Practical Applications of Boolean Expressions

Boolean expressions power many features we use daily. For example, online form validations check if a user’s age is within valid limits. In a smartphone app, Boolean logic might decide whether to send a push notification based on user preferences (e.g., user has notifications turned on AND the time is within typical waking hours).

Additionally, advanced systems like home automation rely on multiple Boolean expressions. A home’s temperature system may only heat up if the temperature drops below a certain point AND the user is home. Consequently, these simple true/false checks drive nearly every intelligent feature found in modern software systems.

Key Terms to Know

Below is a brief chart summarizing the topics covered:

  • Boolean Value: A value that can only be true or false
  • Relational Operators: Compare two values (e.g., =, , >, <, , )
  • Logical Operators: Include NOT, AND, OR; allow combining or modifying Boolean expressions
  • Expression Evaluation: Determines whether a combined statement is true or false
  • Usage Example: Decide which code path to execute, like allowing a user to log in or preventing access

Conclusion

Boolean expressions are the foundation of decision-making in programming. They enable computers to answer yes/no questions and proceed accordingly. As a result, mastering these concepts is vital for success in AP® Computer Science Principles. Therefore, it is advisable to practice writing and evaluating Boolean expressions frequently.

Furthermore, understanding how simple comparisons can expand into complex logic helps tackle more advanced problems later. Exploring additional online resources, coding tutorials, and class activities will reinforce this core skill. Over time, a solid grasp of relational and logical operators empowers students to create more efficient and robust programs.

Sharpen Your Skills for AP® Computer Science Principles

Are you preparing for the AP® Computer Science Principles test? We’ve got you covered! Try our review articles designed to help you confidently tackle real-world AP® Computer Science Principles questions. You’ll find everything you need to succeed, from quick tips to detailed strategies. Start exploring now!

Need help preparing for your AP® Computer Science Principles exam?

Albert has hundreds of AP® Computer Science Principles practice questions and full-length practice tests to try out.

Interested in a school license?​

Bring Albert to your school and empower all teachers with the world's best question bank for:

➜ SAT® & ACT®
➜ AP®
➜ ELA, Math, Science, & Social Studies
➜ State assessments

Options for teachers, schools, and districts.