.\" Made by website/plain/build.mjs from website/words/while.md; change that file. .TH SG-WHILE 7 2026-11-28 "SG" "SG reference" .SH NAME while \- repeats a block as long as a condition holds .SH SYNOPSIS .TP \fBwhile condition { ... }\fR runs the block again and again while the condition holds .TP \fBwhile true { ... }\fR a loop with no end of its own: break or return leaves it .TP \fBbreak\fR leaves the loop at once .TP \fBcontinue\fR goes on with the next round, from the condition .TP \fBname: while ...\fR a loop with a name, for break name and continue name in a loop inside it .TP \fBwhile x is Circle(r) { ... }\fR goes on while x is that variant; r is seen in the block .TP \fBwhile x is null { ... }\fR goes on while x is null; after the loop, x holds a value .SH DESCRIPTION \fBwhile\fR tests its condition before each round and runs the block while it holds. The condition is a \fBbool\fR, written without brackets, and the \fB{\fR stays on its line. When the condition fails the first time, the block does not run at all. .PP \fBwhile true\fR is the loop that never ends by itself; there is no other word for it. Only a \fBbreak\fR, a \fBreturn\fR or the end of the program stops it. A function that ends with a \fBwhile true\fR that no \fBbreak\fR leaves needs no \fBreturn\fR after it, since nothing gets past the loop. Any other \fBwhile\fR can end, so the code after it must still return. .PP \fBbreak\fR leaves the loop it is in, and \fBcontinue\fR goes back to the condition for the next round. Inside a loop within a loop, both act on the inner one. To act on an outer loop, name it: \fBouter: while ...\fR, then \fBbreak outer\fR or \fBcontinue outer\fR\&. A name that no \fBbreak\fR or \fBcontinue\fR uses is an error, and so is the name of a loop around it used again. .PP An \fBis\fR in the condition names what the value carries, for the block. After a \fBwhile x is null\fR, \fBx\fR is known to hold a value, since the loop ends only when the test fails; a \fBbreak\fR inside the loop turns that off. .PP What the block moves, it moves in every round: a value given away inside a loop is an error, since the next round would use it again. .SH EXAMPLES .SS "Counting down" .RS 4 .nf void main() { var n = 5 while n > 0 { print(n) n -= 1 } print("lift off") } .fi .RE .PP .RS 4 .nf 5 4 3 2 1 lift off .fi .RE .PP The condition is tested before every round. When \fBn\fR reaches 0 it fails, and the line after the loop runs. .SS "A loop with no end of its own" .RS 4 .nf int firstSquareOver(int limit) { var i = 1 while true { if i * i > limit { return i } i += 1 } } void main() { print(firstSquareOver(50)) var tries = 0 while true { tries += 1 if tries == 3 { break } } print(tries) } .fi .RE .PP .RS 4 .nf 8 3 .fi .RE .PP \fBfirstSquareOver\fR leaves its loop only through \fBreturn\fR, so it needs no \fBreturn\fR after the loop. In \fBmain\fR, \fBbreak\fR leaves the loop and the program goes on below it. .SS "Skipping a round" .RS 4 .nf void main() { var words = List { "a", "bb", "", "ccc" } nint i = 0 var letters = 0 while i < words.length() { var w = read words[i] i += 1 if w.length() == 0 { continue } letters += (int)w.length() } print(letters) } .fi .RE .PP .RS 4 .nf 6 .fi .RE .PP \fBcontinue\fR goes back to the condition. \fBi\fR is moved on before it; had it been moved on at the end of the block, the empty word would have kept the loop on it for ever. .SS "Leaving two loops at once" .RS 4 .nf void main() { var row = 0 search: while row < 4 { var col = 0 while col < 4 { if row * col == 6 { print($"found at {row}, {col}") break search } col += 1 } row += 1 } } .fi .RE .PP .RS 4 .nf found at 2, 3 .fi .RE .PP A plain \fBbreak\fR would leave only the inner loop, and the outer one would go on with the next row. \fBbreak search\fR leaves both. \fBcontinue search\fR would go on with the outer loop's next round. .SS "As long as a pattern fits" .RS 4 .nf enum Shape : Formattable { Circle(int radius) Square(int side) } void main() { var s = Shape.Circle(1) while s is Circle(r) && r < 4 { s = Shape.Circle(r + 1) } print(s) } .fi .RE .PP .RS 4 .nf Circle(4) .fi .RE .PP \fBr\fR is the radius in each round. It only borrows from \fBs\fR, and it is read before \fBs\fR is given a new value, so the assignment is allowed. .SS "After the loop" .RS 4 .nf void main() { var words = List { "", "", "first", "second" } nint i = 0 string? found = null while found is null { if words[i].length() > 0 { found = words[i].clone() } i += 1 } print($"{found} has {found.length()} letters") } .fi .RE .PP .RS 4 .nf first has 5 letters .fi .RE .PP The loop ends only when \fBfound\fR is not \fBnull\fR, so after it \fBfound\fR is a \fBstring\fR and has a \fBlength()\fR\&. With a \fBbreak\fR in the loop, that would no longer be sure, and \fBfound.length()\fR would be refused. .SH "WHAT THE COMPILER REFUSES" .SS "A condition that is not a bool" .RS 4 .nf void main() { var n = 3 while n { n -= 1 } } .fi .RE .PP .RS 4 .nf error: expected `bool`, found `int` --> example.sg:3:11 3 | while n { | ^ hint: a number is not a condition; compare it: `n != 0` .fi .RE .SS "A struct built in the condition" .RS 4 .nf struct Point : Equatable { int x int y } void main() { var p = Point { x = 0, y = 0 } while p != Point { x = 3, y = 0 } { p.x += 1 } } .fi .RE .PP .RS 4 .nf error: a struct built in a condition is written in brackets, since `{` here starts the block --> example.sg:8:16 8 | while p != Point { x = 3, y = 0 } { | ^^^^^^^^^^^^^^^^^^^^^^ hint: `while p != (Point { x = 3, y = 0 })` .fi .RE .PP The first \fB{\fR after the condition starts the body of the loop, so a struct built there goes in brackets. The same holds for \fBif\fR\&. .SS "break outside a loop" .RS 4 .nf void main() { if true { break } } .fi .RE .PP .RS 4 .nf error: `break` can only be written inside a loop --> example.sg:3:9 3 | break | ^^^^^ .fi .RE .SS "A name no break uses" .RS 4 .nf void main() { var n = 3 outer: while n > 0 { n -= 1 } } .fi .RE .PP .RS 4 .nf error: no `break outer` or `continue outer` uses the name of this loop --> example.sg:3:5 3 | outer: while n > 0 { | ^^^^^ hint: a loop needs a name only for a loop inside it to leave it: remove `outer:` .fi .RE .SS "A while true that a break leaves, at the end of a function" .RS 4 .nf int firstOver(read List xs, int limit) { nint i = 0 while true { if i == xs.length() { break } if xs[i] > limit { return xs[i] } i += 1 } } void main() { var xs = List { 3, 8, 12 } print(firstOver(read xs, 5)) } .fi .RE .PP .RS 4 .nf error: `firstOver` does not return a value on every path; it must return `int` --> example.sg:1:5 1 | int firstOver(read List xs, int limit) { | ^^^^^^^^^ .fi .RE .PP The \fBbreak\fR reaches the end of the function with nothing to return. A \fBreturn \-1\fR after the loop makes it whole. .SS "A value moved inside the loop" .RS 4 .nf void keep(string s) { print(s) } void main() { var name = "Pasha".clone() var n = 0 while n < 2 { keep(name) n += 1 } } .fi .RE .PP .RS 4 .nf error: `name` is moved inside a loop, so the next iteration cannot use it --> example.sg:9:14 9 | keep(name) | ^^^^ .fi .RE .PP \fBkeep\fR takes the text, so the second round would have none. \fBkeep(name.clone())\fR gives each round a copy; a \fBkeep(read string s)\fR, called \fBkeep(read name)\fR, would only borrow it. .SS "{ on a line of its own" .RS 4 .nf void main() { var n = 3 while n > 0 { n -= 1 } } .fi .RE .PP .RS 4 .nf error: `{` cannot start a new line --> example.sg:4:5 4 | { | ^ hint: put `{` at the end of the previous line .fi .RE .SH "SEE ALSO" .BR sg-for (7), .BR sg-break (7), .BR sg-continue (7), .BR sg-is (7), .BR sg-if (7) .PP https://sg\-lang.org/reference/while.html