Skip to content
Kavachकवच

While Loop: while

● Beginner2 min

while condition { … } checks the condition, runs the block, and checks again.

fn main() {
    let fuel = 3
    while fuel > 0 {
        print("burn {fuel}")
        fuel -= 1
    }
    print("empty")
}

The program ran and printed:

kavach run while.kav

burn 3burn 2burn 1empty
while.kav: Kavach code, then what running it prints

Round by round: fuel is 3, 2, 1, and the block runs each time. Then fuel is 0, fuel > 0 is false, and the program goes on after the loop.