Vyper theorytheory 0/50 · 0%
Control flow · easy

10. Iterating with range()

Fixed and bounded ranges for numeric loops.

`range(n)`, `range(start, stop)` and `range(start, stop, step)` all require their bounds to be resolvable at compile time (literals or constants), or you can use `range(n, bound=MAX)` to iterate a runtime value up to a fixed upper bound.

@external
@pure
def factorial(n: uint256) -> uint256:
    result: uint256 = 1
    for i in range(1, 21):
        if i > n:
            break
        result *= i
    return result

`break` and `continue` work inside `for` loops just like in Python, letting you exit early once a condition is met.

Check your understanding

  1. 1. What must a `range()` bound be in Vyper?

  2. 2. How do you loop a runtime-sized count up to a fixed cap?

  3. 3. Does Vyper support `break` inside for loops?