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: CharlieWhile 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
Press Run or Ctrl+Enter
See Also
- Pattern Matching —
matchwith destructuring - Arrays — array methods for
for-inloops - Spawn & Await — concurrent loops