blob: 01a8dc9ecb1492f4f434a4dee099efd4743545e1 [file] [log] [blame] [view] [edit]
# while
The `while` keyword can be used to run a loop while a condition is true.
Let's write the infamous [FizzBuzz][fizzbuzz] using a `while` loop.
```rust,editable
fn main() {
// A counter variable
let mut n = 1;
// Loop while `n` is less than 101
while n < 101 {
if n % 15 == 0 {
println!("fizzbuzz");
} else if n % 3 == 0 {
println!("fizz");
} else if n % 5 == 0 {
println!("buzz");
} else {
println!("{}", n);
}
// Increment counter
n += 1;
}
}
```
[fizzbuzz]: https://en.wikipedia.org/wiki/Fizz_buzz