Back to Syllabus
Chapter 03AAYU Core - Verified

While loops and accumulators

Repeat a block with while and build a running total through assignment.

This lesson uses the verified core subset and links to a checked-in regression program. Its output is covered by the repository test harness. This does not imply support for features outside the lesson; see known limitations.

What you'll learn

✓Initialize loop state
✓Write a while condition
✓Update a variable each iteration
✓Check a known sum

Count and accumulate

The loop regression example adds the numbers 0 through 9. It starts both variables at zero, then increments 'count' until the condition is false.

aayu
let count = 0
let sum = 0
while count < 10
    sum = sum + count
    count = count + 1
end
print(sum)

Expected sum: 45. If the counter update is removed, the loop never ends; if the condition changes to 'count < 9', the final sum changes. The regression file also checks the result with an if/else.

Try It Yourself! 🚀

Change the condition to count < 5. Predict the sum, run the program, and explain why the last included value is 4.