.\" Made by website/plain/build.mjs from website/words/break.md; change that file. .TH SG-BREAK 7 2026-11-28 "SG" "SG reference" .SH NAME break \- leaves the loop it is in, or the loop it names, at once .SH SYNOPSIS .TP \fBbreak\fR leaves the loop it is in .TP \fBbreak name\fR leaves the loop named name, and every loop inside it .TP \fBname: for ..., name: while ...\fR a loop with a name, for break name .TP \fBpattern => break\fR an arm of a match or a select that leaves the loop around it .SH DESCRIPTION \fBbreak\fR stops a loop at once: the rest of the round is not run, and the program goes on after the loop. It takes no value. Inside a loop within a loop, it leaves only the inner one. To leave an outer loop, give it a name, \fBsearch: for ...\fR, and write \fBbreak search\fR; every loop inside it is left too. .PP A \fBbreak\fR belongs to the loops of its own function. A \fBmatch\fR is not a loop, so a \fBbreak\fR in an arm leaves the loop around the \fBmatch\fR; so does a \fBbreak\fR in an arm of a \fBselect\fR\&. A \fBbreak\fR may stand where a value is expected, in an arm of a \fBmatch\fR that gives a value: when it runs, there is no value, and the loop is left. .PP Leaving early lets go of what the round owns. A value made in the round and not yet given away is freed at the \fBbreak\fR, as at the end of the block; compiled code frees it there too. A value given away just before a \fBbreak\fR is fine, since no next round will look for it. A file still open, or a task not yet waited for, is an error at the \fBbreak\fR, as it would be at the end of its block. .PP After a loop, the compiler knows only what holds on every way out. A \fBwhile x is null\fR without a \fBbreak\fR ends only when \fBx\fR holds a value; with a \fBbreak\fR that leaves it, \fBx\fR may still be \fBnull\fR after the loop. A \fBbreak\fR of an inner loop does not count. In the same way, a function that ends with a \fBwhile true\fR needs no \fBreturn\fR after it only when no \fBbreak\fR leaves that loop. .SH EXAMPLES .SS "Stopping at the first find" .RS 4 .nf void main() { var temps = List { 18, 21, 25, 31, 28, 33 } int? firstHot = null for t in read temps { if t > 30 { firstHot = t break } } if firstHot is null { print("no hot day") } else { print($"the first hot day: {firstHot}") } } .fi .RE .PP .RS 4 .nf the first hot day: 31 .fi .RE .PP The loop stops at 31, and 33 is never looked at. The loop also ends when no day is hot, so the code after it tests which way it ended. .SS "Leaving the middle one of three loops" .RS 4 .nf void main() { var teams = List { "red", "blue" } var scores = List>> { List> { List { 3, 4 }, List { 0, 5 }, List { 6, 1 } }, List> { List { 2, 2 }, List { 1, 9 } } } for t in 0..teams.length() { var total = 0 rounds: for round in read scores[t] { for points in read round { if points == 0 { break rounds } total += points } } print($"{teams[t]}: {total}") } } .fi .RE .PP .RS 4 .nf red: 7 blue: 14 .fi .RE .PP There are three loops, and the name is on the middle one. \fBbreak rounds\fR leaves the loop over the points and the loop over the rounds, but the loop over the teams goes on with \fBblue\fR\&. A plain \fBbreak\fR would have left only the points of one round. .SS "break in an arm of a match" .RS 4 .nf enum Step { Add(int amount) Stop } void main() { var steps = List { Step.Add(3), Step.Add(4), Step.Stop, Step.Add(100) } var total = 0 for step in read steps { match step { Add(n) => total += n Stop => break } print($"so far {total}") } print($"in all {total}") } .fi .RE .PP .RS 4 .nf so far 3 so far 7 in all 7 .fi .RE .PP \fBStop => break\fR leaves the \fBfor\fR, not only the \fBmatch\fR: \fB"so far"\fR is not printed for it, and \fBAdd(100)\fR is never reached. .SS "break where a value goes" .RS 4 .nf void main() { var readings = List { 5, 8, -1, 7 } var total = 0 for r in read readings { total += match r { -1 => break var value => value } } print(total) } .fi .RE .PP .RS 4 .nf 13 .fi .RE .PP The \fBmatch\fR gives a number to add, except for \fB\-1\fR, where it gives none and leaves the loop. Nothing is added for it, and \fB7\fR is not read. .SS "What a round owns is let go" .RS 4 .nf void main() { var words = List { "alpha", "", "beta", "stop", "gamma" } var shouted = List() for word in read words { var loud = word.toUpper() if word.length() == 0 { continue } if word == "stop" { break } shouted.add(loud) } print(shouted) } .fi .RE .PP .RS 4 .nf ["ALPHA", "BETA"] .fi .RE .PP \fBloud\fR is a new text each round. At the \fBcontinue\fR and at the \fBbreak\fR it has not been given to the list, so it is freed there, in the interpreter and in compiled code alike. .SS "A value given away, then break" .RS 4 .nf void keep(List names) { print($"kept {names.length()}: {names}") } void main() { var batch = List() var words = List { "one", "two", "three", "four" } for word in read words { batch.add(word.clone()) if batch.length() == 3 { keep(batch) break } } } .fi .RE .PP .RS 4 .nf kept 3: ["one", "two", "three"] .fi .RE .PP \fBkeep\fR takes the list, so \fBbatch\fR is gone. That would be an error if the loop could go on, since the next round adds to \fBbatch\fR; the \fBbreak\fR right after means no round does. .SS "Narrowing kept, narrowing lost" .RS 4 .nf void main() { var rows = List> { List { "", "" }, List { "", "kitten", "cat" } } nint r = 0 string? found = null while found is null { for word in read rows[r] { if word.length() > 0 { found = word.clone() break } } r += 1 } print($"{found} in row {r - 1}, {found.length()} letters") } .fi .RE .PP .RS 4 .nf kitten in row 1, 6 letters .fi .RE .PP The \fBbreak\fR leaves only the inner \fBfor\fR\&. The \fBwhile\fR still ends only when its test fails, so after it \fBfound\fR is a \fBstring\fR, and \fBfound.length()\fR is allowed. Had the \fBbreak\fR left the \fBwhile\fR, \fBfound\fR would need a test of its own after the loop. .SS "A break of an inner loop, at the end of a function" .RS 4 .nf int firstPair(read List xs, int sum) { nint i = 0 while true { nint j = i + 1 while true { if j >= xs.length() { break } if xs[i] + xs[j] == sum { return (int)i } j += 1 } i += 1 } } void main() { var xs = List { 1, 4, 6 } print(firstPair(read xs, 10)) } .fi .RE .PP .RS 4 .nf 1 .fi .RE .PP The \fBbreak\fR leaves the inner loop only, so nothing leaves the outer \fBwhile true\fR but \fBreturn\fR\&. The function needs no \fBreturn\fR after it. .SH "WHAT THE COMPILER REFUSES" .SS "break with a value" .RS 4 .nf void main() { for i in 0..3 { break 5 } } .fi .RE .PP .RS 4 .nf error: `break` takes no value --> example.sg:3:15 3 | break 5 | ^ hint: set a variable before the `break`, or put the loop in a function of its own and `return` the value .fi .RE .PP \fBbreak\fR gives nothing. To bring a value out of a loop, set a variable before the \fBbreak\fR, or put the loop in a function and \fBreturn\fR the value. .SS "break in a function a loop calls" .RS 4 .nf void work(int n) { if n == 2 { break } print(n) } void main() { for i in 0..4 { work(i) } } .fi .RE .PP .RS 4 .nf error: `break` can only be written inside a loop --> example.sg:3:9 3 | break | ^^^^^ .fi .RE .PP A \fBbreak\fR sees only the loops of its own function. \fBwork\fR can \fBreturn\fR, or return a \fBbool\fR that the loop tests. .SS "break in a select with no loop around it" .RS 4 .nf void numbers(Sender out) { _ = out.send(1) } void main() { var ints = Channel(4) var a = Task.run(numbers, ints.sender()) select { ints.receive() as n => print(n) ended => break } a.wait() } .fi .RE .PP .RS 4 .nf error: `break` can only be written inside a loop --> example.sg:10:18 10 | ended => break | ^^^^^ .fi .RE .PP A \fBbreak\fR in a \fBselect\fR leaves the loop around it, never the \fBselect\fR itself. .SS "A name no loop has" .RS 4 .nf void main() { for i in 0..3 { print(i) break outer } } .fi .RE .PP .RS 4 .nf error: no loop around this `break` is named `outer` --> example.sg:4:15 4 | break outer | ^^^^^ hint: a plain `break` leaves the loop it is in .fi .RE .SS "The name of the loop it is in" .RS 4 .nf void main() { outer: for i in 0..3 { print(i) break outer } } .fi .RE .PP .RS 4 .nf error: this `break` is in the loop named `outer` itself, where a plain `break` does the same --> example.sg:4:15 4 | break outer | ^^^^^ hint: write `break`, and take `outer:` away if no loop inside it names it .fi .RE .PP A loop needs a name only for a loop inside it to leave it. .SS "A line after break" .RS 4 .nf void main() { for i in 0..3 { if i == 1 { break print("never") } print(i) } } .fi .RE .PP .RS 4 .nf error: this line is never run: the `break` before it leaves the loop --> example.sg:5:13 5 | print("never") | ^^^^^^^^^^^^^^ hint: take it away, with the lines after it in the block .fi .RE .PP There are no warnings: a line that can never run is an error, as an arm of a \fBmatch\fR that never runs is. .SS "The name of a loop around it, again" .RS 4 .nf void main() { outer: for i in 0..3 { outer: for j in 0..3 { break outer } print(i) } } .fi .RE .PP .RS 4 .nf error: a loop around this one is already named `outer` --> example.sg:3:9 3 | outer: for j in 0..3 { | ^^^^^ it is named here: .fi .RE .PP \fBbreak outer\fR would not say which loop it leaves. .SS "A while true that break name leaves, at the end of a function" .RS 4 .nf int firstPair(read List xs, int sum) { nint i = 0 search: while true { nint j = i + 1 while true { if j >= xs.length() { break } if xs[i] + xs[j] == sum { break search } j += 1 } i += 1 } } void main() { var xs = List { 1, 4, 6 } print(firstPair(read xs, 10)) } .fi .RE .PP .RS 4 .nf error: `firstPair` does not return a value on every path; it must return `int` --> example.sg:1:5 1 | int firstPair(read List xs, int sum) { | ^^^^^^^^^ .fi .RE .PP \fBbreak search\fR leaves the outer loop from inside the inner one, and reaches the end of the function with nothing to return. .SS "A break hidden in a value" .RS 4 .nf int sumUntilZero(read List xs) { var total = 0 nint i = 0 while true { total += match xs[i] { 0 => break var n => n } i += 1 } } void main() { var xs = List { 1, 2, 0 } print(sumUntilZero(read xs)) } .fi .RE .PP .RS 4 .nf error: `sumUntilZero` does not return a value on every path; it must return `int` --> example.sg:1:5 1 | int sumUntilZero(read List xs) { | ^^^^^^^^^^^^ .fi .RE .PP The compiler finds a \fBbreak\fR wherever it stands, here in an arm of a \fBmatch\fR\&. \fBreturn total\fR after the loop makes it whole. .SS "A variable a break leaves maybe null" .RS 4 .nf void main() { var words = List { "", "", "first" } nint i = 0 string? found = null while found is null { if i == words.length() { break } if words[i].length() > 0 { found = words[i].clone() } i += 1 } print(found.length()) } .fi .RE .PP .RS 4 .nf error: this is `string?` and may be `null`; `.length()` needs a value --> example.sg:14:11 14 | print(found.length()) | ^^^^^ hint: test it first: `if found is not null { ... }` .fi .RE .PP The \fBbreak\fR leaves the loop while \fBfound\fR is still \fBnull\fR, so after the loop it is a \fBstring?\fR\&. An \fBif found is null { ... } else { ... }\fR after the loop tests it again. .SS "The names of is not, with a break" .RS 4 .nf void main() { var lines = List { "a", "b", "7" } nint i = 0 while int.parse(read lines[i]) is not Ok(value) { i += 1 if i == lines.length() { break } } print(value) } .fi .RE .PP .RS 4 .nf error: nothing here can use the names in this pattern after a `while`, the names of `is not` are seen only when the test is what ends the loop --> example.sg:4:43 4 | while int.parse(read lines[i]) is not Ok(value) { | ^^^^^^^^^ this `break` leaves the loop while the test may still hold: .fi .RE .PP Without the \fBbreak\fR, the loop could end only once \fBparse\fR gave \fBOk\fR, and \fBvalue\fR would be seen after it. .SS "A file not closed at the break" .RS 4 .nf Result main() { var names = List { "a.txt", "b.txt", "c.txt" } for name in read names { var out = try File.createText(read name) if name == "b.txt" { break } try out.write("hello\en") try out.close() } return Ok() } .fi .RE .PP .RS 4 .nf error: `out` is not closed at this `break`, so an error writing its buffer would be lost --> example.sg:6:13 6 | break | ^^^^^ it is opened here: .fi .RE .PP Closing can fail, and that error must be seen. \fBtry out.close()\fR before the \fBbreak\fR closes it. .SH "SEE ALSO" .BR sg-continue (7), .BR sg-for (7), .BR sg-while (7), .BR sg-match (7), .BR sg-return (7) .PP https://sg\-lang.org/reference/break.html