53 lines
953 B
Plaintext
53 lines
953 B
Plaintext
# Milestone 5: boolean while loops with optional post-iteration updates.
|
|
|
|
return_before_update func() i32 {
|
|
i i32 = 0
|
|
while true : i = i + 1 {
|
|
return i
|
|
}
|
|
}
|
|
|
|
main func() i32 {
|
|
total i32 = 0
|
|
|
|
# ordinary condition and update
|
|
i u32 = 0
|
|
while i < 5 : i = i + 1 {
|
|
total = total + 2
|
|
}
|
|
|
|
# equivalent parenthesized header
|
|
j u32 = 0
|
|
while (j < 4) : (j = j + 1) {
|
|
total = total + 3
|
|
}
|
|
|
|
# nested loops and body-local storage
|
|
outer u32 = 0
|
|
while outer < 2 : outer = outer + 1 {
|
|
inner u32 = 0
|
|
while inner < 3 : inner = inner + 1 {
|
|
total = total + 2
|
|
}
|
|
}
|
|
|
|
# Body-local storage stays scoped to the body. The update still targets
|
|
# the mutable k declared before the loop.
|
|
k u32 = 0
|
|
while k < 4 : k = k + 1 {
|
|
body_k u32 = 100
|
|
if body_k == 100 {
|
|
total = total + 2
|
|
}
|
|
}
|
|
|
|
# zero iterations
|
|
while false {
|
|
total = total + 100
|
|
}
|
|
|
|
# A return exits before the update clause.
|
|
total = total + return_before_update()
|
|
return total
|
|
}
|