What Does This Program Do? - Looping
Contest 2 adds loops to the WDTPD problems. You’ll see FOR loops and WHILE loops in pseudocode. The skill is tracking variables across iterations without losing count.
ACSL loop syntax
FOR loop
FOR i = 1 TO 5 STEP 2
(statements)
NEXT Rules:
- STEP is optional. If omitted, the step is
- The end value is inclusive - the loop runs when i equals the end value
- STEP can be negative (counting down)
Example: FOR i = 1 TO 5 STEP 2 runs with (three iterations).
Example: FOR i = 10 TO 1 STEP -3 runs with (four iterations).
WHILE loop
WHILE condition
(statements)
END WHILE The condition is checked before each iteration. If false initially, the body never executes.
Try it: trace a FOR loop
Watch how sum accumulates across each iteration.
1sum = 02FOR i = 1 TO 4 STEP 13 sum = sum + i4NEXT5OUTPUT: sum| Name | Value |
|---|---|
| sum | 0 |
The iteration table
For loop problems, make a table with one row per iteration. This is non-negotiable for accuracy.
Example:
INPUT: n = 5
s = 0
FOR i = 1 TO n
s = s + i * i
NEXT
OUTPUT: s | Iteration | i | i*i | s |
|---|---|---|---|
| - | - | - | |
Answer: 55 (sum of squares from to )
Nested loops
When loops are inside other loops, the inner loop runs fully for each iteration of the outer loop.
Example:
count = 0
FOR i = 1 TO 3
FOR j = 1 TO i
count = count + 1
NEXT
NEXT
OUTPUT: count | Outer i | Inner j values | count after inner loop |
|---|---|---|
Answer: 6
Tip: For nested loops, track the outer loop iteration and summarize the inner loop’s effect. Don’t try to track every single inner iteration unless necessary.
WHILE loops with changing conditions
WHILE loops are trickier because the number of iterations isn’t obvious upfront.
Example:
INPUT: x = 100
count = 0
WHILE x > 1
x = INT(x / 2)
count = count + 1
END WHILE
OUTPUT: count | Iteration | x (before) | INT(x/2) | x (after) | count |
|---|---|---|---|---|
| 1 | 100 | 50 | 50 | 1 |
| 2 | 50 | 25 | 25 | 2 |
| 3 | 25 | 12 | 12 | 3 |
| 4 | 12 | 6 | 6 | 4 |
| 5 | 6 | 3 | 3 | 5 |
| 6 | 3 | 1 | 1 | 6 |
Now x = 1, so x > 1 is false. Loop ends.
Answer: 6
Recognizing the pattern: This counts how many times you can halve a number before reaching 1 - it’s essentially .
Try it: trace a WHILE loop
This program extracts and sums the digits of a number.
1n = 1232sum = 03WHILE n > 04 digit = n MOD 105 sum = sum + digit6 n = INT(n / 10)7END WHILE8OUTPUT: sum| Name | Value |
|---|---|
| n | 123 |
| sum | 0 |
Accumulator patterns
Most loop problems follow one of these patterns:
Sum accumulator:
s = 0
FOR i = 1 TO n
s = s + (something involving i)
NEXT Product accumulator:
p = 1
FOR i = 1 TO n
p = p * (something involving i)
NEXT (This is factorial when the “something” is just i.)
Counter:
count = 0
FOR i = 1 TO n
IF (condition involving i) THEN
count = count + 1
END IF
NEXT Max/min finder:
max = A[0]
FOR i = 1 TO n-1
IF A[i] > max THEN
max = A[i]
END IF
NEXT Recognizing these patterns helps you predict the answer and verify your trace.
The MOD trick in loops
ACSL loves using MOD inside loops. Common patterns:
Digit extraction (right to left):
n = 1234
WHILE n > 0
digit = n MOD 10
n = INT(n / 10)
END WHILE This processes digits in that order.
Checking divisibility:
count = 0
FOR i = 1 TO 100
IF i MOD 3 == 0 AND i MOD 5 == 0 THEN
count = count + 1
END IF
NEXT This counts numbers from to divisible by both and .
Common mistakes
- Off-by-one errors. ACSL FOR loops are inclusive on both ends.
FOR i = 1 TO 5runs times, not . - Forgetting the STEP.
FOR i = 10 TO 1with no STEP has step , so it never executes ( is already past going up). - WHILE condition checked before each iteration. If the condition is false at the start, the loop body never runs.
- Variable modified inside the loop affecting the loop condition. In a WHILE loop, if the loop body changes the variable in the condition, trace carefully.
- INT() is the floor function. returns the greatest integer . So , but (not ).
Contest strategy
- Build an iteration table every time. No exceptions.
- For FOR loops, count the iterations first
- For WHILE loops, trace until the condition fails
- If a loop has many iterations, look for a pattern after 3-4 rows
- Typical time: 90-120 seconds per problem