Looping Statements

Lesson 4 of 20

Loops do the boring parts for you — trace while, do-while and for round by round until the condition says stop.

The big picture

Decisions and repetition

A loop repeats work for you. while checks first, do-while runs once before checking, and for packs the counter on one line.

while loop

Repeats its block while the condition stays true — check first, run after.

do-while loop

Runs the body once before any check — guaranteed at least one round.

for loop

A while loop with a built-in counter: start, condition and step sit on one line.

The exit rule

Something inside must push the condition toward false — or the loop never ends.