Introduction
Welcome to the FiveHive article for topic 2.5 of AP CSA!
In this article, we will cover the learning objective of 2.5 as shown in the AP® Computer Science A Course and Exam Description 2025
2.5.A - Develop code to represent compound Boolean expressions and determine the result of these expressions.
In this article, we’re gonna expand on our current idea of boolean expressions by introducing the following logical operators:
- ! (not)
- && (and)
- || (or)
You’re probably confused right now as to how these things are related to boolean expressions, but trust me, it’ll make sense in a bit.
Anyways, let’s get to it!
Logical Operators
We’ll start by covering all three logical operators that are the heart of compound boolean expressions, starting simple and slowly peeling the complexity onion.
! (not)
This one is very simple. It essentially just negates the result of a boolean expression or a boolean value.
Quick Question (QQ) I: What does this program output? Answers at the end.
System.out.println(!true);
System.out.println(!false);
|| (or)
Let A and B be two boolean expressions/values. The way you use || and && is to simply place them in between your two booleans. Then, depending on the operator and booleans, your full compound boolean expression will evaluate to true or false.
Let’s start with ||.
A || B
Boolean expressions with || evaluate to true if EITHER A or B are/evaluate to true.
QQ II: What does this program output? Answers at the end.
System.out.println(true || true);
System.out.println(true || false);
System.out.println(false || true);
System.out.println(false || false);
&& (and)
A || B
Boolean expressions with && evaluate to true if BOTH A or B are/evaluate to true.
QQ III: What does this program output? Answers at the end.
System.out.println(true && true);
System.out.println(true && false);
System.out.println(false && true);
System.out.println(false && false);
Order of Operations
Now that we have all of these logical operators, compound boolean expressions can quickly start
to get very complicated. For example:
Yuck! Thankfully, you’ll probably never have to evaluate something this disgusting on the MCQs, but it’s CollegeBoard, so you never know!
We’re gonna solve this together, don't worry. Let’s start by establishing the order of logical operators, as outlined in the CED.
- This is NOT mentioned in the CED but is important, like in PEMDAS, you first evaluate ANYTHING in brackets.
-
AND THEN you evaluate !, which has the highest precedence as outlined in the CED
- Because there is no way you're evaluating !(true || !(false && false)) without breaking down what's in the brackets first.
- Then &&.
- And lastly, ||.
With that, let’s kill this disgusting creature.
Short Circuiting Evaluation
Short-circuiting is a very simple concept that makes a computer’s (and our) lives substantially easier.
Basically, this is a process when a whole boolean expression using && or || can be evaluated by simply evaluating the first part of the expression. Here, the second boolean expression won’t be evaluated, saving some time for the computer (and you as well)!
Let’s look at this in action! Here’s our first simple boolean expression:
Normally, you would look at the whole expression to evaluate it, but the computer does it in a much more efficient way. Let’s think like our binary friend! Looking at only the first half of the expression:
The left part is true. Now, what is the operator?
It’s an or operator! As we know, for compound boolean expressions using || to evaluate to true, only one of the booleans must be true! Thus, using short-circuiting, since the left side of this boolean expression is already true, the whole expression is true and there is no need to see what the right side is!
However, if the expression looked like this:
Ruh roh raggy! We can’t use short-circuiting! The left side is false. In order for compound boolean expressions using || to evaluate false, BOTH sides must be false. Thus, to see the result of this compound boolean expression, we (and the computer) must look at the right side.
Now try it on your own!
QQ IV: Does short circuiting work on this compound boolean expression? If so, how?
QQ V: Does short circuiting work on this compound boolean expression? If so, how?
Practice
QQ Answers
QQ I: false, true
QQ II: true, true, true, false
QQ III: true, false, false, false
QQ IV: Yes. Explanations will vary.
MG8mer’s sigma explanation: Looking at the left side of the expression, it is false, and then looking at the operator, it is &&. In an && expression, both sides of the expression must be/evaluate to true in order for the whole expression to evaluate to true. Thus, we can use short-circuiting to quickly evaluate this expression to be false.
QQ V: No. Explanations will vary.
MG8mer’s sigma explanation: Looking at the left side of the expression, it is true, and then looking at the operator, it is &&. Same word salad as in the last explanation about && expressions… Because of this, we MUST look at the right side to evaluate this whole compound boolean expression and short-circuiting isn’t valid.
