// Conformance Test: Control Flow
//
// SEMANTIC CONTRACT:
// - if/else executes the correct branch based on condition
// - if/else can be used as an expression (returns a value)
// - while loops execute while condition is true
// - for loops iterate over ranges and collections
// - loop creates infinite loops (exit with break)
// - break exits the innermost loop
// - continue skips to the next iteration
// - Nested control flow works correctly
// - Short-circuit evaluation for && and ||
//
// EXPECTED OUTPUT:
// [if_true] branch: yes
// [if_false] branch: no
// [if_expr] sign of 5: positive
// [if_expr] sign of -3: negative
// [if_expr] sign of 0: zero
// [if_chain] small
// [if_chain] medium
// [if_chain] large
// [while] count to 5: 5
// [while] sum 1..5: 15
// [for_range] sum 0..5: 10
// [for_range] sum 1..=5: 15
// [for_vec] items: apple banana cherry
// [for_index] 0:a 1:b 2:c
// [loop_break] loop exited at: 3
// [continue] odd numbers: 1 3 5 7 9
// [nested] (0,0) (0,1) (1,0) (1,1) (2,0) (2,1)
// [shortcircuit] true && true = true
// [shortcircuit] true && false = false
// [shortcircuit] false && true = false
// [shortcircuit] true || false = true
// [shortcircuit] false || true = true
// [shortcircuit] false || false = false
// [control_all] PASSED
// --- If/else branches ---
fn test_if_else() {
let x = 10
if x > 5 {
println("[if_true] branch: yes")
} else {
println("[if_true] branch: no")
}
let y = 2
if y > 5 {
println("[if_false] branch: yes")
} else {
println("[if_false] branch: no")
}
}
// --- If/else as expression ---
fn sign(x: int) -> string {
if x > 0 {
"positive"
} else if x < 0 {
"negative"
} else {
"zero"
}
}
fn test_if_expression() {
println("[if_expr] sign of 5: ${sign(5)}")
println("[if_expr] sign of -3: ${sign(-3)}")
println("[if_expr] sign of 0: ${sign(0)}")
}
// --- If/else chain ---
fn categorize(x: int) -> string {
if x < 10 {
"small"
} else if x < 100 {
"medium"
} else {
"large"
}
}
fn test_if_chain() {
println("[if_chain] ${categorize(5)}")
println("[if_chain] ${categorize(50)}")
println("[if_chain] ${categorize(500)}")
}
// --- While loops ---
fn test_while() {
let mut count = 0
while count < 5 {
count += 1
}
println("[while] count to 5: ${count}")
let mut sum = 0
let mut i = 1
while i <= 5 {
sum += i
i += 1
}
println("[while] sum 1..5: ${sum}")
}
// --- For loops with ranges ---
fn test_for_range() {
let mut sum = 0
for i in 0..5 {
sum += i
}
println("[for_range] sum 0..5: ${sum}")
let mut sum2 = 0
for i in 1..=5 {
sum2 += i
}
println("[for_range] sum 1..=5: ${sum2}")
}
// --- For loops with collections ---
fn test_for_vec() {
let fruits = vec!["apple", "banana", "cherry"]
let mut result = ""
for fruit in fruits {
if result.len() > 0 {
result = result + " "
}
result = result + fruit
}
println("[for_vec] items: ${result}")
}
// --- For loops with index ---
fn test_for_index() {
let letters = vec!["a", "b", "c"]
let mut result = ""
let mut idx = 0
for letter in letters {
if idx > 0 {
result = result + " "
}
result = result + "${idx}:${letter}"
idx += 1
}
println("[for_index] ${result}")
}
// --- Loop with break ---
fn test_loop_break() {
let mut i = 0
loop {
if i >= 3 {
break
}
i += 1
}
println("[loop_break] loop exited at: ${i}")
}
// --- Continue ---
fn test_continue() {
let mut result = ""
for i in 0..10 {
if i % 2 == 0 {
continue
}
if result.len() > 0 {
result = result + " "
}
result = result + "${i}"
}
println("[continue] odd numbers: ${result}")
}
// --- Nested loops ---
fn test_nested() {
let mut result = ""
for i in 0..3 {
for j in 0..2 {
if result.len() > 0 {
result = result + " "
}
result = result + "(${i},${j})"
}
}
println("[nested] ${result}")
}
// --- Short-circuit evaluation ---
fn test_short_circuit() {
println("[shortcircuit] true && true = ${true && true}")
println("[shortcircuit] true && false = ${true && false}")
println("[shortcircuit] false && true = ${false && true}")
println("[shortcircuit] true || false = ${true || false}")
println("[shortcircuit] false || true = ${false || true}")
println("[shortcircuit] false || false = ${false || false}")
}
fn main() {
test_if_else()
test_if_expression()
test_if_chain()
test_while()
test_for_range()
test_for_vec()
test_for_index()
test_loop_break()
test_continue()
test_nested()
test_short_circuit()
println("[control_all] PASSED")
}