2.1 - Algorithms with Selection and Repetition

mg8mer

Introduction

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

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

2.1.A - Represent patterns and algorithms that involve selection and repetition found in everyday life using written language or diagrams. 

This is going to be a short article serving as an introduction for the key concepts we’re going to be learning in Unit 2! As such, there won’t be much practice in this article, as you’ll be practicing these “higher-level concepts” in the other articles following this one that cover more specific aspects of Java.

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

Algorithms

Algorithms in any field are logical, step-by-step procedures to solve a particular problem. This definition of an algorithm is no different in Java, where often we use algorithms to do things such as searching through a list, sorting a list, getting the palindrome of a word, and more.

Algorithms in Java are made up of three main components, which we will learn about more specifically throughout this unit:

  • Selection
  • Repetition
  • Sequencing

Let's now unpack each of these!

Selection

The first aspect of an algorithm is selection. Selection in programming is essentially decision-making. Specifically, selection in an algorithm occurs when the algorithm decides how to continue executing based on a condition that evaluates to true or false. Depending on the implementation, whether the condition is true or false, the algorithm can continue executing in different ways.

Selection in Java is implemented through boolean expressions, which we will learn more about in 2.2, 2.5, and 2.6, and through if statements, which we will learn more about in 2.3 and 2.4.


Repetition

The second aspect of an algorithm is repetition. Repetition is simply when a step (or process) in an algorithm is repeated either a set number of times or until desired outcome happens.

In my opinion, this is the heart of most algorithms you will write in this course. It is really difficult to implement an algorithm without some element of repetition. Without it, what you’re writing is either not an algorithm, or is just extremely inefficient (hopefully it’s the first option).

Repetition in Java is implemented through two types of loops: for and while loops. We will learn more about these types of loops in 2.7, 2.8, and 2.11.

Sequencing

The last (and naturally implemented) aspect of an algorithm is sequencing. Sequencing is the most “abstract” of the three algorithm components. It just refers to the order in which the steps of the algorithms are carried out. A multi-step algorithm has sequencing. An algorithm with repetition and selection is a multi-step algorithm. Thus, if you have sequencing and repetition in your algorithm, your algorithm makes use of sequencing.

If you only have selection, there is no guarantee your code is an algorithm. One if statement/pair of if/else-if/else statements is not an algorithm. However, a chain of if statements/pair of if/else-if/else statements could be an algorithm (i.e determining a letter grade given a test score from 1-100).

Practice

Let’s practice briefly through a few questions.