All concepts

Loops

Loops

Qureo level 14

Why this matters

Stuck on a Qureo level where something has to happen "again and again"? That's a loop. Once you see how a loop counts, those levels stop being guesswork.

The idea

A loop repeats a block of code. A counting loop walks through a range of numbers and does something each time. Here, it adds each number to a running total.

Picture it

flowchart TD
  A[Start: total = 0] --> B{more numbers?}
  B -- yes --> C[add next number to total]
  C --> B
  B -- no --> D[print total]

Worked example

total = 0
for i in range(1, 6):
    total += i
print(total)   # 15

Your turn

Try the practice questions for this lesson: predict what a loop prints, then write one yourself.

Recap

  • A loop repeats code; iteration is one pass through it.
  • A counting loop uses a range and a running total.

Glossary

iteration
Repeating a step or block of code multiple times.
loop
A construct that runs the same code repeatedly until a condition ends it.

Practice & Assessment

Quizzes unlock when you score 100% on their practice.