59 lines
1.4 KiB
Plaintext
59 lines
1.4 KiB
Plaintext
# Milestone 18: `break` and `continue`.
|
|
#
|
|
# `break` exits the innermost loop; `continue` skips to that loop's next
|
|
# iteration (running the update / index increment first). Both target the
|
|
# innermost enclosing loop. Each section returns a distinct code on failure so
|
|
# a regression points at the broken behaviour; success falls through to 42.
|
|
|
|
main :: func() i32 {
|
|
# 1. `break` out of a `while` once i reaches 5.
|
|
i i32 = 0
|
|
a i32 = 0
|
|
while i < 100 : i += 1 {
|
|
if (i == 5) break
|
|
a += 1
|
|
}
|
|
if (a != 5) return 101
|
|
|
|
# 2. `continue` past n == 3 while summing 0..9 (45 - 3 = 42).
|
|
b i32 = 0
|
|
for 0..10 |n| {
|
|
if (n == 3) continue
|
|
b = b + n
|
|
}
|
|
if (b != 42) return 102
|
|
|
|
# 3. Nested loops: the inner `break` exits only the inner loop, so the outer
|
|
# loop still runs all three iterations (each contributing one y == 0 pass).
|
|
c i32 = 0
|
|
for 0..3 |x| {
|
|
for 0..3 |y| {
|
|
if (y == 1) break
|
|
c += 1
|
|
}
|
|
_ = x
|
|
}
|
|
if (c != 3) return 103
|
|
|
|
# 4. `continue` on the final element of an inclusive range bounded by the
|
|
# element type's maximum must exit cleanly, not overflow the increment.
|
|
hi u8 :: 255
|
|
d i32 = 0
|
|
for 0..=hi |v| {
|
|
if (v == 255) continue
|
|
d += 1
|
|
}
|
|
if (d != 255) return 104
|
|
|
|
# 5. `while true` is exitable via `break` (so it is not an infinite loop and
|
|
# the code after it is reachable).
|
|
e i32 = 0
|
|
while true {
|
|
e += 1
|
|
if (e == 7) break
|
|
}
|
|
if (e != 7) return 105
|
|
|
|
return 42
|
|
}
|