Control Flow
Autolang elevates traditional control structures by treating if and when as first-class expressions that return values, eliminating intermediate mutable flags in AI-generated orchestration.
If / If-Else Statements & Expressions
Parentheses around condition expressions are optional. When used as an expression, the else branch is mandatory, and the final evaluated expression in the selected block becomes the return value.
val accountActive = true
val balance = 1200
// 1. Condition without parentheses
if accountActive && balance > 0 {
println("Account eligible for transaction")
}
// 2. Single-line if statement
var priorityLevel = 1
if balance > 1000 priorityLevel = 2
println("Priority: " + priorityLevel)
// 3. If as an expression
val tier = if (balance > 1000) "PREMIUM" else "STANDARD"
println("Customer tier: " + tier)
// 4. Block expression: last line is the evaluated value
val fee = if balance > 500 {
val discount = 5
15 - discount
} else {
20
}
println("Computed fee: " + fee)When Expression
The when expression replaces legacy switch statements without fall-through bugs. It can match values, evaluate boolean guards, check ranges with in / !in, and check types with is / !is.
// 1. State machine workflow dispatching
val event = "VERIFY"
var state = "PAID"
val nextState = when (state) {
"START" -> if (event == "CREATE") "CREATED" else "REJECTED"
"CREATED" -> if (event == "PAY") "PAID" else "REJECTED"
"PAID" -> if (event == "VERIFY") "VERIFIED" else "REJECTED"
"VERIFIED" -> if (event == "SHIP") "SHIPPED" else "REJECTED"
else -> "UNKNOWN"
}
println("Next state: " + nextState) // VERIFIED
// 2. Boolean guard form (no subject argument)
val score = 85
val evaluation = when {
score >= 90 -> "EXCELLENT"
score >= 75 -> "APPROVED"
score >= 50 -> "REVIEW_REQUIRED"
else -> "REJECTED"
}
println("Evaluation: " + evaluation) // APPROVED
// 3. Range and type matching
val inputData: Any = 42
val classification = when (inputData) {
in 1..10 -> "Single-digit range"
!in 0..100 -> "Out-of-bounds number"
is Int -> "Valid integer value"
!is String -> "Non-string object"
else -> "Other"
}
println("Classification: " + classification)For Loops & Iteration
The for loop iterates over ranges, arrays, sets, and maps. Parentheses around the loop target are optional. Autolang supports inclusive ranges (..), exclusive upper bounds (..< or until), custom stepping with step, and destructuring.
// 1. Inclusive range with step
var stepSum = 0
for (i in 0..10 step 2) {
stepSum += i
}
println("Step sum (0, 2, 4, 6, 8, 10): " + stepSum) // 30
// 2. Range using 'until' keyword
var untilSum = 0
for i in 0 until 4 {
untilSum += i
}
println("Until sum (0, 1, 2, 3): " + untilSum) // 6
// 3. Iterating Map with key and value variables
val rates = mapOf("EUR" to 1.08, "GBP" to 1.27)
for (currency, rate in rates) {
println(currency + " -> " + rate)
}
// 4. Iterating with index using withIndex()
val services = ["auth", "billing", "notifications"]
for ((idx, serviceName) in services.withIndex()) {
println("Service " + idx + ": " + serviceName)
}While Loops, Break & Continue
while repeats execution while its condition evaluates to true. break terminates the innermost loop, and continue advances to the next iteration immediately.
var retries = 0
val maxRetries = 3
while (retries < maxRetries) {
retries += 1
if (retries == 2) {
println("Skipping diagnostic retry: " + retries)
continue
}
println("Processing attempt: " + retries)
}Elvis Operator for Early Exit
Combine the Elvis operator ?: with return, break, continue, or throw to write robust, concise guard clauses without nested branching.
fun processBatch(ledger: Map<String, Int>, requiredIds: Array<String>): Int {
var totalProcessed = 0
for (id in requiredIds) {
// Skip entry if not present in ledger
val amount = ledger[id] ?: continue
// Abort processing if an item exceeds risk ceiling
if (amount > 10000) break
totalProcessed += amount
}
return totalProcessed
}