← Class Notes

CS · Week 3

Programming Basics — Loops & Arrays

Repeating work, storing lists, and common off-by-one mistakes.

An array (or list) stores values in order. Indexing usually starts at 0, so the last item is length − 1.

A for loop is good when you know how many times to repeat. A while loop repeats until a condition is false.

Off-by-one errors happen when you use < vs ≤, or forget that the last index is length − 1.

Trace a loop on paper with a tiny list. If you can predict each step, the code is easier to debug.

Practice