2.3 - if Statements

mg8mer

Introduction

Welcome to the FiveHive article for topic 2.3 of AP CSA! 

In this article, we will cover the learning objective of 2.3 as shown in the AP® Computer Science A Course and Exam Description 2025 

2.3.A - Develop code to represent branching logical processes by using selection statements and determine the result of these processes.

Yuck, that's a mouthful, but it’s simple don’t worry :).

Essentially, this article introduces how we actually perform selection in Java through if statements. In particular, 2.3 covers single if statements and if-else statements. Then in 2.4, we will finish covering if statements by learning about if-else-if statements and nested if-statements.

Anyways, let’s not waste anymore time and just jump straight in!

if Statements From a High Level Perspective

Before we actually get into the syntax and more “lower level” concepts of if statements, what do if statements serve to do anyway? Simply put, if statements, also known as selection statements, serve to “break” the natural sequence of execution.

Typically, when we look at how a program executes, it is quite linear, where often one statement executes, then another statement, then another statement, then another statement… and so on.

Visualizing this:

But, with a program that uses selection statements, the execution will often not be linear.

Let’s say after statement 2 we put some kind of selection statement. Depending on what its boolean expression evaluates to, other programming statements might consequently execute in addition to statements 3 and 4, or they might not even execute at all. 

Visualizing this:

With the addition of a selection statement, our program became a little more complex as it no longer has a linear execution.

Here, if our boolean expression in our selection statement evaluates to true, you will become an alpha (+9999 aura), however if it evaluates to false, you become a beta (-1000 aura).

In any case, statements 3 and 4 will then proceed to execute.

Let’s now take a closer look at if & if-else statements.

Single if Statements

This is the simplest example of a selection statement in Java. The syntax for it is as follows:

if (CONDITION)
{
	[DO SOMETHING]
}

Let’s break it down. Here, let’s assume that there are some unknown amount of execution statements before and after the above if statement. The ones before would execute linearly as normal. 

Then, we get to our if statement, where the program is essentially saying this:

“Instead of simply executing these statements (represented by [DO SOMETHING]) sequentially and without hesitation like the previous execution statements, I’ll ONLY execute these statements if (CONDITION) (which is a boolean expression) evaluates to true. Otherwise if it evaluates to false, I won’t execute these statements at all.”

In other words:

IF (condition is true), then DO SOMETHING. Otherwise, if the condition is false, do NOT DO SOMETHING. In any case, continue executing the remainder of the execution statements.

This is called one-way selection.

Let’s visualize this in a diagram and then do a quick practice problem!


if-Else Statements

This type of selection is also really simple, and it is simply an addition to our previous selection statement! Here’s the syntax:

if (CONDITION)
{
	[DO THING ONE]
}
else
{
	[DO THING TWO]
}

You know how with a single if-statement, the program would simply proceed with the execution statements after the selection statement, only if its boolean expression evaluated to false? Well here, we’re now having the program do additional execution statements if the boolean expression evaluates to false, essentially properly addressing that case instead of leaving it to be!

The program essentially is saying the following:

“Alrighty, imma check this boolean expression. If it’s true, imma DO THING ONE. Otherwise if it’s false, instead of just moving on with my (theoretical) life, I’ll execute these other statements instead. In any case, I’ll move on to executing the remainder execution statements outside of this if-else dimension.”

In other words:

IF (condition is true), then do THING 1. Otherwise, if the condition is false, do THING 2.

This is called two-way selection.

The diagram below visualizing this is basically the exact same as the one I showed in the beginning, just changed up a tad!

Practice

Alright dude you should know the drill by now. Two practice MCQs, on what you just learned, let’s knock 'em out!