break
break ends the innermost for or while loop. The program continues after the loop.
Details
breaktakes no label. In nested loops it ends only the innermost one.breakhas typenever, so it can stand where a value is expected, as inx ?? break.breakdoes not cross a function, a lambda, agoblock, or adeferblock. Abreakwith no loop inside the same boundary is a syntax error.breakruns thedeferblocks of every block it leaves.- In a
forover a sequence,breakmakesyieldreturnfalse, and the sequence must then return. - A
while trueloop has typeneveronly when its body has nobreak. With abreak, it has typevoid.
Basic examples
Stop at the first negative number:
let xs = [3, 8, -1, 5]
for x in xs {
if x < 0 { break }
println(x)
}3
8Leave a while true loop once a condition holds:
var n = 1
while true {
n *= 2
if n > 100 { break }
}
println(n)128Scope
As a value
break has type never, so it fits after ??. The loop ends at the first name without an age:
let ages = Map{ "ann": 31, "bob": 27 }
let names = ["ann", "cy", "bob"]
for name in names {
let age = ages[name] ?? break
println("${name}: ${age}")
}ann: 31Strings
A for over a string yields one character at a time:
let text = "hello world"
var length = 0
for ch in text {
if ch == " " { break }
length += 1
}
println(length)5Sequences
In a for over a sequence, break makes yield return false. The sequence sees it and returns:
func countdown(from: int): Seq[int] {
func(yield: func(int): bool) {
var i = from
while i > 0 {
if !yield(i) {
println("stopped at ${i}")
return
}
i -= 1
}
}
}
for n in countdown(5) {
if n == 3 { break }
println(n)
}5
4
stopped at 3With defer
break runs the defer blocks of the loop body it leaves:
for x in [1, 2] {
defer { println("closed ${x}") }
if x == 1 { break }
}closed 1Inside select
A break in a select block ends the loop around the select. Here either a closed lines channel or a value on quit ends the while true:
let lines: Channel[string] = Channel.new(3)
let quit: Channel[void] = Channel.new(1)
lines.send("a")
lines.send("b")
lines.close()
while true {
select {
let line = Channel.receive(lines) {
if line == null { break }
println(line)
}
let _ = Channel.receive(quit) { break }
}
}
println("done")a
b
doneApplications
Read input up to a blank line
let input = Os.stdin()
let names: Array[string] = []
while true {
let line = try input.readLine() ?? break
if line == "" { break }
names.push(line)
}
println(names | String.join(", "))Ann, BobThe first break handles the end of input, the second the blank line.
Leave nested loops
break has no label. To leave several loops at once, put them in a function and return:
type Cell = { row: int, col: int }
func findZero(grid: Array[Array[int]]): Cell | null {
for { index: row, value: cells } in grid.entries() {
for { index: col, value } in cells.entries() {
if value == 0 { return Cell{ row, col } }
}
}
null
}
println(findZero([[1, 2], [3, 0], [0, 4]]))Cell{ row: 1, col: 1 }Properties and relations
A loop that keeps the first match and breaks gives what find gives. Prefer find when the first match is all the code needs:
func firstNegative(xs: Array[int]): int | null {
var found: int | null = null
for x in xs {
if x < 0 {
found = x
break
}
}
found
}
let xs = [4, -2, -7]
assert(firstNegative(xs) == xs.find({ x < 0 }))
assert(firstNegative([]) == null)Possible issues
break in a lambda
A lambda is a boundary, so a break in it cannot end a loop outside it. Use a for loop instead of a callback such as map when the code needs to stop early:
let rows = [[1, 2], [3, 4]]
for row in rows {
row.map({
if x > 2 { break } // error: 'break' outside a loop
})
}Only the innermost loop ends
The break ends the loop over cells. The loop over rows goes on to the next row:
let grid = [[1, 2, 3], [4, 0, 6], [7, 8, 9]]
for row in grid {
let seen: Array[string] = []
for cell in row {
if cell == 0 { break }
seen.push("${cell}")
}
println(seen | String.join(" "))
}1 2 3
4
7 8 9A while true with break has type void
Without a break, while true never ends normally, and its type is never. A break makes its type void, so it no longer fits as the last expression of a function that returns a value:
func firstPowerOver(limit: int): int {
var n = 1
while true { // error: cannot use void where int is expected
n *= 2
if n > limit { break }
}
}Return the value from inside the loop instead: if n > limit { return n }.
break takes no value
A loop has no result, so break takes no operand. Assign the value to a var before break:
for x in [1, 2, 3] {
if x == 2 { break x } // error: 'break' takes no value
}var found: int | null = null
for x in [1, 2, 3] {
if x == 2 {
found = x
break
}
}
found // 2