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.

Code
1sum = 02FOR i = 1 TO 4 STEP 13    sum = sum + i4NEXT5OUTPUT: sum
Variables
NameValue
sum0
Step 1 of 10

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
Iterationii*is
---

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 iInner j valuescount 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
Iterationx (before)INT(x/2)x (after)count
110050501
25025252
32512123
412664
56335
63116

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.

Code
1n = 1232sum = 03WHILE n > 04    digit = n MOD 105    sum = sum + digit6    n = INT(n / 10)7END WHILE8OUTPUT: sum
Variables
NameValue
n123
sum0
Step 1 of 15

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

  1. Off-by-one errors. ACSL FOR loops are inclusive on both ends. FOR i = 1 TO 5 runs times, not .
  2. Forgetting the STEP. FOR i = 10 TO 1 with no STEP has step , so it never executes ( is already past going up).
  3. WHILE condition checked before each iteration. If the condition is false at the start, the loop body never runs.
  4. 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.
  5. 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