Skip to content

Control Flow

If / Else

wyn
var score = 85

if score >= 90 {
    println("A grade")
} else if score >= 80 {
    println("B grade")
} else {
    println("Try harder")
}

For Loops

Range

wyn
for i in 0..5 {
    println("Count: ${i}")
}

Over Arrays

wyn
var fruits = ["apple", "banana", "orange"]
for fruit in fruits {
    println("Fruit: ${fruit}")
}

Indexed Iteration

Access both the index and value with for i, v in arr:

wyn
var names = ["Alice", "Bob", "Charlie"]
for i, name in names {
    println(i.to_string() + ": " + name)
}
// 0: Alice
// 1: Bob
// 2: Charlie

Over Maps

Iterate a HashMap directly. Bind both key and value with for k, v in map, or just the keys with for k in map:

wyn
var scores = {"alice": 90, "bob": 85}

for name, score in scores {
    println("${name}: ${score}")
}

for name in scores {
    println(name)
}

Membership: in / not in

Test membership with in and not in. It works on arrays (element), strings (substring), and maps (key):

wyn
var nums = [1, 2, 3]
if 2 in nums { println("has 2") }
if 5 not in nums { println("no 5") }

if "ell" in "hello" { println("substring") }

var config = {"debug": "on"}
if "debug" in config { println("debug is set") }

in sits at comparison precedence, so it reads naturally inside if and while conditions. (The in of a for … in … loop is unaffected.)

While Loops

wyn
var count = 0
while count < 3 {
    println("Count: ${count}")
    count = count + 1
}

Break and Continue

wyn
for i in 0..100 {
    if i == 5 { break }
    if i % 2 == 0 { continue }
    println(i.to_string())
}

Try It

🐉 Playground
Press Run or Ctrl+Enter

See Also

MIT License — v1.16