Unit 2.2: Boolean Expressions

AP Computer Science A

Selection and Iteration

Learning Objectives

Develop code to create Boolean expressions with relational operators and determine the result of these expressions.

What are Boolean Expressions?

Definition

A Boolean expression is an expression that evaluates to either true or false.

Essential Knowledge 2.2.A.3

An expression involving relational operators evaluates to a Boolean value.

Examples of Boolean Values

boolean isRaining = true; boolean isComplete = false; boolean result = (5 > 3); // evaluates to true

Key Point: Boolean expressions are the foundation for making decisions in programming!

Equality Operators

Essential Knowledge 2.2.A.1

Values can be compared using the relational operators == and != to determine whether the values are the same.

Operator Meaning Example Result
== Equal to 5 == 5 true
!= Not equal to 5 != 3 true

Code Examples

int x = 10; int y = 10; int z = 5; boolean equal1 = (x == y); // true boolean equal2 = (x == z); // false boolean notEqual = (x != z); // true

Primitive vs Reference Types

Important Distinction!

Primitive types: == compares actual values

Reference types: == compares object references (memory addresses)

Primitive Types Example

int a = 5; int b = 5; boolean result = (a == b); // true (same values)

Reference Types Example

String str1 = new String("hello"); String str2 = new String("hello"); boolean result1 = (str1 == str2); // false (different objects) boolean result2 = str1.equals(str2); // true (same content)

Remember: For String comparison, use .equals() method for content comparison!

Numerical Comparison Operators

Essential Knowledge 2.2.A.2

Numeric values can be compared using the relational operators <, >, <=, and >= to determine the relationship between the values.

Operator Meaning Example Result
< Less than 3 < 5 true
> Greater than 8 > 3 true
<= Less than or equal 5 <= 5 true
>= Greater than or equal 7 >= 10 false

Practical Examples

Age Verification

int age = 17; boolean canVote = (age >= 18); // false boolean isMinor = (age < 18); // true

Grade Evaluation

double grade = 85.5; boolean isA = (grade >= 90); // false boolean isB = (grade >= 80); // true boolean isPassing = (grade >= 60); // true

String Comparison

String name1 = "Alice"; String name2 = "Bob"; boolean sameName = name1.equals(name2); // false boolean notSame = !name1.equals(name2); // true

Common Mistakes to Avoid

❌ Mistake 1: Using = instead of ==

// WRONG if (x = 5) { // This is assignment, not comparison!     System.out.println("x is 5"); } // CORRECT if (x == 5) { // This compares x to 5     System.out.println("x is 5"); }

❌ Mistake 2: Using == with Strings

// WRONG if (name == "Alice") { // Compares references!     System.out.println("Hello Alice"); } // CORRECT if (name.equals("Alice")) { // Compares content!     System.out.println("Hello Alice"); }

Practice Problems

Evaluate these Boolean expressions:

int a = 10; int b = 20; int c = 10; 1. (a == c) // ? 2. (a != b) // ? 3. (b > a) // ? 4. (a <= c) // ? 5. (b >= 25) // ?

Answers:

1. true      2. true      3. true      4. true      5. false

String Practice:

String word1 = "cat"; String word2 = "dog"; String word3 = new String("cat"); 1. word1.equals(word2) // ? 2. word1.equals(word3) // ? 3. (word1 == word3) // ?

Summary

Key Concepts Learned:

  • Boolean expressions evaluate to true or false
  • Equality operators: == and !=
  • Comparison operators: <, >, <=, >=
  • Difference between primitive and reference type comparisons
  • Use .equals() for String content comparison

Remember for the AP Exam:

  • Relational operators create Boolean expressions
  • == compares values for primitives, references for objects
  • Always use .equals() for String comparison
  • Boolean expressions are essential for conditional statements

Next Up: Using these Boolean expressions in if statements and loops!

1 / ?