Skip to content
Kavachकवच

For Loop: Over a list

● Beginner3 min

Loop over a list: the block runs once per item, in order, and the name you choose holds the item.

fn main() {
    const names = ["Asha", "Ravi", "Meena"]
    for name in names {
        print("Hello, {name}")
    }
}

The program ran and printed:

kavach run list.kav

Hello, AshaHello, RaviHello, Meena
list.kav: Kavach code, then what running it prints

Lists get their own page in Collections; here you only need the square brackets. A loop inside a loop goes through a list of lists:

fn main() {
    const grid = [[1, 2], [3, 4]]
    for row in grid {
        for cell in row {
            print(cell)
        }
    }
}

The program ran and printed:

kavach run nested.kav

1234
nested.kav: Kavach code, then what running it prints