Loops let a few lines do the work of hundreds — for walks a sequence, while watches a condition.
1 for = Visitor
for i in range(3) hands i the values 0, 1 and 2 — one visit per value.
2 range = Number Factory
range(1, 4) yields 1, 2, 3 — it starts at the first number and stops before the second.
3 while = Watchman
Repeats the body as long as the condition stays True; it re-checks before every pass.
4 Update or Loop Forever
Inside a while, something must move the condition toward False — like count = count - 1.
Repeat without repeating yourself
for = Visitor1for i in range(3) hands i the values 0, 1 and 2 — one visit per value. range = Number Factory2range(1, 4) yields 1, 2, 3 — it starts at the first number and stops before the second. while = Watchman3Repeats the body as long as the condition stays True; it re-checks before every pass.
Update or Loop Forever4Inside a while, something must move the condition toward False — like count = count - 1.
4 building blocks make up Repeat without repeating yourself
Watch i change on every pass of the for loop — and the while loop re-check its condition each round.
maintotal starts at 0 — the accumulator that will collect the sum.
Console
output appears as the program runs…
A while loop re-checks its condition after every pass — the arrow looping back IS the loop.
process decision input / output start / end range is a number factory: press Run and watch it hand out values one pass at a time.
String repetition meets a range — trace each pass before you guess.
Your predicted output — one line per console line
Hint: range(1, 3) yields 1 and 2 — the end value is excluded. "ab" * n repeats the string n times.
01range(1, 4) yields 1, 2, 3 — the end value is always excluded.
02for walks any sequence; while repeats until its condition turns False.
03The loop line is re-highlighted on every pass — that re-check IS the loop.
04Every while needs an update inside the body, or it never stops.
05Accumulate with a total variable: start at 0 and add inside the loop.