Concept

How do loops work in programming?

A loop runs the same block of code once per item, updating its variables each time through. Below is a real widget from epochai's own curriculum, not a diagram.

A for loop takes a collection — a list, a range of numbers — and runs its body once for each item in it. Each pass, the loop variable (often called n, i, or whatever name fits) gets set to the next item, and any code inside the loop runs again with that new value.

The part worth watching closely is an accumulator: a variable declared before the loop starts, that the loop body updates on every pass. total = 0 followed by total += n inside the loop is the classic pattern — by the time the loop ends, total holds the sum of everything it saw, one small update at a time.

This is also where most beginner bugs live: forgetting to initialize the accumulator before the loop, or updating the wrong variable inside it. Tracing the loop step by step — which is exactly what the widget below does — is the fastest way to see where a loop's logic actually breaks.

Watch it happen

The same interactive widget epochai's own lessons use — live, not a recording.

Playing automatically. Every epochai lesson also has an Explore mode where you drive it yourself.

This is one line out of 49 free lessons

Module 1 — Programming Fundamentals — covers variables, control flow, loops, functions, and debugging. Free, no card required.