while loops
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
# 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
|
||||
}
|
||||
}
|
||||
|
||||
# The body-local k shadows only inside the body. The update still targets
|
||||
# the mutable k declared before the loop.
|
||||
k u32 = 0
|
||||
while k < 4 : k = k + 1 {
|
||||
k u32 = 100
|
||||
if 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
|
||||
}
|
||||
Reference in New Issue
Block a user