For loops
Run uses for as its only loop construct. It covers all common looping patterns.
Infinite loop
Section titled “Infinite loop”A bare for loops forever. Use break to exit.
for { // runs forever break}Conditional loop
Section titled “Conditional loop”A for with a condition works like while in other languages.
package main
use "fmt"
pub fun main() { x := 0 for x < 10 { fmt.println(x) x = x + 1 }}Range loop
Section titled “Range loop”Use in with a range expression to iterate over a sequence of numbers.
package main
use "fmt"
pub fun main() { for i in 0..5 { fmt.println(i) // prints 0, 1, 2, 3, 4 }}Iterator loop
Section titled “Iterator loop”Use in to iterate over any iterable value like slices or strings.
package main
use "fmt"
pub fun main() { names := []string{"Alice", "Bob", "Charlie"} for name in names { fmt.println(name) }}