.\" Made by website/plain/build.mjs from website/words/continue.md; change that file. .TH SG-CONTINUE 7 2026-11-28 "SG" "SG reference" .SH NAME continue \- ends this round of the loop and goes on with the next .SH SYNOPSIS .TP \fBcontinue\fR ends this round of the loop it is in; the next round starts .TP \fBcontinue name\fR ends the round of every loop inside the loop named name, and goes on with its next round .TP \fBname: for ..., name: while ...\fR a loop with a name, for continue name .TP \fBpattern => continue\fR an arm of a match or a select that goes on with the loop around it .SH DESCRIPTION \fBcontinue\fR skips the rest of the round. In a \fBfor\fR, the next round has the next number or the next item; in a \fBwhile\fR, the next round starts with the condition, tested again. It takes no value. Inside a loop within a loop, it acts on the inner one; \fBcontinue name\fR leaves the loops inside the one named \fBname\fR and goes on with its next round. .PP What the block would have done after the \fBcontinue\fR is not done. In a \fBwhile\fR, a count moved on at the end of the block is not moved on, and the loop goes round on the same value for ever: move it on before the \fBcontinue\fR, as in Skipping a round on the page of \fBwhile\fR\&. .PP A \fBcontinue\fR belongs to the loops of its own function. A \fBmatch\fR is not a loop, so a \fBcontinue\fR in an arm goes on with the loop around the \fBmatch\fR; so does one in an arm of a \fBselect\fR\&. It may stand where a value is expected, in an arm of a \fBmatch\fR that gives a value. .PP A \fBcontinue\fR starts a round, so it is checked as the end of the block is. What the round owns is let go there, in compiled code too. A value from outside the round that was given away must have a new value before the \fBcontinue\fR, since the next round may use it. A file still open, or a task not yet waited for, is an error at the \fBcontinue\fR\&. .SH EXAMPLES .SS "Skipping what is not wanted" .RS 4 .nf void main() { var config = "# the size of the window\enwidth = 80\en\enheight = 24\en# done\en" for line in config.lines() { if line.length() == 0 || line.startsWith("#") { continue } print(line) } } .fi .RE .PP .RS 4 .nf width = 80 height = 24 .fi .RE .PP An empty line and a line that starts with \fB#\fR end their round before \fBprint\fR\&. .SS "continue where a value goes" .RS 4 .nf void main() { var inputs = List { "12", "", "x7", "30", "-4" } var total = 0 var skipped = 0 for text in read inputs { var n = match int.parse(read text) { Ok(value) => value Error(_) => { skipped += 1 continue } } total += n } print($"total {total}, {skipped} skipped") } .fi .RE .PP .RS 4 .nf total 38, 2 skipped .fi .RE .PP \fBn\fR is the number when the text is one. For \fB""\fR and \fB"x7"\fR, the arm counts the text and goes on with the next item, so \fBn\fR is never given a value it does not have. .SS "Going on with an outer while" .RS 4 .nf void main() { var codes = List { "4021", "33a1", "7", "12-5", "900" } var sums = List() nint i = 0 lines: while i < codes.length() { var code = read codes[i] i += 1 var sum = 0 for c in code.chars() { if !c.isAsciiDigit() { continue lines } sum += (int)c - (int)'0' } sums.add(sum) } print(sums) } .fi .RE .PP .RS 4 .nf [7, 7, 9] .fi .RE .PP At the first character that is not a digit, \fBcontinue lines\fR drops the rest of the code and the sum, and the \fBwhile\fR tests its condition again. \fBi\fR is moved on at the top of the block, so every way back to the condition has moved it. .SS "continue in a select" .RS 4 .nf void numbers(Sender out) { for i in -3..5 { _ = out.send(i) } } void words(Sender out) { _ = out.send("ab".clone()) _ = out.send("cde".clone()) } void main() { var ints = Channel(4) var texts = Channel(4) var a = Task.run(numbers, ints.sender()) var b = Task.run(words, texts.sender()) var sum = 0 var letters = 0 var kept = 0 while true { select { ints.receive() as n => { if n <= 0 { continue } sum += n } texts.receive() as w => letters += (int)w.length() ended => break } kept += 1 } a.wait() b.wait() print($"{kept} kept: {sum} from numbers, {letters} letters") } .fi .RE .PP .RS 4 .nf 6 kept: 10 from numbers, 5 letters .fi .RE .PP The \fBcontinue\fR goes back to the top of the \fBwhile\fR, past \fBkept += 1\fR, so the numbers up to 0 are not counted. \fBended => break\fR leaves the loop once both tasks have ended. .SS "A value given away, and given again" .RS 4 .nf void keep(List names) { print($"kept {names.length()}: {names}") } void main() { var batch = List() var words = List { "one", "two", "three", "four", "five" } for word in read words { batch.add(word.clone()) if batch.length() == 2 { keep(batch) batch = List() continue } print($"waiting with {word}") } print($"{batch.length()} left over") } .fi .RE .PP .RS 4 .nf waiting with one kept 2: ["one", "two"] waiting with three kept 2: ["three", "four"] waiting with five 1 left over .fi .RE .PP \fBkeep\fR takes the list. A new empty list goes into \fBbatch\fR before the \fBcontinue\fR, so the next round has one to add to. .SH "WHAT THE COMPILER REFUSES" .SS "continue in a function a loop calls" .RS 4 .nf void work(int n) { if n == 2 { continue } print(n) } void main() { for i in 0..4 { work(i) } } .fi .RE .PP .RS 4 .nf error: `continue` can only be written inside a loop --> example.sg:3:9 3 | continue | ^^^^^^^^ .fi .RE .PP \fBwork\fR can \fBreturn\fR early instead: the loop then goes on by itself. .SS "continue with a value" .RS 4 .nf void main() { for i in 0..3 { continue "next" } } .fi .RE .PP .RS 4 .nf error: `continue` takes no value --> example.sg:3:18 3 | continue "next" | ^^^^^^ .fi .RE .SS "A name no loop has" .RS 4 .nf void main() { for i in 0..4 { if i == 2 { continue outer } print(i) } } .fi .RE .PP .RS 4 .nf error: no loop around this `continue` is named `outer` --> example.sg:4:22 4 | continue outer | ^^^^^ hint: a plain `continue` goes on with the loop it is in .fi .RE .SS "The name of the loop it is in" .RS 4 .nf void main() { rounds: for i in 0..4 { if i == 2 { continue rounds } print(i) } } .fi .RE .PP .RS 4 .nf error: this `continue` is in the loop named `rounds` itself, where a plain `continue` does the same --> example.sg:4:22 4 | continue rounds | ^^^^^^ hint: write `continue`, and take `rounds:` away if no loop inside it names it .fi .RE .SS "A line after continue" .RS 4 .nf void main() { for i in 0..3 { if i == 1 { continue print("never") } print(i) } } .fi .RE .PP .RS 4 .nf error: this line is never run: the `continue` before it goes on with the next turn of the loop --> example.sg:5:13 5 | print("never") | ^^^^^^^^^^^^^^ hint: take it away, with the lines after it in the block .fi .RE .SS "A value given away, then continue" .RS 4 .nf void keep(List names) { print(names.length()) } void main() { var batch = List() var words = List { "one", "two", "three" } for word in read words { batch.add(word.clone()) if batch.length() == 2 { keep(batch) continue } } } .fi .RE .PP .RS 4 .nf error: `batch` is moved inside a loop, so the next iteration cannot use it --> example.sg:11:18 11 | keep(batch) | ^^^^^ .fi .RE .PP The next round adds to \fBbatch\fR, which is gone. \fBbatch = List()\fR before the \fBcontinue\fR gives it a new list. .SS "A task not waited for at the continue" .RS 4 .nf int square(int n) { return n * n } void main() { for i in 0..3 { var t = Task.run(square, i) if i == 1 { continue } print(t.wait()) } } .fi .RE .PP .RS 4 .nf error: `t` is not waited for at this `continue` every task is waited for: its error is not lost, and no work is cut off --> example.sg:9:13 9 | continue | ^^^^^^^^ the task is kept here: .fi .RE .PP The round ends at the \fBcontinue\fR, and \fBt\fR with it. \fBt.wait()\fR before the \fBcontinue\fR waits for it; \fB_ = t.wait()\fR drops what it gives. .SH "SEE ALSO" .BR sg-break (7), .BR sg-for (7), .BR sg-while (7), .BR sg-match (7) .PP https://sg\-lang.org/reference/continue.html