Once you understand basic selection and iteration, AP Computer Science A asks you to apply loops to solve algorithmic problems. Topics 2.9 and 2.10 focus on implementing selection and iteration algorithms and processing strings using loops.
In this interactive practice set, you will trace algorithms involving accumulator variables, string processing, character traversal, pattern matching, and digit extraction.
Key Concepts to Remember Before You Start:
-
Accumulator Patterns:
Initialize a variable such as
sumto0or a product accumulator to1, then update it during each loop iteration. -
String Processing:
Traverse strings using methods such as
substring(),indexOf(), andlength(). - Reversing Strings: Build a new string by traversing the original string from the last index toward the first.
- Pattern Matching: Use loops and String methods to locate characters, substrings, or repeated patterns.
-
Digit Extraction:
Use
% 10to isolate the rightmost digit and integer division by10to remove it.
Test your knowledge with the interactive quiz below. Review the explanations for any questions you miss to strengthen your algorithm-tracing skills.
AP CSA Topics 2.9–2.10: Loop & String Algorithms
String rev = “”;
for (int i = str.length() – 1; i >= 0; i–) {
rev += str.substring(i, i + 1);
}
System.out.println(rev);
str.length() is 4, so i starts at index 3 (‘a’).
The loop then visits indexes 2, 1, and 0. Concatenating those characters
produces the reversed string "avaj".
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.substring(i, i + 1).equals(“o”)) {
count++;
}
}
System.out.println(count);
The loop examines every character in "code.org".
The letter 'o' appears twice, so count
becomes 2.
int maxDigit = 0;
while (n > 0) {
int d = n % 10;
if (d > maxDigit) {
maxDigit = d;
}
n /= 10;
}
System.out.println(maxDigit);
The algorithm extracts digits from right to left using % 10
and removes each digit using integer division by 10.
The digits are 5, 2, 8, and 4. The largest digit is 8.
String result = “”;
for (int i = 1; i < word.length(); i += 2) {
result += word.substring(i, i + 1);
}
System.out.println(result);
The loop starts at index 1 and increases by 2. It therefore visits
indexes 1, 3, 5, and 7, producing o, p,
t, and r. The result is "optr".
for (int i = 1; i <= 5; i++) {
sum += (i * 10);
}
System.out.println(sum);
The loop runs five times. The values added are
10 + 20 + 30 + 40 + 50, giving 150.
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
System.out.println(sum);
The algorithm extracts the digits 4, 3,
2, and 1. Their sum is
4 + 3 + 2 + 1 = 10.
String res = “”;
for (int i = 0; i < s.length(); i++) {
String c = s.substring(i, i + 1);
if (res.indexOf(c) < 0) {
res += c;
}
}
System.out.println(res);
indexOf(c) returns -1 when the character
is not already present in res. The first occurrence
of each character is therefore added, producing "aple".
for (int i = 1; i <= 4; i++) {
val = val * 2 + i;
}
System.out.println(val);
Trace the value of val:
- Start:
val = 0 i = 1:val = 0 * 2 + 1 = 1i = 2:val = 1 * 2 + 2 = 4i = 3:val = 4 * 2 + 3 = 11i = 4:val = 11 * 2 + 4 = 26
The final value printed is 26.
int c = 0;
for (int i = 0; i < s.length() – 1; i++) {
if (s.substring(i, i + 2).equals(“ab”)) {
c++;
}
}
System.out.println(c);
The loop stops at s.length() - 2, so
substring(i, i + 2) always has two valid characters.
The substring "ab" occurs twice in
"abracadabra".
n?
The remainder when a positive base-10 integer is divided by
10 is its rightmost digit. For example,
345 % 10 is 5.
Integer division by 10 removes the rightmost digit.
Unit 2.9–2.10 Algorithms Practice Complete!
Why Loop and String Algorithms Matter
Loop-based algorithms are an important part of AP Computer Science A. You should be comfortable tracing loops, processing strings character by character, counting occurrences, building new strings, and using accumulator variables to solve algorithmic problems.
These skills also provide a foundation for understanding more complex algorithms involving nested iteration and data collections.
Continue Your AP CSA Practice
Next, practice Topic 2.11: Nested Iteration to learn how one loop executes inside another and how to trace the resulting execution.
Continue to Topic 2.11: Nested Iteration Practice .