← All examples

FizzBuzz

Loop over a range with for..in and branch with if / else if / else.

for n in range(1, 101) {
    if n % 15 == 0 {
        println("FizzBuzz")
    } else if n % 3 == 0 {
        println("Fizz")
    } else if n % 5 == 0 {
        println("Buzz")
    } else {
        println(toString(n))
    }
}

range(1, 101) produces the integers 1 through 100. % is the modulo operator, and numbers are turned into strings for printing with toString.

#basics#control-flow · View as Markdown