Unit 3 Overview: Boolean Expressions and if Statements

5 min readdecember 22, 2022

Milo Chang

Milo Chang

AP Computer Science A 💻

130 resources
See Units

The Big Takeaway Of This Unit

Logic: Using Boolean Expressions and If-Else If-Else statements, we can represent combinational logic and branching decisions.

Unit Overview

Exam Weighting

  • 15-17.5% of the test
  • Roughly 6 to 7 multiple-choice questions
  • A part of FRQ #1, which tests your ability to write methods using control structures, including If—Else If—Else statements.

Enduring Understanding

This unit introduces the idea of logic, especially when it comes to making decisions in code. We are introduced to boolean statements, which evaluate to a boolean value, either true or false. Using these, we can focus on branching, one of the building blocks of algorithms (the others are sequencing, which is going line by line, and iteration, which we will learn in unit 4). To achieve branching, we will learn If—Else If—Else statements.

Building Computational Thinking

Using boolean expressions with If-Else If-Else statements allows for the introduction of branching paths and choice to your programs, especially when you want the user to make a decision or when filtering out data. Using these, there are many ways that you can get the same result with the same logic, but we will focus on writing ones that are efficient.

Main Ideas for This Unit

  • Boolean Expressions
  • If-Else Statements
  • Comparing Objects

Potential Unit 3 Topics

These potential topics come from College Board:
  • Finding Boolean values with expressions involving relational operators
  • Using conditional statements to execute different statements based on input values
  • Building on conditional statements to create multiple possible outcomes
  • Creating the same value using equivalent Boolean expressions
  • Referencing objects with aliases

Unit 3 Key Concepts

Boolean expressions tell us whether something is true or false. There are the operators that can be used to create boolean expressions:
  • == equals to (two primitive types have the same value)
  • != checks for inequality (not equal)
  • < less than
  • <= less than or equal to
  • > greater than
  • >= greater than or equal to
Conditional statements allow us to control the flow of our program by choosing to run code only if a certain condition is true.
  • By combining if statements, if-else statements, and else if statements, we can create complex programs that result in different behaviors for different conditions.
  • If a return statement is run, the computer will not run the rest of the code in the method.
  • You can use nested conditionals to check for conditions within conditions. (For example, you can place an if else statement within the body of an if statement.)
some code before the if statement
if (number % 2 == 0) {
if (number % 3 == 0) {
return "Even and divisible by 3";
} else {
return "Even";
  • Programs with conditional statements can get confusing, so make sure you use indentations and brackets properly to make your code more readable.
Boolean logic operators help us create compound boolean statements. These are the boolean logic operators:
  • ! (NOT) negates whatever is in front of it
  • && (AND) returns true if BOTH the statement directly preceding it and the statement following it are true
  • || (OR) returns true if at least one of the statements directly preceding or following it is true
There is an order of operations to these operators if there are multiple in one expression. The NOT operator has the highest precedence, followed by the AND operator and finally the OR operator.
Compound boolean statements combine several simple boolean expressions together.
  • You can use compound boolean statements to test for multiple conditions at the same time in an if statement, so they can replace nested conditionals.
some code before the if statement
if ((number % 2 == 0) && (number % 3 == 0)) {
return "Even and divisible by 3";
} else if (number % 2 == 0) {
return "Even";
  • Sometimes there are multiple ways to express the same boolean expression. The easiest way to simplify a boolean expression is to test all possible inputs to get all possible outputs. If the outputs of two boolean statements are all the same, then these two statements are equivalent. We can organize this testing using a truth table.
Here is a truth table for a || !b && !a && !b. Since there is an order of operations to the boolean operators, we can rewrite this with parentheses as a || ((!b && !a) && !b).
ab!a!b!b && !a(!b && !a) && !ba || ((!b && !a) && !b)
Note that a || ((!b && !a) && !b) is true whenever a is true or b is false, so we can rewrite a || ((!b && !a) && !b) as a || !b.
When comparing objects, you want to be careful.
  • Using the "==" operator may lead to unexpected results, since it will only return true if you're referring to the exact same object. For example, if there are two different strings that happen to have the same text, you will get false if you compare them with the "==" operators because they technically are not the same string.
  • We almost always want to use the equals method to compare objects, since this will return true if they have the same attributes, even if they aren't exactly the same object. The only time you wouldn't use the equals method is if you wanted to make sure both objects are the exact same object.
String a = "Hi";
String b = new String("Hi");
System.out.println(a == b); // Prints false
System.out.println(a.equals(b)); // Prints true
Browse Study Guides By Unit

Stay Connected

© 2023 Fiveable Inc. All rights reserved.

© 2023 Fiveable Inc. All rights reserved.