Skip to content

Control Flow

Wysna provides structured control flow primitives: conditionals (if / elif / else), loops (for in, while), and exhaustive pattern matching (match).

Conditionals evaluate boolean expressions without requiring surrounding parentheses. Use elif for chained branches:

fn assess_score(score: int) -> str {
if score >= 90 {
return "Excellent"
} elif score >= 70 {
return "Good"
} else {
return "Needs Improvement"
}
}

Because if blocks can act as expressions in statement positions, all branches must return compatible types.


Wysna includes first-class range syntax:

  • 0..limit: Half-open range (from 0 up to, but not including, limit).
  • 1..=limit: Inclusive range (from 1 up to and including limit).
fn main() -> unit {
# Summing numbers 1 through 10
let mut total = 0
for i in 1..=10 {
total = total + i
}
print("Total:", total)
# Iterating over lists
let items = ["apple", "banana", "orange"]
for fruit in items {
print("Fruit:", fruit)
}
# Iterating with index
for idx, fruit in enumerate(items) {
print(idx, fruit)
}
}

Use while for condition-driven iteration:

fn countdown(start: int) -> unit {
let mut current = start
while current > 0 {
print(current)
current = current - 1
}
print("Liftoff!")
}

Inside any loop, use break to terminate early and continue to jump directly to the next iteration:

fn find_first_even(numbers: list[int]) -> int {
let mut found = -1
for n in numbers {
if n % 2 != 0 {
continue
}
found = n
break
}
return found
}