Skip to content
UPEEC

Loops

Loops repeat a block of code — a fixed number of times (for) or while a condition holds (while).

Beginner
Comments 0
  • for — when you know how many times: iterate over a range or collection.
  • while — repeat while a condition stays true.
  • do-while (C, C++, Java) — runs at least once, then checks.
for i in range(5):        # 0, 1, 2, 3, 4
    print(i)

reading = read_sensor()
while reading < 100:
    reading = read_sensor()

break exits a loop early; continue skips to the next iteration.

In embedded code the main program is usually an intentional infinite loop (while (1) { ... } or Arduino's loop()), because the device should run forever.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.