What to practice
- Trace while and for loops, including early return, off-by-one boundaries, and infinite-loop conditions.
- Implement base-10 integer digit extraction and String sliding-window algorithms.
- Calculate exact statement execution counts in independent and dependent nested loops.
Current CED topics: 2.1, 2.7, 2.8, 2.9, 2.10, 2.11, 2.12
Current learning objectives: 2.7.B, 2.8.A, 2.9.A, 2.10.A, 2.11.A, 2.12.A
1. Core Concept Instruction
A for loop for (init; condition; update) executes init once, tests condition before each pass, executes the body if true, and runs update at the end of each pass. Two useful loop patterns are: (1) Base-10 Digit Extraction: while (n > 0) { int digit = n % 10; /* process rightmost digit */ n /= 10; } processes digits from right to left and executes exactly d times for a d-digit positive integer; (2) Sliding-Window String Traversal: for (int i = 0; i <= s.length() - k; i++) { String window = s.substring(i, i + k); } inspects every length-k substring without throwing StringIndexOutOfBoundsException. In nested loops, if the outer loop runs R times and the inner loop runs C times independently, an inner statement executes R * C times; if the inner bound depends on the outer index i, sum the inner counts across each value of i. To analyze a loop, count body executions instead of reading just its final bound. A loop whose control value is divided by 2 each pass reaches 1 after only six passes when it starts at 64. For a positive power of two, doubling the starting value adds one pass. A triangular nested loop with inner counts 0 through n - 1 performs n(n - 1)/2 inner iterations.
2. Worked Examples
Worked Digit-Reversal Loop & Dependent Nested Loop Execution Count
Part 1: Trace rev after running: int n = 4072, rev = 0; while (n > 0) { rev = rev * 10 + n % 10; n /= 10; }. Part 2: How many stars '*' are printed by: for (int i = 1; i <= 5; i++) { for (int j = i; j <= 5; j++) { System.out.print("*"); } }
- Step 1 (Trace Digit Extraction Pass 1 & 2): Pass 1: n=4072 -> rev = 010 + 2 = 2, n = 407. Pass 2: n=407 -> rev = 210 + 7 = 27, n = 40.
- Step 2 (Trace Digit Extraction Pass 3 & 4): Pass 3: n=40 -> rev = 2710 + 0 = 270, n = 4. Pass 4: n=4 -> rev = 27010 + 4 = 2704, n = 0 (loop terminates with rev == 2704).
- Step 3 (Count Inner Loop Executions for Each i): When i = 1, j runs 1..5 (5 stars). When i = 2, j runs 2..5 (4 stars). When i = 3, j runs 3..5 (3 stars). When i = 4, j runs 4..5 (2 stars). When i = 5, j runs 5..5 (1 star).
- Step 4 (Sum Total Executions): Total stars printed = 5 + 4 + 3 + 2 + 1 = 15.
Result:
Nested Loop Execution Count & Rectangular Grid Coordinate Trace
Determine how many times "*" is printed and the final value of sum:
int sum = 0;
for (int r = 1; r <= 3; r++) {
for (int c = r; c <= 3; c++) {
sum += c;
System.out.print("*");
}
}
- Step 1 (Trace r = 1): c runs 1, 2, 3 (3 stars; sum += 1 + 2 + 3 = 6).
- Step 2 (Trace r = 2 and r = 3): For r = 2, c runs 2, 3 (2 stars; sum += 5 -> 11). For r = 3, c runs 3 (1 star; sum += 3 -> 14).
Result: 6 stars printed and sum = 14