.\" Made by website/plain/build.mjs from website/words/else.md; change that file. .TH SG-ELSE 7 2026-11-28 "SG" "SG reference" .SH NAME else \- the block that runs when the condition of an if is false .SH SYNOPSIS .TP \fBif c { ... } else { ... }\fR the second block runs when c is false .TP \fB} else if d {\fR a further test, tried only when the ones above failed .SH DESCRIPTION \fBelse\fR is written only after the block of an \fBif\fR, on the same line as its \fB}\fR, and is followed by a block in braces or by another \fBif\fR\&. Of an \fBif\fR and its \fBelse\fR, exactly one block runs. A chain of \fBelse if\fR is tried from the top, and at most one block of it runs; a last \fBelse\fR runs when no condition held. .PP In the \fBelse\fR block the compiler knows the condition failed. After \fBif x is null\fR, \fBx\fR is a \fBT\fR in \fBelse\fR; after \fBif x is not Ok(v)\fR, \fBv\fR is seen there. When the \fBelse\fR block always leaves (\fBreturn\fR, \fBbreak\fR, \fBcontinue\fR), the code after the \fBif\fR knows the condition held: the names of an \fBis\fR and what it narrows are seen there too. .PP When every block of a chain returns and the chain ends with an \fBelse\fR, a function needs no \fBreturn\fR after it. .PP \fBelse\fR has no other place. A \fBmatch\fR takes the rest with \fB_\fR or \fBvar name\fR, \fB??\fR gives a value in place of \fBnull\fR, and in \fBc ? a : b\fR the arm after \fB:\fR is the value when \fBc\fR is \fBfalse\fR\&. .SH EXAMPLES .SS "The other way" .RS 4 .nf void main() { var balance = 40 var price = 55 if balance >= price { balance -= price print($"paid, {balance} left") } else { print($"{price - balance} short") } } .fi .RE .PP .RS 4 .nf 15 short .fi .RE .PP The balance is too small, so the \fBelse\fR block runs. .SS "A chain of else if" .RS 4 .nf string grade(int score) { if score >= 90 { return "A" } else if score >= 75 { return "B" } else if score >= 50 { return "C" } else { return "F" } } void main() { print(grade(95)) print(grade(80)) print(grade(12)) } .fi .RE .PP .RS 4 .nf A B F .fi .RE .PP 95 is also at least 75, but the first test that holds picks the block. The last \fBelse\fR covers every other score, so \fBgrade\fR returns on every path. .SS "What the else block knows" .RS 4 .nf void describe(read string? name) { if name is null { print("no name") } else { print($"{name}, {name.length()} letters") } } int portOf(read string text) { if int.parse(text) is not Ok(port) { return 80 } else if port > 65535 { return 80 } else { return port } } void main() { describe(null) describe("Pasha") print(portOf("8080")) print(portOf("web")) } .fi .RE .PP .RS 4 .nf no name Pasha, 5 letters 8080 80 .fi .RE .PP In \fBelse\fR, \fBname\fR is not \fBnull\fR, and the number \fBport\fR of a parse that did not fail is seen. [\fBis\fR](is.md) has the whole of these rules. .SS "After an if whose else leaves" .RS 4 .nf enum Shape { Circle(int radius) Rect(int width, int height) } int diameterOf(read Shape shape) { if shape is Circle(r) { print("a circle") } else { return 0 } return r * 2 } int lengthOf(read string? text) { if text is not null { print("a text") } else { return -1 } return (int)text.length() } void main() { print(diameterOf(read Shape.Circle(5))) print(diameterOf(read Shape.Rect(2, 3))) print(lengthOf("tea")) print(lengthOf(null)) } .fi .RE .PP .RS 4 .nf a circle 10 0 a text 3 -1 .fi .RE .PP Only a circle gets past the \fBif\fR, so \fBr\fR is seen after it. In \fBlengthOf\fR, \fBtext\fR is a \fBread string\fR after the \fBif\fR for the same reason. .SH "WHAT THE COMPILER REFUSES" .SS "else on a line of its own" .RS 4 .nf void main() { var count = 3 if count > 2 { print("many") } else { print("few") } } .fi .RE .PP .RS 4 .nf error: `else` cannot start a new line --> example.sg:6:5 6 | else { | ^^^^ hint: put `else` on the same line as `}`: `} else {` .fi .RE .SS "else without braces" .RS 4 .nf void main() { var count = 3 if count > 2 { print("many") } else print("few") } .fi .RE .PP .RS 4 .nf error: expected `{`, found `print` --> example.sg:5:12 5 | } else print("few") | ^^^^^ .fi .RE .SS "A chain with no last else" .RS 4 .nf string grade(int score) { if score >= 90 { return "A" } else if score >= 50 { return "C" } } void main() { print(grade(95)) } .fi .RE .PP .RS 4 .nf error: `grade` does not return a value on every path; it must return `string` --> example.sg:1:8 1 | string grade(int score) { | ^^^^^ .fi .RE .PP A score under 50 reaches the end of the function with nothing to return. An \fBelse { return "F" }\fR makes it whole. .SS "A value that is null in the else" .RS 4 .nf void greet(read string? name) { if name is not null { print($"hello, {name}") } else { print(name.length()) } } void main() { greet(null) } .fi .RE .PP .RS 4 .nf error: `name` is `null` here, as the `if` around it tested; `.length()` needs a value --> example.sg:5:15 5 | print(name.length()) | ^^^^ .fi .RE .PP In \fBelse\fR, the test \fBname is not null\fR failed: \fBname\fR is \fBnull\fR there, and the message says so. .SH "SEE ALSO" .BR sg-if (7), .BR sg-is (7), .BR sg-return (7), .BR sg-match (7) .PP https://sg\-lang.org/reference/else.html