AutolangDocs

Control Flow

Autolang supports traditional control flow structures and elevates them by treating if and when as expressions that return values.

If / If-Else

The standard if statement executes a block only when the condition is true. An optional else branch runs when the condition is false.

var counter = 0 if (true) { counter += 1 } if (false) { counter += 100 } else { counter += 2 } println(counter) // 3

If as an Expression

Similar to Kotlin, if and else can be used as expressions — they return a value and can be assigned directly to a variable. When used as an expression, the else branch is mandatory. The last expression in each block is the returned value.

// Single-expression form val a = if (true) 10 else 20 println(a) // 10 // Block form: last expression is the return value val b = if (a == 10) { println("A is 10!") 20 // returned } else { 100 } println(b) // 20

While Loop

The while loop repeats a block as long as its condition is true.

var n = 0 while (n < 5) { n += 1 } println(n) // 5 // Infinite loop broken manually var k = 0 while (true) { k += 1 if (k == 3) break } println(k) // 3

For Loop

The for...in loop iterates over a range or collection. Autolang supports both inclusive and exclusive ranges, as well as iterating directly over arrays.

// Inclusive range: 1..10 (includes both 1 and 10) var sum = 0 for (i in 1..10) { sum += i } println(sum) // 55 // Exclusive upper bound: 0..<10 (includes 0 to 9) var sumExclusive = 0 for (i in 0..<10) { sumExclusive += i } println(sumExclusive) // 45 // Iterate over an array val items = <Int>[10, 20, 30, 40, 50] var total = 0 for (item in items) { total += item } println(total) // 150 // Nested loops var product = 0 for (i in 1..3) { for (j in 1..3) { product += i * j } } println(product) // 36

Break & Continue

Use break to exit the innermost loop immediately. Use continue to skip the rest of the current iteration and move to the next one. Both work inside for and while loops.

// break: stop loop when i == 4 var breakAt = -1 for (i in 0..9) { if (i == 4) { breakAt = i break } } println(breakAt) // 4 // continue: skip even numbers, sum only odds var sum = 0 for (i in 0..9) { if (i % 2 == 0) continue sum += i // 1+3+5+7+9 = 25 } println(sum) // 25 // break only exits the inner loop var outerCount = 0 for (i in 0..2) { outerCount += 1 for (j in 0..9) { if (j == 2) break } } println(outerCount) // 3

When Expression

The when expression replaces traditional switch statements. It has no fall-through, so no break is needed. It can match exact values, evaluate boolean conditions, check ranges with in, and perform type checks with is.

1. Exact value matching

val day = 3 val dayName = when (day) { 1 -> "Mon" 2 -> "Tue" 3 -> "Wed" 4 -> "Thu" 5 -> "Fri" else -> "Weekend" } println(dayName) // Wed

2. Without argument — boolean guard form

When when has no argument, each branch is evaluated as a boolean expression. This is a clean alternative to long if-else if chains.

val score = 75 val grade = when (score) { score >= 90 -> "A" score >= 80 -> "B" score >= 70 -> "C" score >= 60 -> "D" else -> "F" } println(grade) // C // Also works without an argument (as a pure boolean chain) val x = 5 val result = when { x > 10 -> "big" x > 0 -> "small" else -> "negative" } println(result) // small

3. Range check with in

val n = 42 val category = when (n) { n in 0..9 -> "single digit" n in 10..99 -> "double digit" else -> "large" } println(category) // double digit

4. Type check with is

val b = 6 when (b) { is Int -> println("b is an Int") else -> println("unknown type") } // Prints: b is an Int

5. When as a statement (no value)

val a = 5 when (a) { in 1..9 -> println("Single digit") else -> println("Other") } // Prints: Single digit